@@ -265,6 +265,17 @@ impl<'tcx> MainThreadState<'tcx> {
265265 // Deal with our thread-local memory. We do *not* want to actually free it, instead we consider TLS
266266 // to be like a global `static`, so that all memory reached by it is considered to "not leak".
267267 this. terminate_active_thread ( TlsAllocAction :: Leak ) ?;
268+
269+ // Machine cleanup. Only do this if all threads have terminated; threads that are still running
270+ // might cause Stacked Borrows errors (https://github.com/rust-lang/miri/issues/2396).
271+ if this. have_all_terminated ( ) {
272+ // Even if all threads have terminated, we have to beware of data races since some threads
273+ // might not have joined the main thread (https://github.com/rust-lang/miri/issues/2020,
274+ // https://github.com/rust-lang/miri/issues/2508).
275+ this. allow_data_races_all_threads_done ( ) ;
276+ EnvVars :: cleanup ( this) . expect ( "error during env var cleanup" ) ;
277+ }
278+
268279 // Stop interpreter loop.
269280 throw_machine_stop ! ( TerminationInfo :: Exit { code: exit_code, leak_check: true } ) ;
270281 }
@@ -467,16 +478,6 @@ pub fn eval_entry<'tcx>(
467478
468479 // If we get here there was no fatal error.
469480
470- // Machine cleanup. Only do this if all threads have terminated; threads that are still running
471- // might cause Stacked Borrows errors (https://github.com/rust-lang/miri/issues/2396).
472- if ecx. have_all_terminated ( ) {
473- // Even if all threads have terminated, we have to beware of data races since some threads
474- // might not have joined the main thread (https://github.com/rust-lang/miri/issues/2020,
475- // https://github.com/rust-lang/miri/issues/2508).
476- ecx. allow_data_races_all_threads_done ( ) ;
477- EnvVars :: cleanup ( & mut ecx) . expect ( "error during env var cleanup" ) ;
478- }
479-
480481 // Possibly check for memory leaks.
481482 if leak_check && !ignore_leaks {
482483 // Check for thread leaks.
0 commit comments