Skip to content

fix(os): serialize process environment updates - #36074

Merged
nathanwhit merged 2 commits into
denoland:mainfrom
nathanwhit:fix/process-environment-updates
Aug 10, 2026
Merged

nathanwhit merged 2 commits into
denoland:mainfrom
nathanwhit:fix/process-environment-updates

Conversation

@nathanwhit

Copy link
Copy Markdown
Member

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

@nathanwhit
nathanwhit marked this pull request as ready for review July 15, 2026 22:55

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cli/util/env.rs Resolved
Comment thread cli/util/env.rs Resolved
@nathanwhit
nathanwhit merged commit 19c25e2 into denoland:main Aug 10, 2026
136 checks passed
@nathanwhit
nathanwhit deleted the fix/process-environment-updates branch August 10, 2026 19:22
bartlomieju pushed a commit that referenced this pull request Aug 27, 2026
## 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants