Skip to content

fix(reef): release inbox sockets before shutdown completes - #197717

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-micro-reef-tls-shutdown-close-20260919
Nov 12, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-micro-reef-tls-shutdown-close-20260919

Conversation

@steipete

@steipete steipete commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes Gateway processes remaining alive after shutdown when a Reef relay does not acknowledge WebSocket closure.

User Impact

Stopping Reef or shutting down the Gateway releases the inbox socket without waiting for the relay. In-flight inbox handling and cursor persistence still finish before the account stops. No configuration or migration is required.

Why This Change Was Made

Reef called ws.close() and settled its account task while the TLS socket and WebSocket library's 30-second close-handshake timer remained alive. The transport now terminates abandoned sockets and joins their close event. Generic Gateway shutdown already awaits account teardown, so core production code is unchanged.

Related: #197382.

Evidence

  • The regression starts the real Reef account through the channel manager, then runs the Gateway close handler against a local TLS relay that withholds its close reply. On the base it fails with TCPSocketWrap and Timeout remaining; with the fix the active-resource census returns to its empty baseline before fixture cleanup.
  • All 45 focused tests passed, including in-flight processing, reconciliation, reconnect, and parked-delivery recovery. The final Gateway regression also passed on Node 26.12.1 and pinned Bun, checking real TLS socket destruction and the active-resource census before fixture cleanup.
  • Synthetic owner measurement: retained socket/timer in 12/12 base runs and 0/12 fixed runs. Median settlement cost is 0.346 ms before and 0.616 ms after because the fixed operation includes physical teardown; this is not a throughput claim. Production confirmation remains the shutdown handoff after deployment carrying this change.

Codex autoreview of the final local patch against 83e3641e092125c009d3421e60560fda8af75c86 was scoped-clean through P2, with no accepted/actionable findings. pnpm check:changed completed: own Promise-executor and sorting lint findings were corrected and affected lint/format checks rerun successfully. The only unresolved local failure is seven unused exports in untouched Rerank/TypeSafe files, reproduced on the exact base by lane 2435 and introduced by #197615 (81ec45243fd6); #197679 (c06331c4b8e6) already repairs this main regression. All other checks passed. Final Node replay after the mechanical lint corrections passed (2.383 s body). Production LOC delta is zero. Exact-head CI 43215689570 and openclaw/ci-gate passed. Hosted Codex review found no issues; ClawSweeper reviewed the final head with zero findings and no open items. Red/green terminal excerpts show TLS destruction and the resource census before cleanup.

Terminate the inbox WebSocket and join its close event so a relay that withholds the close reply cannot retain TLS and the ws close timer after account shutdown. Preserve serialized inbox handling and cursor writes. Generic Gateway shutdown already awaits account teardown. Related: #197382.
@clawsweeper

clawsweeper Bot commented Nov 12, 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 channel: reef Channel integration: reef size: S labels Nov 12, 2026
@steipete
steipete marked this pull request as ready for review November 12, 2026 12:11
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor
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-11-12T12:13:51.391597Z 4d78275 Draft marked ready
ℹ️ 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.

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 12, 2026
@clawsweeper

clawsweeper Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR terminates abandoned Reef inbox sockets and waits for their close event before account teardown completes.

Example: The Gateway stops while a Reef relay withholds its WebSocket close reply.

  • Before: The account can finish stopping while its TLS socket and 30-second close timer remain alive.
  • After: The socket is terminated, and teardown joins both socket closure and outstanding inbox processing.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The focused implementation and meaningful transport regression look correct, but inspectable runtime proof is still required.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is not required for maintainer- or bot-authored pull requests.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Stopping Reef or the Gateway can leave its process alive while a relay ignores WebSocket closure.
Reason: The maintainer-authored direction addresses a concrete resource-lifecycle failure with a narrow transport change.

Merge readiness

✅ Ready for maintainer review

Keep open: this maintainer-authored PR makes a focused repair to Reef shutdown, and source review found no actionable correctness defect.

Priority: P2
Reviewed head: 4d78275a8bfafb45102fc26d867b8bb698f20b30

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: Attach redacted terminal output or a transport trace showing the reported base failure and fixed Gateway shutdown against the withholding TLS relay, including socket destruction and the resource census before cleanup.
Agent review details

How this fits together

The Reef inbox transport receives relay messages over WebSocket, serializes delivery and cursor persistence, and exposes an account task that Gateway shutdown awaits.

flowchart TD
  A[Gateway shutdown] --> B[Channel manager]
  B --> C[Reef account lifecycle]
  C --> D[Inbox transport]
  D --> E[Terminate relay socket]
  D --> F[Drain serialized inbox work]
  E --> G[Account completion]
  F --> G
Loading

Technical review

Best possible solution:

Keep teardown in ReefInboxConnection, terminate abandoned sockets, and preserve the existing serialized delivery and cursor-persistence drain.

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

Current main exposes the premature-settlement path, and the committed real-TLS regression targets it directly; this read-only review did not execute the reproduction.

Is this the best way to solve the issue?

The patch repairs the transport owner directly without adding a Gateway workaround, new state owner, configuration, or redundant regression layer.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against edc890bd243d.

Provenance checked

  • Socket closure and account settlement changes intended behavior with a stated reason (882b2fe: Bundled Reef established abort-aware shutdown and guarded settlement; the PR explains why graceful closure still retains TLS and the close-handshake timer.)
  • Serialized inbox shutdown and cursor persistence keeps the original intent (9f8550e: Recovery must not overlap older socket processing or replay entries whose handlers completed during shutdown.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 9f8550e adds extensions/reef/src/transport.ts:399 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 9f8550e8b7dd; files: extensions/reef/src/transport.ts)

PR surface

Source 0, Tests +157, Docs +4. Total +161 across 5 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 1 9 9 0
Tests 3 157 0 +157
Docs 1 4 0 +4
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 5 170 9 +161

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Labels

Label changes:

  • add P2: A nonresponsive Reef relay can retain socket resources after shutdown; this is a focused lifecycle defect with limited blast radius.
  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P2: A nonresponsive Reef relay can retain socket resources after shutdown; this is a focused lifecycle defect with limited blast radius.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed November 12, 2026, 7:28 AM ET / 12:28 UTC.

@steipete

Copy link
Copy Markdown
Contributor Author

ClawSweeper reviewed 4d78275a8bfafb45102fc26d867b8bb698f20b30 with zero findings and no open checklist items. Here is the requested runtime output from the already-completed reproduction.

Command:

OPENCLAW_VITEST_RUNTIME=node node scripts/run-vitest.mjs test/plugins/reef-shutdown.gateway.test.ts --maxWorkers=1

Original transport from 83e3641e092125c009d3421e60560fda8af75c86, with the new regression:

Gateway shutdown releases Reef TLS and timers when the relay withholds its close reply
AssertionError: expected false to be true
  expect.soft(clientSocket?.destroyed).toBe(true);
AssertionError: expected [ 'TCPSocketWrap', 'Timeout' ] to deeply equal []
  expect(resources()).toEqual(before);
Test Files  1 failed (1)
     Tests  1 failed (1)

Fixed transport, final committed source:

✓ |gateway-methods| test/plugins/reef-shutdown.gateway.test.ts > Gateway shutdown releases Reef TLS and timers when the relay withholds its close reply 2383ms
Test Files  1 passed (1)
     Tests  1 passed (1)
[test] passed 1 Vitest shard in 106.79s

The assertions run after the actual Gateway close handler and before fixture cleanup. They verify the real TLS client is destroyed and the TCP/TLS/Timeout resource census returns to its empty pre-start baseline. The local TLS peer withholds its close reply; fixture-owned handles are unreferenced. The final regression also passed pinned Bun.

Exact-head CI run 43215689570 and openclaw/ci-gate passed. Production confirmation remains the shutdown handoff after deployment carrying this fix; no live deployment or restart was performed for this PR.

@steipete
steipete merged commit 8ce4799 into main Nov 12, 2026
224 of 226 checks passed
@steipete
steipete deleted the steipete/perf-micro-reef-tls-shutdown-close-20260919 branch November 12, 2026 12:34
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 13, 2026
…197717)

Terminate the inbox WebSocket and join its close event so a relay that withholds the close reply cannot retain TLS and the ws close timer after account shutdown. Preserve serialized inbox handling and cursor writes. Generic Gateway shutdown already awaits account teardown. Related: openclaw#197382.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: reef Channel integration: reef P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: S 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