Skip to content

jobserver: replace the pool when a compiler dies holding tokens - #3047

Merged
sylvestre merged 2 commits into
mozilla:mainfrom
caniko:jobserver-replace-leaked-pool
Nov 26, 2026
Merged

sylvestre merged 2 commits into
mozilla:mainfrom
caniko:jobserver-replace-leaked-pool

Conversation

@caniko

@caniko caniko commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Fixes #2874.

The server shares its private jobserver with the rustc processes it starts. When a client disconnects, the server kills that rustc (kill_on_drop), and any tokens it had read from the pipe never come back. On a long-lived server the pool shrinks until nothing can start. I hit this on 0.19.0 with one daemon shared by many builds: the pool went from 32 to 0 and every compile hung until I restarted it.

With this change, when a child that was given the shared jobserver doesn't exit on its own (it dies from a signal, or its wait is dropped), the server moves new work to a fresh, full pool. Children still running on the old pool give their tokens back to it, and it's dropped once the last of them finishes. A semaphore shared across pools keeps the number of concurrent children at the configured limit, so a replacement never lets more children run than before. Tokens a rustc borrows from an older pool aren't counted against the new one, so the borrowed total can briefly go over the limit until those children finish; the child count can't. Build-owned fifo: jobservers (#2978) are unchanged.

The only dependency change is tokio's sync feature, for the watch channel and the semaphore.

Tests:

  • killed_child_does_not_leak_jobserver_tokens: a child takes a token and kills itself with SIGKILL; both tokens must be available again. It times out on main and passes with this change.
  • replaced_pool_keeps_the_child_limit: with a child still running on the old pool, a replacement must not hand out more slots than the limit.

Both are Linux-only because they read the jobserver fd through /proc, which also keeps them working under dash. cargo clippy with the CI flags and cargo test --locked --lib --bins --tests pass locally. I couldn't run the CUDA/HIP tests here.

I used AI assistance (OpenCode with GPT-6.1 Terra, reviewed with GPT-6.2 Sol and Claude) to write this change. I've read the diff and will answer review questions myself.

Reviewed with sol-loop: 3 rounds with openai/gpt-6.2-sol (xhigh). The last round's only open finding, a line over 100 columns, was fixed afterwards by running rustfmt through treefmt, so there was no final approve verdict.

The server shares its private jobserver with the rustc processes it
starts. When a client disconnects, the server kills that rustc
(kill_on_drop), and any tokens it had read from the pipe never come
back. On a long-lived server the pool shrinks until nothing can start
(mozilla#2874).

When a child that was given the shared jobserver does not exit on its
own, move new work to a fresh, full pool. Children still running on the
old pool return their tokens to it, and it is dropped once the last of
them finishes. A semaphore shared across pools keeps the number of
concurrent children at the configured limit. Build-owned fifo:
jobservers are unchanged.

Fixes mozilla#2874

Assisted-by: OpenCode (openai/gpt-6.1-terra)
@codecov-commenter

codecov-commenter commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.00000% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.03%. Comparing base (ef8e7d7) to head (90f1c13).
⚠️ Report is 23 commits behind head on main.

Files with missing lines Patch % Lines
src/jobserver.rs 93.75% 7 Missing ⚠️
src/mock_command.rs 97.05% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3047      +/-   ##
==========================================
+ Coverage   78.92%   80.03%   +1.10%     
==========================================
  Files          75       76       +1     
  Lines       46005    47504    +1499     
==========================================
+ Hits        36310    38019    +1709     
+ Misses       9695     9485     -210     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/mock_command.rs
let token = self.jobserver.acquire().await?;
let mut inner = tokio::process::Command::from(inner);
let child = inner
.kill_on_drop(true)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

if the spawn fails here, token is dropped with shared set and not exited, so we retire a pool that lost nothing, no?
could you please mark it as exited (or only set shared) once the spawn succeeded?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch — fixed in 90f1c13. Acquired::configure no longer sets shared; mark_shared() is only called after spawn() succeeds, with a failed_spawn_does_not_retire_jobserver_pool test.

Comment thread src/mock_command.rs
r#"a=${CARGO_MAKEFLAGS##*--jobserver-auth=}; [ "$(head -c1 /proc/self/fd/${a%,*} | wc -c)" -eq 1 ] && kill -9 $$"#,
]);
victim.share_jobserver();
let timeout = std::time::Duration::from_secs(10);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

same shell snippet as in the previous test, could be a const, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 90f1c13 — extracted to const LEAK_TOKEN_AND_SIGKILL.

Comment thread src/jobserver.rs
fifo.configure(cmd);
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

exited as both a field and a method that only sets it on_its_own is a bit confusing, maybe exited_on_its_own?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 90f1c13: renamed the field to exited_on_its_own and the method to record_exit so they do not share a name.

- Mark an acquired token as shared only after spawn() succeeds, so a spawn failure does not retire a healthy pool; add a regression test.
- Rename Acquired::exited to exited_on_its_own (field) and record_exit (method) to clarify its meaning and avoid reusing the same name for both.
- Deduplicate the Linux token-leak shell snippet in mock_command tests into a shared constant.
@sylvestre
sylvestre merged commit 2c7ae2e into mozilla:main Nov 26, 2026
51 checks passed
@sylvestre

Copy link
Copy Markdown
Collaborator

Thanks for your PR

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.

Jobserver tokens leak when compiler processes are killed

3 participants