Skip to content

fix(macos): bound websocket ping so a dropped pong cannot orphan its continuation - #115362

Merged
steipete merged 2 commits into
mainfrom
fix/macos-sendping-continuation-leak
Jul 28, 2026
Merged

steipete merged 2 commits into
mainfrom
fix/macos-sendping-continuation-leak

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Every OpenClaw Mac node was logging this continuously:

SWIFT TASK CONTINUATION MISUSE: sendPing() leaked its continuation without resuming it.
This may cause tasks waiting on it to remain suspended forever.

URLSessionWebSocketTask.sendPing(pongReceiveHandler:) drops its handler entirely when the task is cancelled or closed mid-flight — the callback simply never fires. WebSocketTaskBox.sendPing() wrapped that callback in withCheckedThrowingContinuation, and while WebSocketPingContinuationGate guarded the handler firing twice, nothing guarded it firing zero times.

The consequence is worse than a log line. GatewayChannel.keepaliveLoop does:

try await task.sendPing()

An orphaned continuation means that await never returns, so the keepalive loop is wedged for the lifetime of the process. Observed live on a production node.

Why This Change Was Made

Race the ping against a deadline and let the existing gate arbitrate, so the continuation always resumes exactly once.

The subtle part is cancellation. A first attempt used try? await Task.sleep(...), which swallows the CancellationError — so when a delivered pong cancelled the deadline, the deadline task fell straight through into the timeout resume and raced the callback. A healthy ping could then be reported as .timedOut. Codex autoreview caught this (P1, 0.99); the deadline now returns without resuming when cancelled.

User Impact

Mac nodes keep their gateway keepalive alive instead of silently wedging after the first dropped pong. No API change: sendPing(timeout:) defaults to WebSocketTaskBox.pingTimeout (10s), so existing call sites are unchanged.

Evidence

Red/green on the new timeout test, run on macOS 27 / Xcode 27.0 (27A5228h), Swift 6.4:

  • Red — with the deadline removed, websocket ping times out when no pong callback ever arrives hangs; killed at 150s (exit 124).
  • Green — with the fix, the same test passes in 0.054s.

Full filtered suite, swift test --filter "websocket ping" → 4/4 passed:

✔ websocket ping times out when no pong callback ever arrives (0.054s)
✔ websocket ping succeeds when the pong beats the deadline (0.526s)
✔ websocket ping ignores duplicate success callbacks (0.001s)
✔ websocket ping ignores duplicate callbacks after first error (0.001s)

The race test drives 20 sequential pings whose pong lands 20ms before a 5s deadline — it fails if a cancelled deadline can win the gate.

  • scripts/format-swift.sh macos → 0/26 files require formatting
  • scripts/lint-swift.sh macos --strict → 0 violations, 0 serious in 22 files
  • Codex autoreview (gpt-5.6-sol, xhigh) → clean, "patch is correct" (0.9)

…continuation

URLSession drops the pong handler entirely when a web socket task is cancelled or
closed mid-flight. The existing gate guarded a double resume but nothing guarded
zero resumes, so the checked continuation leaked, Swift logged CONTINUATION
MISUSE, and keepaliveLoop's await never returned - wedging the keepalive for the
lifetime of the process.

Race the ping against a deadline and reuse the gate so exactly one resume wins.
Cancellation of the deadline returns without resuming, otherwise a delivered pong
would race the gate and surface as a spurious timeout.
@openclaw-barnacle openclaw-barnacle Bot added size: S maintainer Maintainer-authored PR labels Jul 28, 2026
@steipete
steipete marked this pull request as ready for review July 28, 2026 18:18
Periphery flags a public enum referenced only inside its own module as dead
code in the shared OpenClawKit intersection scan. URLError keeps the timeout
indistinguishable from a transport timeout for callers, which already handle
URLSession errors from every other path, and adds no public surface.
@steipete
steipete merged commit 773d60d into main Jul 28, 2026
54 checks passed
@steipete
steipete deleted the fix/macos-sendping-continuation-leak branch July 28, 2026 18:58
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

vincentkoc added a commit to christian-lallo/openclaw that referenced this pull request Jul 28, 2026
* origin/main: (61 commits)
  perf(ui): debounce event-driven session list refreshes and right-size child paging (openclaw#115374)
  feat(talk): support GPT-Live realtime voice over ChatGPT OAuth (openclaw#115226)
  feat(system-agent): guide gateway configuration in Ask OpenClaw (openclaw#115363)
  fix(agents): make overflow recovery append-only via branch-and-reappend (openclaw#115271) (openclaw#115310)
  fix(macos): bound websocket ping so a dropped pong cannot orphan its continuation (openclaw#115362)
  fix(codex): isolate native hook relay startup (openclaw#115078)
  fix(ci): stop reviving superseded pull request workflows (openclaw#115378)
  feat(openshell): add workspace selection (openclaw#114952)
  fix(skills): persist workshop apply reason; correct Skill Workshop docs (openclaw#114471)
  fix: agent stops replying when a session transcript has no header row (openclaw#115080)
  perf(usage): stale-while-revalidate caches for usage.status and sessions.usage (openclaw#115369)
  fix(discord): reject invalid UTF-8 probe responses (openclaw#111734)
  feat(gateway): add memory.search operator method (openclaw#115346)
  fix(android): keep welcome action reachable with large text (openclaw#111747)
  fix(gateway): validate doctor memory agent ids (openclaw#115365)
  refactor: harden shell completion and shard Control UI E2E (openclaw#115332)
  fix(ui): anchor the thread branches menu to its trigger button (openclaw#115352)
  perf(sessions): incremental snapshot revalidation and shallow list projections (openclaw#115359)
  refactor(sessions): consolidate entry state (openclaw#115299)
  feat(ui): swap three plain Lobsterdex palettes for balloon, ascii, and portal (openclaw#115308)
  ...
vincentkoc added a commit that referenced this pull request Jul 28, 2026
…etwork-none

* origin/main: (74 commits)
  fix(ui): make Control UI ownership and permissions reliable (#115364)
  feat(system-agent): host memory import in Ask OpenClaw (#115376)
  fix(chat): preserve literal Markdown image examples (#113904)
  refactor(chat): keep web and terminal sessions synchronized (#115377)
  fix(ui): order model picker by provider strength (#115380)
  feat(ui): make Memory settings a destination page with live overview and dreams (#115383)
  docs(security): fix warning line wrap (#115388)
  fix(memory-core): avoid cubic MMR similarity rescans (#113359)
  fix(discord): preserve code-leading reasoning chunks (#103166)
  fix(slack): protect outbound hooks from previews
  fix(ui): keep explicit navigation ahead of chat canonicalization (#115366)
  fix(exec): safely cancel pending node approvals (#115373)
  fix: make Code Mode reliable across gateway and provider lifecycles (#115379)
  perf(ui): debounce event-driven session list refreshes and right-size child paging (#115374)
  feat(talk): support GPT-Live realtime voice over ChatGPT OAuth (#115226)
  feat(system-agent): guide gateway configuration in Ask OpenClaw (#115363)
  fix(agents): make overflow recovery append-only via branch-and-reappend (#115271) (#115310)
  fix(macos): bound websocket ping so a dropped pong cannot orphan its continuation (#115362)
  fix(codex): isolate native hook relay startup (#115078)
  fix(ci): stop reviving superseded pull request workflows (#115378)
  ...

# Conflicts:
#	CHANGELOG.md
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 29, 2026
…continuation (openclaw#115362)

* fix(macos): bound websocket ping so a dropped pong cannot orphan its continuation

URLSession drops the pong handler entirely when a web socket task is cancelled or
closed mid-flight. The existing gate guarded a double resume but nothing guarded
zero resumes, so the checked continuation leaked, Swift logged CONTINUATION
MISUSE, and keepaliveLoop's await never returned - wedging the keepalive for the
lifetime of the process.

Race the ping against a deadline and reuse the gate so exactly one resume wins.
Cancellation of the deadline returns without resuming, otherwise a delivered pong
would race the gate and surface as a spurious timeout.

* fix(macos): reuse URLError(.timedOut) instead of a new public ping error

Periphery flags a public enum referenced only inside its own module as dead
code in the shared OpenClawKit intersection scan. URLError keeps the timeout
indistinguishable from a transport timeout for callers, which already handle
URLSession errors from every other path, and adds no public surface.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant