Repository navigation
fix(worker): Drain leased work on shutdown and let OOM kill commands first - #458
Conversation
…first The first SIGTERM or SIGINT stops leasing and lets leased work run and settle; a second signal or the 335 s grace aborts as before. The native executor leaves group signals to its draining parent, and raises its OOM score so the kernel picks a runaway command before the worker. The runbook unit and a drop-in for existing units set KillMode=mixed, TimeoutStopSec=360s and OOMPolicy=continue.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f10d116d90
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| export const WORKER_DRAIN_GRACE_MS = | ||
| BRIDGE_WORKSPACE_COMMAND_MAX_TIMEOUT_MS + | ||
| BRIDGE_WORKSPACE_COMMAND_SETTLEMENT_RESERVE_MS + | ||
| REJECTION_ACK_GRACE_MS; |
There was a problem hiding this comment.
Account for non-command assignment budgets in the drain grace
When the service uses a JOB_TIMEOUT greater than 335 seconds, leased runtime or programmatic work can legitimately outlive this grace: service/src/service/programmatic-router.ts derives its deadline from env.JOB_TIMEOUT, and service/src/bridge/store.test.ts explicitly covers a 15-minute runtime assignment. Those assignments are not bounded by BRIDGE_WORKSPACE_COMMAND_MAX_TIMEOUT_MS, but the CLI unconditionally aborts their execution signal after this command-derived grace. Consequently, a normal first-signal shutdown still interrupts valid leased work and can quarantine its workspace despite the claimed finish-and-settle drain. Account for the remaining budgets of all leased assignment kinds, or explicitly constrain admission to the supported drain budget.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 6d93aad: the worker records each leased assignment's own deadline, and a drain waits until every one plus the 30 s settle window has passed (at least the 335 s workspace-tool floor), so runtime and programmatic jobs under a longer JOB_TIMEOUT finish. The runbook now says TimeoutStopSec must exceed JOB_TIMEOUT + 60 s for workers that serve them. Test: worker-receipts drain deadline case.
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
An interrupted workspace mutation quarantines its workspace until an operator clears it by hand. On a production BYOM worker, every quarantine measured over two months followed one of two events: a service stop while commands were running (38% of rejections), or a kernel OOM kill (62%). Both can be avoided without protocol changes.
Stop. The CLI aborted the run on the first SIGTERM/SIGINT.
settleWithRetrythen quarantines a fulfilled mutation without attempting the settle, and a running command is cut off mid-write. The runbook unit'sKillMode=control-groupalso sent SIGTERM to the native executor and every command, so a worker that waited would still lose them, andTimeoutStopSec=35swas shorter than one command.OOM. With systemd's default
OOMPolicy=stop, one OOM kill anywhere in the unit stops the whole service and quarantines every in-flight mutation on the other lanes. Nothing steered the kernel towards the command that grew.After this change
oom_score_adjto at least 500 (Linux, best effort, never lowers), inherited by every command, so a runaway command is picked firstexitCode: null, signal: "SIGKILL"; settles fulfilled, no quarantineThe drain waits at least 335 s, derived rather than configured:
BRIDGE_WORKSPACE_COMMAND_MAX_TIMEOUT_MS(300 s, the longest command a worker accepts) plusBRIDGE_WORKSPACE_COMMAND_SETTLEMENT_RESERVE_MS(5 s) plus the 30 s retry window for an acknowledged rejection. The worker records each executing assignment's own deadline, and once that floor passes the CLI re-arms its timer until every leased deadline plus the 30 s settle window has passed, so a runtime or programmatic job under aJOB_TIMEOUTabove 300 s also finishes. The runbook unit now setsKillMode=mixed,TimeoutStopSec=360s(its comment says it must exceed the drain, so a worker that serves runtime jobs needsJOB_TIMEOUT+ 60 s) andOOMPolicy=continue. Existing units don't pick up runbook edits, sodocs/remote-bridge/librechat-code-drain.confis a drop-in that takes effect ondaemon-reloadwithout a restart.The bridge has no worker drain message, and none is added. A draining worker keeps heartbeating while its leased work settles and simply stops polling, so the protocol and Code API are unchanged. New requests for that worker wait in its queue exactly as they do for a busy worker. Detached commands are not awaited; as before, an interrupted one fences only itself. A stop that reaches the end of the drain or a second signal behaves exactly as today, and the settle path is untouched.
Tests
worker-receipts: on one slot and on lane slots, a drain lets a leased command settle fulfilled with no quarantine and no lease after the drain. A signal-killed command settles fulfilled. A stop during a drain still quarantines the interrupted command. While a drained command runs,drainRemainingMs()covers its deadline plus the settle window, and it is 0 once the run returns.native-sandbox: a real bash process killed with SIGKILL returns an ordinary signalled result with its partial output.native-process-child: a connected executor keeps serving after SIGINT/SIGTERM and exits 0 on disconnect; SIGHUP still shuts it down; itsoom_score_adjis raised on Linux.native-process: the fake child gainsdisconnect.npx tsc --noEmitand the fullpackages/codesuite on Linux: 1194 pass, 27 skipped, 9 fail (at the current head). The same 9 fail onmainon this host: 5 programmatic-execution tests need Bash 5.2+ and jq, and 4 storage tests need a filesystem that enforces chmod.systemctl showreportsKillMode=mixed,TimeoutStopUSec=6minandOOMPolicy=continue, with the same main PID.