fix: honor context cancel during bridge reconnect backoff - #5
Conversation
Reconnect used time.Sleep(backoff) after connect failure and on reconnect, so SIGINT could not interrupt up to 15s. Replace both sleeps with sleepContext so run returns on ctx cancel. Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
|
🦞👀 Pull request received. I will update this pull request when review starts. |
|
Codex review: needs maintainer review before merge. Reviewed August 29, 2026, 9:59 AM ET / 13:59 UTC. ClawSweeper reviewWhat this changesThis PR makes the headless Clawgo node stop reconnect backoff promptly on SIGINT or SIGTERM and adds cancellation tests for the wait helper. Merge readinessKeep open for routine maintainer merge review: the focused patch fixes two current reconnect waits that ignore the node shutdown context, with no actionable defect found in the introduced code. Priority: P2 Review scores
Verification
How this fits together
flowchart LR
A[Operator starts Clawgo node] --> B[Bridge connection loop]
B --> C{Bridge available?}
C -- no --> D[Cancelable reconnect wait]
D --> B
E[SIGINT or SIGTERM] --> D
C -- yes --> F[Bridge session and chat routing]
Before merge
Agent review detailsSecurityNone. Review metrics
Technical reviewBest possible solution: Merge the narrow context-aware wait change so bridge reconnection retains its existing backoff while shutdown remains prompt. Do we have a high-confidence way to reproduce the issue? Yes, by source: current main has two Is this the best way to solve the issue? Yes. A local context-aware timer is the narrowest maintainable repair and leaves the established exponential-backoff behavior unchanged. AGENTS.md: not found in the target repository. Codex review notes: model internal, reasoning high; reviewed against 5f1b9d90abe2. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (29 earlier review cycles; latest 8 shown)
|
What Problem This Solves
clawgo runreconnects to the gateway bridge with exponential backoff (1s, doubling, capped at 15s). After a connect failure, and again on the reconnect path, the loop calledtime.Sleep(backoff). That sleep cannot be interrupted.The process already installs
signal.NotifyContextfor SIGINT and SIGTERM. The inner select already returns onctx.Done(). The two backoff sleeps did not, sorunstayed stuck until the current sleep finished (up to 15 seconds).Evidence
Live
go runof the old Sleep versus the new helper. Context already canceled. Requested wait 1500ms:Live
clawgo runagainst a closed port. SIGINT sent afterbridge connect failed(during the first reconnect backoff):Canceled helper behavior from
go test ./cmd/clawgo -run TestSleepContext -v(supplemental):Real behavior proof
clawgo runreconnect backoff usedtime.Sleep, so SIGINT could not stop the process until the current 1s-15s sleep finished.fix/reconnect-sleep-context, binary built from./cmd/clawgoto/tmp/clawgo-fixed, down bridge127.0.0.1:1.clawgo run -bridge 127.0.0.1:1 -mdns=false -tts-engine none -chat-subscribe=false. Waited forbridge connect failed. Sent SIGINT and measured time to exit. Also rango run /tmp/sleep-context-demo.goandgo test ./cmd/clawgo -run TestSleepContext -v.context canceledin 0s instead of sleeping 1.503s.ctxis canceled, matching the existingcase <-ctx.Done()path. Backoff math (1s, double, cap 15s) is unchanged.Summary
Call chain:
main->run->runNode-> connect failure orreconnect:label ->time.Sleep(backoff).runNodecreatesctxwithsignal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM). The inner select already handlesctx.Done(). The two Sleep calls did not.This has been present since
f601408(2026-01-04, 223 days).Related work:
modules/audioand the queue, not this reconnect loop. The audio helper landed on main asa86cdbb(sleepWithContext). This PR applies the same idea tocmd/clawgo.