Skip to content

fix(gateway): prevent queued startup work after shutdown begins - #139092

Merged
steipete merged 1 commit into
mainfrom
codex/perf-sep05-post-ready-producer-fence
Sep 5, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/perf-sep05-post-ready-producer-fence

Conversation

@steipete

@steipete steipete commented Sep 5, 2026

Copy link
Copy Markdown
Contributor
Additional instructions

MUST: Keep Allow edits from maintainers enabled for this PR so maintainers
can help update the branch when needed.

What Problem This Solves

Resolves a problem where stopping or restarting a newly started Gateway could still start queued background work or emit avoidable startup-failure warnings after local shutdown began, while existing requests were finishing. A callback paused for admission, a module import, or ACP backend readiness could resume into the closing Gateway.

Why This Change Was Made

The post-ready producer owner now carries the Gateway's existing live closing predicate through scheduling, admitted callback entry, and asynchronous import/catalog boundaries, preventing unstarted producers from entering after close wins. Cleanup remains with its existing lifecycle owner after received work drains; already-entered work stays owned, and real errors from admitted callbacks remain visible.

User Impact

Queued Gmail watcher/model validation, transcript auto-start, internal startup hooks, restart wake, and Control UI preparation respect local Gateway closing state. ACP identity reconciliation does not begin when close wins during admission, backend readiness, or manager loading; a scan that has already entered retains its existing completion behavior.

There are no config, protocol, database, timeout, or teardown-order changes. This does not change the earlier process-manager restart-drain-only interval or promise to cancel every operation already in progress.

Evidence

  • Controlled failures before the repair: the original 111-case owner run had 107 passes and four expected failures: queued producers during direct close; queued warnings during restart drain after local close; a Control UI callback parked behind suspension; and Gmail startup resuming from an import after close prelude. The intermediate producer repair passed all 111 owner cases and 83 sibling cases.
  • ACP boundary proof: the added cases against that intermediate owner produced three expected failures—suspended admission, backend readiness, and manager import. The already-entered reconciliation control passed. The final candidate passed the identical 115 owner cases and 87 cases across nine whole sibling files, preserving the earlier 83 sibling identities and per-file order. Original failures remain retained.
  • Normal validation: all 29 selected changed-gate stages and the normal mapped build passed. Build evidence retains dependency-eval, worker SOURCEMAP_BROKEN, and browser node:path warnings; these are not described as warning-free results. Managed Codex review returned seven scoped passes with no actionable P0–P2 findings; the reviewer did not execute project code.
  • Ordinary built smoke: one real built Gateway completed readiness, then health({probe:false}), status, tts.providers, and plugins.list in that order, followed by normal SIGTERM shutdown. Full decoded results and initial refusal/503/pre-ready-timeout observations were retained. The smoke recorded 22 INFO messages without warnings/errors; root verified all eight observed PIDs, four groups, and the listener closed. It does not reproduce the controlled close races or establish real-provider/ACP/channel race behavior. The zero-session status was a point-in-time observation before later background bindings, and none of the recorded durations is a latency comparison.

Production is +37/−5, net +32 lines; tests are +321/−50, net +271 lines. The production growth propagates an existing lifecycle fact into previously unguarded producer boundaries, including ACP's distinct dispatch path, without adding a new cancellation framework. The immediate scheduler also removes its duplicate stopped flag; timer error handling preserves the distinction between actual stop and an admitted callback failing during close.

Candidate commit cb4ccc8d50a887df0201860d38e2f67c27ac11f6 preserves the exact reviewed and tested patch against base 17b0c435dd5b341d645703c04354706a1c7f9a55. A source-only comparison with current main 4cb260cd132f1315fccecc8c9dcb765d3ccd1f28 found both touched baseline files unchanged and all 53 selected contracts identical to the context supplied to review. The eight accounted-for context changes include host lifecycle types, prepared session facts, ACP initialization cleanup, transcript retry identity, and update-notice ownership. This is not current-main runtime or CI proof. Full source, failures, closures, gate/build logs, and built-smoke readback remain under the retained post-ready-producer-fence evidence family.

@steipete steipete self-assigned this Sep 5, 2026
@clawsweeper

clawsweeper Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added the gateway Gateway runtime label Sep 5, 2026
@steipete
steipete marked this pull request as ready for review September 5, 2026 13:01
@openclaw-barnacle openclaw-barnacle Bot added size: M maintainer Maintainer-authored PR labels Sep 5, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Sep 5, 2026
@clawsweeper

clawsweeper Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 5, 2026, 9:09 AM ET / 13:09 UTC.

ClawSweeper review

What this changes

Prevents queued Gateway startup tasks from starting after local shutdown begins, while preserving cleanup ordering and errors from already-admitted callbacks.

Merge readiness

✅ Ready for maintainer review

The repair remains necessary on fetched main. No blocking correctness or security defect was found in the introduced patch.

Priority: P2
Reviewed head: cb4ccc8d50a887df0201860d38e2f67c27ac11f6

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-boundary repair with meaningful regression coverage and no blocking finding; reported execution results were not independently rerun.
Proof confidence 🌊 off-meta tidepool Not applicable: The supplied MEMBER association exempts this internal lifecycle repair from contributor proof requirements. Reported owner tests exercise the changed races; the built Gateway smoke covers ordinary readiness and shutdown only.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The supplied MEMBER association exempts this internal lifecycle repair from contributor proof requirements. Reported owner tests exercise the changed races; the built Gateway smoke covers ordinary readiness and shutdown only.
Evidence reviewed 9 items Verified introduced scope: The complete local merge-base comparison contains only the startup owner and its tests. The verified test merge has the pinned base followed by the exact PR head as parents and changes only those same files.
Current-main defect remains: Fetched main still checks only the sidecar's stopped flag and does not recheck eligibility inside resumed admission. Both touched files are unchanged between the merge base and fetched main.
Authoritative closing state and cleanup ordering: The lifecycle synchronously records closing and begins connection-work closure. Public server shutdown releases the post-ready barrier, drains received work, then stops general sidecars; stopping those sidecars earlier would violate the existing dependency lifetime.
Findings None None.
Security None None.

How this fits together

After the Gateway attaches its server, startup scheduling launches background services and preparation tasks. Shutdown must retire queued producers while keeping dependencies available until received work finishes.

flowchart TD
  A[Gateway startup] --> B[Queued background tasks]
  B --> C[Admission and readiness waits]
  C --> D{Gateway closing?}
  D -->|No| E[Start background work]
  D -->|Yes| F[Retire queued task]
  G[Received requests finish] --> H[Clean up services]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +32, Tests +271. Total +303 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 37 5 +32
Tests 1 321 50 +271
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 358 55 +303

Review metrics

Metric Value Why it matters
Production versus test growth Production net +32 lines; tests net +271 lines Production growth propagates an existing lifecycle fact across distinct asynchronous boundaries and removes the immediate scheduler's duplicate stopped flag.

Technical review

Best possible solution:

Retain the shared scheduler checks and ACP's separate dispatch checks; these additional await-boundary checks preserve cleanup ownership without introducing another cancellation framework.

Do we have a high-confidence way to reproduce the issue?

Yes, from source: close the Gateway while queued startup work awaits readiness, suspension, or an import and received work delays sidecar cleanup. Fetched main lacks the necessary closing checks; this review did not execute the reproduction.

Is this the best way to solve the issue?

Yes, for the stated local-shutdown boundary: checking producer eligibility preserves the existing drain-before-cleanup contract. Moving teardown earlier or cancelling already-admitted operations would change that contract.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against f09c139f3c62.

Labels

Label changes:

  • add P2: Repairs a bounded startup/shutdown race without evidence of widespread outages or a blocked first-run workflow.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The supplied MEMBER association exempts this internal lifecycle repair from contributor proof requirements. Reported owner tests exercise the changed races; the built Gateway smoke covers ordinary readiness and shutdown only.

Label justifications:

  • P2: Repairs a bounded startup/shutdown race without evidence of widespread outages or a blocked first-run workflow.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The supplied MEMBER association exempts this internal lifecycle repair from contributor proof requirements. Reported owner tests exercise the changed races; the built Gateway smoke covers ordinary readiness and shutdown only.

Evidence

What I checked:

  • Verified introduced scope: The complete local merge-base comparison contains only the startup owner and its tests. The verified test merge has the pinned base followed by the exact PR head as parents and changes only those same files. (src/gateway/server-startup-post-attach.ts:262, cb4ccc8d50a8)
  • Current-main defect remains: Fetched main still checks only the sidecar's stopped flag and does not recheck eligibility inside resumed admission. Both touched files are unchanged between the merge base and fetched main. (src/gateway/server-startup-post-attach.ts:264, f09c139f3c62)
  • Authoritative closing state and cleanup ordering: The lifecycle synchronously records closing and begins connection-work closure. Public server shutdown releases the post-ready barrier, drains received work, then stops general sidecars; stopping those sidecars earlier would violate the existing dependency lifetime. (src/gateway/server-lifecycle.ts:397, cb4ccc8d50a8)
  • Admission implementation boundary: OpenClaw's own admission helper waits through suspension and retains admitted work through completion. The changed scheduling behavior depends on this local implementation, not the Codex runtime or protocol. (src/process/gateway-work-admission.ts:405, cb4ccc8d50a8)
  • Producer repair and ACP boundary: Shared immediate and timer schedulers consume the existing closing predicate. ACP separately checks before readiness work, after readiness, and after manager imports; already-entered reconciliation retains its existing completion behavior. (src/gateway/server-startup-post-attach.ts:866, cb4ccc8d50a8)
  • Regression and sibling coverage inspected: Added cases cover direct close, restart drain, suspended Control UI admission, delayed Gmail imports, admitted hook errors, and four ACP boundaries. Existing connection-work and idle-task tests preserve received-work completion and cleanup joins. Tests were inspected, not executed. (src/gateway/server-startup-post-attach.test.ts:3525, cb4ccc8d50a8)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@steipete
steipete merged commit 426ca0d into main Sep 5, 2026
246 checks passed
@steipete
steipete deleted the codex/perf-sep05-post-ready-producer-fence branch September 5, 2026 16:43
@steipete

steipete commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

vincentkoc added a commit to aniruddhaadak80/openclaw that referenced this pull request Sep 5, 2026
* commit '3e4becea5e2ce38ede69e8ecf5302805ecbfc09c':
  refactor: simplify outbound message execution context (openclaw#139107)
  fix(models): keep ordinary chat selections session-scoped (openclaw#139232)
  docs: slim root agent instructions and clarify testing scope (openclaw#139236)
  test(agents): give the sibling-drain completion wait a tolerance on slow runners (openclaw#139176)
  refactor(workboard): use canonical queries for card-tree writes (openclaw#139106)
  fix(canvas): recognize versioned macOS nodes (openclaw#138958)
  fix: load GitHub connections without a default agent (openclaw#139222)
  fix(memory): explain unsafe legacy host-event path recovery (openclaw#139195)
  refactor(talk): centralize typed voice session queries (openclaw#139103)
  fix(cli): preserve command dispatch and structured errors (openclaw#139067)
  refactor(agents): consolidate reply failure summaries (openclaw#139175)
  fix(auth): keep an unset default model unchanged during login (openclaw#139218)
  perf(media): reuse validated attachment read handles (openclaw#139168)
  refactor(channels): simplify account configuration updates (openclaw#139096)
  refactor(android): remove unused updater export root (openclaw#139198)
  fix(gateway): stop admitting post-ready producers during close (openclaw#139092)
  fix(gateway): expose agent hook completion outcomes (openclaw#139155)
  fix(config): return the saved configuration after plugin install commits (openclaw#139203)
  fix(android): reduce repeated retries to unreachable gateways (openclaw#138732)
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 6, 2026
necromman pushed a commit to necromman/openclaw that referenced this pull request Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime maintainer Maintainer-authored PR P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant