Skip to content

fix(worker): Drain leased work on shutdown and let OOM kill commands first - #458

Merged
danny-avila merged 2 commits into
mainfrom
danny-avila/worker-drain
Dec 6, 2026
Merged

danny-avila merged 2 commits into
mainfrom
danny-avila/worker-drain

Conversation

@danny-avila

@danny-avila danny-avila commented Dec 6, 2026 •

Copy link
Copy Markdown
Collaborator

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. settleWithRetry then quarantines a fulfilled mutation without attempting the settle, and a running command is cut off mid-write. The runbook unit's KillMode=control-group also sent SIGTERM to the native executor and every command, so a worker that waited would still lose them, and TimeoutStopSec=35s was 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

Event Before After
First SIGTERM/SIGINT while serving Abort; in-flight mutations quarantined Stop leasing; leased work runs and settles under its normal deadline; exit
Second signal, or the end of the drain n/a Abort exactly as before (fail closed)
Signal during startup or setup Abort Abort (unchanged)
Group SIGTERM/SIGINT reaching the native executor Executor shuts down, commands aborted Ignored while its parent is connected; the parent closes it after draining (IPC disconnect starts the same shutdown)
Kernel OOM Victim chosen by size, worker included Executor raises its oom_score_adj to at least 500 (Linux, best effort, never lowers), inherited by every command, so a runaway command is picked first
Command killed by a signal n/a Ordinary result exitCode: null, signal: "SIGKILL"; settles fulfilled, no quarantine

The drain waits at least 335 s, derived rather than configured: BRIDGE_WORKSPACE_COMMAND_MAX_TIMEOUT_MS (300 s, the longest command a worker accepts) plus BRIDGE_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 a JOB_TIMEOUT above 300 s also finishes. The runbook unit now sets KillMode=mixed, TimeoutStopSec=360s (its comment says it must exceed the drain, so a worker that serves runtime jobs needs JOB_TIMEOUT + 60 s) and OOMPolicy=continue. Existing units don't pick up runbook edits, so docs/remote-bridge/librechat-code-drain.conf is a drop-in that takes effect on daemon-reload without a restart.

SIGTERM
  drain.abort()            # lease long polls end; an assignment already returned is still acknowledged
  executeAndSettle         # unchanged: runs under the hard signal, settles, clears its guard
  run() returns
  controller.abort()       # releases everything else bound to the run, as a stop did
  sandbox.close()          # IPC disconnect, executor shutdown, exit

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; its oom_score_adj is raised on Linux. native-process: the fake child gains disconnect.
  • npx tsc --noEmit and the full packages/code suite on Linux: 1194 pass, 27 skipped, 9 fail (at the current head). The same 9 fail on main on this host: 5 programmatic-execution tests need Bash 5.2+ and jq, and 4 storage tests need a filesystem that enforces chmod.
  • Applied the drop-in to two running user-unit workers; systemctl show reports KillMode=mixed, TimeoutStopUSec=6min and OOMPolicy=continue, with the same main PID.

…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.
@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review (head f10d116)

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 6, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-06T14:48:07.352267Z 6d93aad Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

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".

@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review (pass 2, head f10d116): check the drain path for leased work that can outlive the grace, detached commands at stop, and the executor's signal handling when IPC is already gone.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment on lines +169 to +172
export const WORKER_DRAIN_GRACE_MS =
BRIDGE_WORKSPACE_COMMAND_MAX_TIMEOUT_MS +
BRIDGE_WORKSPACE_COMMAND_SETTLEMENT_RESERVE_MS +
REJECTION_ACK_GRACE_MS;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review (pass 3, head 6d93aad): drain now extends to each leased assignment's own deadline; check the timer re-arm and the deadline bookkeeping on every executeAndSettle exit path.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 6d93aad2e1

ℹ️ 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".

@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review (final pass, head 6d93aad): final review of the shutdown drain, per-assignment drain deadline and OOM score change before merge.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 6d93aad2e1

ℹ️ 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".

@danny-avila
danny-avila merged commit 0dedc0c into main Dec 6, 2026
31 checks passed
@danny-avila
danny-avila deleted the danny-avila/worker-drain branch December 6, 2026 14:53
@danny-avila
danny-avila restored the danny-avila/worker-drain branch December 6, 2026 14:53
@danny-avila
danny-avila deleted the danny-avila/worker-drain branch December 6, 2026 14:54
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.

1 participant