Skip to content

Compiler panic with a Box<dyn std::error::Error> in a future #143618

Open
@kthui

Description

@kthui

Code

Change this block of code to the following:

                let stream = stream.then(move |res| {
                    let client = client.clone();
                    async move {
                        if let Some(err) = res.err() {
                            const STREAM_ERR_MSG: &str = "Stream ended before generation completed";
                            if format!("{:?}", err) == STREAM_ERR_MSG {
                                client.report_instance_down(instance_id).await;
                            }
                        }
                        res
                    }
                });

which essentially access the err, that should be typed Box<dyn std::error::Error>, inside the future block.

Meta

rustc --version --verbose:

rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1

Error output

At the root directory of the repository, compile the code with

RUST_BACKTRACE=1 cargo +nightly build --release --locked --workspace

Output:

thread 'rustc' panicked at compiler/rustc_mir_transform/src/validate.rs:80:25:
broken MIR in Item(DefId(0:947 ~ dynamo_runtime[774d]::pipeline::network::egress::push_router::{impl#1}::generate_with_fault_detection::{closure#0})) (after phase change to runtime-optimized) at bb19[0]:
Unsize coercion, but `std::pin::Pin<std::boxed::Box<tokio_stream::adapters::Then<std::pin::Pin<std::boxed::Box<dyn engine::AsyncEngineStream<U>>>, {async block@lib/runtime/src/pipeline/network/egress/push_router.rs:200:21: 200:31}, {closure@lib/runtime/src/pipeline/network/egress/push_router.rs:198:42: 198:52}>>>` isn't coercible to `std::pin::Pin<std::boxed::Box<dyn futures::Stream<Item = U> + std::marker::Send + std::marker::Sync>>`
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: <rustc_mir_transform::validate::CfgChecker>::fail::<alloc::string::String>
   3: <rustc_mir_transform::validate::Validator as rustc_mir_transform::pass_manager::MirPass>::run_pass
   4: rustc_mir_transform::run_optimization_passes
   5: rustc_mir_transform::optimized_mir
      [... omitted 1 frame ...]
   6: <rustc_middle::ty::context::TyCtxt>::coroutine_layout
   7: rustc_ty_utils::layout::layout_of_uncached
   8: rustc_ty_utils::layout::layout_of
      [... omitted 1 frame ...]
   9: <rustc_mir_transform::known_panics_lint::KnownPanicsLint as rustc_mir_transform::pass_manager::MirLint>::run_lint
  10: rustc_mir_transform::run_analysis_to_runtime_passes
  11: rustc_mir_transform::mir_drops_elaborated_and_const_checked
      [... omitted 1 frame ...]
  12: <rustc_middle::ty::context::TyCtxt>::par_hir_body_owners::<rustc_interface::passes::run_required_analyses::{closure#1}::{closure#0}>::{closure#0}
  13: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  14: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  15: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: please make sure that you have updated to the latest nightly

note: please attach the file at `/workspace/rustc-ice-2025-07-07T22_40_06-29919.txt` to your bug report

note: compiler flags: --crate-type lib -C opt-level=3 -C linker-plugin-lto -C codegen-units=1 -C strip=debuginfo

note: some of the compiler flags provided by cargo are hidden
Backtrace

Full output

RUST_BACKTRACE=1 cargo +nightly build --release --locked --workspace
   Compiling dynamo-runtime v0.3.1 (/workspace/lib/runtime)
warning: [email protected]: Building with CUDA KV off
warning: [email protected]: CUDA not enabled, re-run with `--features cuda`

thread 'rustc' panicked at compiler/rustc_mir_transform/src/validate.rs:80:25:
broken MIR in Item(DefId(0:947 ~ dynamo_runtime[774d]::pipeline::network::egress::push_router::{impl#1}::generate_with_fault_detection::{closure#0})) (after phase change to runtime-optimized) at bb19[0]:
Unsize coercion, but `std::pin::Pin<std::boxed::Box<tokio_stream::adapters::Then<std::pin::Pin<std::boxed::Box<dyn engine::AsyncEngineStream<U>>>, {async block@lib/runtime/src/pipeline/network/egress/push_router.rs:200:21: 200:31}, {closure@lib/runtime/src/pipeline/network/egress/push_router.rs:198:42: 198:52}>>>` isn't coercible to `std::pin::Pin<std::boxed::Box<dyn futures::Stream<Item = U> + std::marker::Send + std::marker::Sync>>`
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: <rustc_mir_transform::validate::CfgChecker>::fail::<alloc::string::String>
   3: <rustc_mir_transform::validate::Validator as rustc_mir_transform::pass_manager::MirPass>::run_pass
   4: rustc_mir_transform::run_optimization_passes
   5: rustc_mir_transform::optimized_mir
      [... omitted 1 frame ...]
   6: <rustc_middle::ty::context::TyCtxt>::coroutine_layout
   7: rustc_ty_utils::layout::layout_of_uncached
   8: rustc_ty_utils::layout::layout_of
      [... omitted 1 frame ...]
   9: <rustc_mir_transform::known_panics_lint::KnownPanicsLint as rustc_mir_transform::pass_manager::MirLint>::run_lint
  10: rustc_mir_transform::run_analysis_to_runtime_passes
  11: rustc_mir_transform::mir_drops_elaborated_and_const_checked
      [... omitted 1 frame ...]
  12: <rustc_middle::ty::context::TyCtxt>::par_hir_body_owners::<rustc_interface::passes::run_required_analyses::{closure#1}::{closure#0}>::{closure#0}
  13: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  14: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  15: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: please make sure that you have updated to the latest nightly

note: please attach the file at `/workspace/rustc-ice-2025-07-07T22_40_06-29919.txt` to your bug report

note: compiler flags: --crate-type lib -C opt-level=3 -C linker-plugin-lto -C codegen-units=1 -C strip=debuginfo

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [optimized_mir] optimizing MIR for `pipeline::network::egress::push_router::<impl at lib/runtime/src/pipeline/network/egress/push_router.rs:96:1: 99:54>::generate_with_fault_detection::{closure#0}`
#1 [layout_of] computing layout of `{async fn body of pipeline::network::egress::push_router::<impl at lib/runtime/src/pipeline/network/egress/push_router.rs:96:1: 99:54>::generate_with_fault_detection()}`
#2 [mir_drops_elaborated_and_const_checked] elaborating drops for `pipeline::network::egress::push_router::<impl at lib/runtime/src/pipeline/network/egress/push_router.rs:96:1: 99:54>::generate_with_fault_detection`
#3 [analysis] running analysis passes on this crate
end of query stack
error: future cannot be sent between threads safely
   --> lib/runtime/src/pipeline/network/egress/push_router.rs:210:40
    |
210 |                 Ok(ResponseStream::new(Box::pin(stream), engine_ctx))
    |                                        ^^^^^^^^^^^^^^^^ future created by async block is not `Send`
    |
    = help: the trait `std::marker::Send` is not implemented for `dyn StdError`
note: future is not `Send` as this value is used across an await
   --> lib/runtime/src/pipeline/network/egress/push_router.rs:204:74
    |
201 |                         if let Some(err) = res.err() {
    |                                     --- has type `Box<dyn StdError>` which is not `Send`
...
204 |                                 client.report_instance_down(instance_id).await;
    |                                                                          ^^^^^ await occurs here, with `err` maybe used later
    = note: required for the cast from `Pin<Box<Then<Pin<Box<dyn AsyncEngineStream<U>>>, ..., ...>>>` to `Pin<Box<dyn futures::Stream<Item = U> + std::marker::Send + Sync>>`
    = note: the full name for the type has been written to '/workspace/target/release/deps/dynamo_runtime-c73794161cb63689.long-type-6849251775564483029.txt'
    = note: consider using `--verbose` to print the full type name to the console

error: future cannot be shared between threads safely
   --> lib/runtime/src/pipeline/network/egress/push_router.rs:210:40
    |
210 |                 Ok(ResponseStream::new(Box::pin(stream), engine_ctx))
    |                                        ^^^^^^^^^^^^^^^^ future created by async block is not `Sync`
    |
    = help: the trait `Sync` is not implemented for `dyn StdError`
note: future is not `Sync` as this value is used across an await
   --> lib/runtime/src/pipeline/network/egress/push_router.rs:204:74
    |
201 |                         if let Some(err) = res.err() {
    |                                     --- has type `Box<dyn StdError>` which is not `Sync`
...
204 |                                 client.report_instance_down(instance_id).await;
    |                                                                          ^^^^^ await occurs here, with `err` maybe used later
    = note: required for the cast from `Pin<Box<Then<Pin<Box<dyn AsyncEngineStream<U>>>, ..., ...>>>` to `Pin<Box<dyn futures::Stream<Item = U> + std::marker::Send + Sync>>`
    = note: the full name for the type has been written to '/workspace/target/release/deps/dynamo_runtime-c73794161cb63689.long-type-6849251775564483029.txt'
    = note: consider using `--verbose` to print the full type name to the console

error: could not compile `dynamo-runtime` (lib) due to 2 previous errors

rustc-ice-2025-07-07T22_40_06-29919.txt

thread 'rustc' panicked at compiler/rustc_mir_transform/src/validate.rs:80:25:
broken MIR in Item(DefId(0:947 ~ dynamo_runtime[774d]::pipeline::network::egress::push_router::{impl#1}::generate_with_fault_detection::{closure#0})) (after phase change to runtime-optimized) at bb19[0]:
Unsize coercion, but `std::pin::Pin<std::boxed::Box<tokio_stream::adapters::Then<std::pin::Pin<std::boxed::Box<dyn engine::AsyncEngineStream<U>>>, {async block@lib/runtime/src/pipeline/network/egress/push_router.rs:200:21: 200:31}, {closure@lib/runtime/src/pipeline/network/egress/push_router.rs:198:42: 198:52}>>>` isn't coercible to `std::pin::Pin<std::boxed::Box<dyn futures::Stream<Item = U> + std::marker::Send + std::marker::Sync>>`
stack backtrace:
   0:     0x78b87430cac5 - std::backtrace::Backtrace::create::hafc9430f4c5b7bca
   1:     0x78b87430ca15 - std::backtrace::Backtrace::force_capture::h638ed5ec06317f78
   2:     0x78b8733f3bfc - std[5392b2688ccf6c11]::panicking::update_hook::<alloc[6097bca32dba2b22]::boxed::Box<rustc_driver_impl[486328ec49f3cc14]::install_ice_hook::{closure#1}>>::{closure#0}
   3:     0x78b874326aab - std::panicking::rust_panic_with_hook::he4807f33a3524f43
   4:     0x78b8743267aa - std::panicking::begin_panic_handler::{{closure}}::heb653abb954cf289
   5:     0x78b874322e29 - std::sys::backtrace::__rust_end_short_backtrace::hb1b8fb13fadd2724
   6:     0x78b87432648d - __rustc[c0c724db799d4e69]::rust_begin_unwind
   7:     0x78b870aa83a0 - core::panicking::panic_fmt::hfcd28465d10d6ced
   8:     0x78b8721de3a2 - <rustc_mir_transform[c14be100631af529]::validate::CfgChecker>::fail::<alloc[6097bca32dba2b22]::string::String>
   9:     0x78b875ce0799 - <rustc_mir_transform[c14be100631af529]::validate::Validator as rustc_mir_transform[c14be100631af529]::pass_manager::MirPass>::run_pass
  10:     0x78b874a103d0 - rustc_mir_transform[c14be100631af529]::run_optimization_passes
  11:     0x78b8756adc60 - rustc_mir_transform[c14be100631af529]::optimized_mir
  12:     0x78b8756ad435 - rustc_query_impl[dc4ccaf3cfeb0260]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[dc4ccaf3cfeb0260]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[e63663f80c63ef31]::query::erase::Erased<[u8; 8usize]>>
  13:     0x78b874a375e5 - rustc_query_system[65e39bd11f71302c]::query::plumbing::try_execute_query::<rustc_query_impl[dc4ccaf3cfeb0260]::DynamicConfig<rustc_query_system[65e39bd11f71302c]::query::caches::DefIdCache<rustc_middle[e63663f80c63ef31]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[dc4ccaf3cfeb0260]::plumbing::QueryCtxt, false>
  14:     0x78b874a36adb - rustc_query_impl[dc4ccaf3cfeb0260]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace
  15:     0x78b875e28a09 - <rustc_middle[e63663f80c63ef31]::ty::context::TyCtxt>::coroutine_layout
  16:     0x78b875161f3d - rustc_ty_utils[7ffa9575db8c2558]::layout::layout_of_uncached
  17:     0x78b875154bd4 - rustc_ty_utils[7ffa9575db8c2558]::layout::layout_of
  18:     0x78b875154b40 - rustc_query_impl[dc4ccaf3cfeb0260]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[dc4ccaf3cfeb0260]::query_impl::layout_of::dynamic_query::{closure#2}::{closure#0}, rustc_middle[e63663f80c63ef31]::query::erase::Erased<[u8; 16usize]>>
  19:     0x78b875153962 - rustc_query_system[65e39bd11f71302c]::query::plumbing::try_execute_query::<rustc_query_impl[dc4ccaf3cfeb0260]::DynamicConfig<rustc_query_system[65e39bd11f71302c]::query::caches::DefaultCache<rustc_middle[e63663f80c63ef31]::ty::PseudoCanonicalInput<rustc_middle[e63663f80c63ef31]::ty::Ty>, rustc_middle[e63663f80c63ef31]::query::erase::Erased<[u8; 16usize]>>, false, true, false>, rustc_query_impl[dc4ccaf3cfeb0260]::plumbing::QueryCtxt, false>
  20:     0x78b8751535a4 - rustc_query_impl[dc4ccaf3cfeb0260]::query_impl::layout_of::get_query_non_incr::__rust_end_short_backtrace
  21:     0x78b87599ccb2 - <rustc_mir_transform[c14be100631af529]::known_panics_lint::KnownPanicsLint as rustc_mir_transform[c14be100631af529]::pass_manager::MirLint>::run_lint
  22:     0x78b874a0aa63 - rustc_mir_transform[c14be100631af529]::run_analysis_to_runtime_passes
  23:     0x78b874bb858d - rustc_mir_transform[c14be100631af529]::mir_drops_elaborated_and_const_checked
  24:     0x78b874bb7e31 - rustc_query_impl[dc4ccaf3cfeb0260]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[dc4ccaf3cfeb0260]::query_impl::mir_drops_elaborated_and_const_checked::dynamic_query::{closure#2}::{closure#0}, rustc_middle[e63663f80c63ef31]::query::erase::Erased<[u8; 8usize]>>
  25:     0x78b874bbe2ee - rustc_query_system[65e39bd11f71302c]::query::plumbing::try_execute_query::<rustc_query_impl[dc4ccaf3cfeb0260]::DynamicConfig<rustc_data_structures[7fa2acbf0e33eb94]::vec_cache::VecCache<rustc_span[5b31911843326824]::def_id::LocalDefId, rustc_middle[e63663f80c63ef31]::query::erase::Erased<[u8; 8usize]>, rustc_query_system[65e39bd11f71302c]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[dc4ccaf3cfeb0260]::plumbing::QueryCtxt, false>
  26:     0x78b874bbda55 - rustc_query_impl[dc4ccaf3cfeb0260]::query_impl::mir_drops_elaborated_and_const_checked::get_query_non_incr::__rust_end_short_backtrace
  27:     0x78b874bbce3d - <rustc_middle[e63663f80c63ef31]::ty::context::TyCtxt>::par_hir_body_owners::<rustc_interface[b1f97b06592f8920]::passes::run_required_analyses::{closure#1}::{closure#0}>::{closure#0}
  28:     0x78b874bba610 - rustc_interface[b1f97b06592f8920]::passes::analysis
  29:     0x78b874bb9acb - rustc_query_impl[dc4ccaf3cfeb0260]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[dc4ccaf3cfeb0260]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[e63663f80c63ef31]::query::erase::Erased<[u8; 0usize]>>
  30:     0x78b875a70b3c - rustc_query_system[65e39bd11f71302c]::query::plumbing::try_execute_query::<rustc_query_impl[dc4ccaf3cfeb0260]::DynamicConfig<rustc_query_system[65e39bd11f71302c]::query::caches::SingleCache<rustc_middle[e63663f80c63ef31]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[dc4ccaf3cfeb0260]::plumbing::QueryCtxt, false>
  31:     0x78b875a70706 - rustc_query_impl[dc4ccaf3cfeb0260]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  32:     0x78b875be6ad6 - rustc_interface[b1f97b06592f8920]::passes::create_and_enter_global_ctxt::<core[919d75d9604c1f70]::option::Option<rustc_interface[b1f97b06592f8920]::queries::Linker>, rustc_driver_impl[486328ec49f3cc14]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  33:     0x78b875cf29c3 - rustc_interface[b1f97b06592f8920]::interface::run_compiler::<(), rustc_driver_impl[486328ec49f3cc14]::run_compiler::{closure#0}>::{closure#1}
  34:     0x78b875c07e78 - std[5392b2688ccf6c11]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[b1f97b06592f8920]::util::run_in_thread_with_globals<rustc_interface[b1f97b06592f8920]::util::run_in_thread_pool_with_globals<rustc_interface[b1f97b06592f8920]::interface::run_compiler<(), rustc_driver_impl[486328ec49f3cc14]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  35:     0x78b875c07b56 - <<std[5392b2688ccf6c11]::thread::Builder>::spawn_unchecked_<rustc_interface[b1f97b06592f8920]::util::run_in_thread_with_globals<rustc_interface[b1f97b06592f8920]::util::run_in_thread_pool_with_globals<rustc_interface[b1f97b06592f8920]::interface::run_compiler<(), rustc_driver_impl[486328ec49f3cc14]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[919d75d9604c1f70]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  36:     0x78b875c0617d - std::sys::pal::unix::thread::Thread::new::thread_start::h746b9bcf86250858
  37:     0x78b86f68aaa4 - <unknown>
  38:     0x78b86f717a34 - clone
  39:                0x0 - <unknown>


rustc version: 1.90.0-nightly (a84ab0ce6 2025-07-06)
platform: x86_64-unknown-linux-gnu

query stack during panic:
#0 [optimized_mir] optimizing MIR for `pipeline::network::egress::push_router::<impl at lib/runtime/src/pipeline/network/egress/push_router.rs:96:1: 99:54>::generate_with_fault_detection::{closure#0}`
#1 [layout_of] computing layout of `{async fn body of pipeline::network::egress::push_router::<impl at lib/runtime/src/pipeline/network/egress/push_router.rs:96:1: 99:54>::generate_with_fault_detection()}`
#2 [mir_drops_elaborated_and_const_checked] elaborating drops for `pipeline::network::egress::push_router::<impl at lib/runtime/src/pipeline/network/egress/push_router.rs:96:1: 99:54>::generate_with_fault_detection`
#3 [analysis] running analysis passes on this crate
end of query stack

This issue is not a blocker for our work, but it would be nice to know whether this is a bug in the Rust code that was not handled gracefully by the compiler or the Rust code is well-formed that the compiler just failed to compile.

cc @ryanolson @grahamking if you are also interested in this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions