Repository navigation
fix(os): serialize process environment updates - #36074
Conversation
bartlomieju
left a comment
There was a problem hiding this comment.
Nice hardening, and I appreciate that the guard's doc comment is honest about not being able to coordinate non-Deno native readers. I checked lock ordering across the tracker mutex and the env guard and didn't find a deadlock (consistent self.inner -> ProcessEnvGuard order, and the guard is reused rather than re-locked inside the dotenv loops), and the process_env_lock_is_held_through_timezone_notification test genuinely proves the notification stays inside the critical section.
Two notes below are inline. One more that I couldn't attach inline because it's outside the diff: the startup free function load_env_variables_from_env_files (around line 303+) still uses raw env::vars_os()/env::set_var without the guard. It runs pre-worker and single-threaded so it's low risk, but it's the one env-mutation path left uncoordinated, which slightly undercuts the "all runtime paths go through the guard" framing. Worth either routing it through the guard too or noting why it's intentionally exempt.
Also a minor perf observation (non-blocking): every Deno.env.get/toObject now takes a process-global mutex, so env reads are serialized across all isolates. Not a hot path, just noting the added contention surface.
## Summary - coordinate runtime process-environment access with a shared guard - keep `TZ` mutation, native timezone refresh, and V8 timezone redetection in one critical section - use the same coordination for Node dotenv loading and watched dotenv reloads - cover `TZ` deletion and `process.loadEnvFile()` timezone updates ## Details Process-environment updates can originate from multiple runtime workers. `TZ` changes also refresh native and V8 timezone state, but those stages were not coordinated as one operation, so another environment update could overlap the refresh. This adds a guard for the runtime environment paths and keeps it held through the complete timezone update. The guard is also used by Node dotenv loading and watched dotenv reloads so those paths follow the same ordering. ## Validation - `./tools/format.js` - `cargo check -p deno_os -p deno_node` - `cargo check -p deno` - `cargo test -p deno_os process_env_lock_is_held_through_timezone_notification` - `cargo build --bin deno --bin test_server` - `cargo test -p specs_tests --test specs -- run::tz_env` - `cargo test -p specs_tests --test specs -- run::process_env_load::env_load_file_updates_timezone` - bounded eight-worker environment and timezone update stress
Summary
TZmutation, native timezone refresh, and V8 timezone redetection in one critical sectionTZdeletion andprocess.loadEnvFile()timezone updatesDetails
Process-environment updates can originate from multiple runtime workers.
TZchanges also refresh native and V8 timezone state, but those stages were not coordinated as one operation, so another environment update could overlap the refresh.This adds a guard for the runtime environment paths and keeps it held through the complete timezone update. The guard is also used by Node dotenv loading and watched dotenv reloads so those paths follow the same ordering.
Validation
./tools/format.jscargo check -p deno_os -p deno_nodecargo check -p denocargo test -p deno_os process_env_lock_is_held_through_timezone_notificationcargo build --bin deno --bin test_servercargo test -p specs_tests --test specs -- run::tz_envcargo test -p specs_tests --test specs -- run::process_env_load::env_load_file_updates_timezone