Repository navigation
Conversation
Add a lightweight AbortError classifier that distinguishes restart, user-cancel, timeout and transient abort causes. During a SIGUSR1 restart cycle every in-flight fetch is aborted; without classification these are retried up to 5x with the full session context, causing $10+/day in wasted LLM spend. Changes: - New `abort-classifier.ts` with `classifyAbort()`, `isRestartAbort()`, `isNonRetryableAbort()` helpers - `restart.ts`: add `gatewayRestarting` flag set/cleared by run loop - `run-loop.ts`: set flag on SIGUSR1, clear on new iteration - `retry.ts`: break immediately on non-retryable aborts (both overloads) - `delivery-queue.ts`: move non-retryable aborts straight to failed/ - `chat.ts`: surface user-friendly messages for restart/cancel aborts - 9 Vitest test cases covering all classification paths
| setGatewayRestarting(true); | ||
| markGatewaySigusr1RestartHandled(); | ||
| request("restart", "SIGUSR1"); |
There was a problem hiding this comment.
Race condition: flag set too early, before server shutdown begins
setGatewayRestarting(true) is called here, but the actual server shutdown (which aborts fetch requests) happens later in the async request() function (line 77). Between lines 132-134 and line 77, there's a delay where:
- The flag is
true - But fetch requests haven't been aborted yet
- New requests could start and immediately see
isGatewayRestarting() === true
This means new requests that start after line 132 but before line 77 will be classified as "restart" aborts even though they were never actually aborted by the restart process.
Move setGatewayRestarting(true) to just before server?.close() (around line 77) to ensure the flag is only set when fetch requests are actually being aborted.
| setGatewayRestarting(true); | |
| markGatewaySigusr1RestartHandled(); | |
| request("restart", "SIGUSR1"); | |
| const authorized = consumeGatewaySigusr1RestartAuthorization(); | |
| if (!authorized && !isGatewaySigusr1RestartExternallyAllowed()) { | |
| gatewayLog.warn( | |
| "SIGUSR1 restart ignored (not authorized; enable commands.restart or use gateway tool).", | |
| ); | |
| return; | |
| } | |
| markGatewaySigusr1RestartHandled(); | |
| request("restart", "SIGUSR1"); |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cli/gateway-cli/run-loop.ts
Line: 132:134
Comment:
Race condition: flag set too early, before server shutdown begins
`setGatewayRestarting(true)` is called here, but the actual server shutdown (which aborts fetch requests) happens later in the async `request()` function (line 77). Between lines 132-134 and line 77, there's a delay where:
1. The flag is `true`
2. But fetch requests haven't been aborted yet
3. New requests could start and immediately see `isGatewayRestarting() === true`
This means new requests that start after line 132 but before line 77 will be classified as "restart" aborts even though they were never actually aborted by the restart process.
Move `setGatewayRestarting(true)` to just before `server?.close()` (around line 77) to ensure the flag is only set when fetch requests are actually being aborted.
```suggestion
const authorized = consumeGatewaySigusr1RestartAuthorization();
if (!authorized && !isGatewaySigusr1RestartExternallyAllowed()) {
gatewayLog.warn(
"SIGUSR1 restart ignored (not authorized; enable commands.restart or use gateway tool).",
);
return;
}
markGatewaySigusr1RestartHandled();
request("restart", "SIGUSR1");
```
How can I resolve this? If you propose a fix, please make it concise.
Additional Comments (1)
To fix the race condition, add Prompt To Fix With AIThis is a comment left during a code review.
Path: src/cli/gateway-cli/run-loop.ts
Line: 77:80
Comment:
Missing `setGatewayRestarting(true)` call before aborting connections
To fix the race condition, add `setGatewayRestarting(true)` here, just before calling `server?.close()`. This ensures the flag is only set when the server is actually closing and aborting active fetch requests.
```suggestion
if (isRestart) {
setGatewayRestarting(true);
}
await server?.close({
reason: isRestart ? "gateway restarting" : "gateway stopping",
restartExpectedMs: isRestart ? 1500 : null,
});
```
How can I resolve this? If you propose a fix, please make it concise. |
Move setGatewayRestarting(true) from the SIGUSR1 handler into the request() async function, right before server?.close(). This prevents new requests arriving between flag-set and actual close from being misclassified as restart aborts.
The vi.mock for restart.js was missing the setGatewayRestarting export added in the previous commit, causing Vitest to throw at runtime. Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Thanks for the review — agreed on the race condition. I've moved Let me know if you'd like further tightening. |
|
CI is now fully green (including node and Windows test suites). To summarize the scope clearly:
No new dependencies, no behavior changes outside restart scenarios, and existing retry semantics for transient failures remain intact. Happy to make any small naming or structural adjustments if preferred. |
|
Heads up: this PR focuses specifically on the remaining scope called out in #17589 ("retry/error messaging behavior" after #15026). It should be complementary to the ongoing SIGUSR1 drain/timeout work (e.g., #17445): even with drain hardening, preventing run-level retries on restart/user aborts is still needed to avoid the high-cost re-execution path. CI is green; happy to rebase promptly if any overlap lands first. |
Summary
During a SIGUSR1 gateway restart, all in-flight fetch requests abort. Without classification, every
AbortErroris treated as a transient network failure and retried up to 5x with the full session context (100k+ tokens), causing $10+/day in wasted LLM spend.This PR adds a lightweight abort classifier that distinguishes restart, user-cancel, timeout, and transient abort causes, and wires it into the two retry layers (delivery queue recovery and
retryAsync) so that non-retryable aborts fail immediately.Fixes #17589
Changes
src/infra/abort-classifier.tsclassifyAbort(),isRestartAbort(),isNonRetryableAbort()src/infra/abort-classifier.test.tssrc/infra/restart.tsgatewayRestartingflag with get/setsrc/cli/gateway-cli/run-loop.tsserver?.close(), clear on new iterationsrc/cli/gateway-cli/run-loop.test.tssetGatewayRestartingto restart.js mocksrc/infra/retry.tssrc/infra/outbound/delivery-queue.tsfailed/src/gateway/server-methods/chat.tsTotal: 8 files, ~280 LOC added, 4 LOC changed. No breaking changes. No new dependencies.
Abort Classification
restartisGatewayRestarting())usercause.source === "user")timeoutTimeoutErrorcause)transientHow It Works
classifyAbort()inspects AbortError properties to determine the abort type (restart, user, timeout, or transient).retryAsync()andrecoverPendingDeliveries()).server?.close()insiderequest(), ensuring it only becomestruewhen active connections are actually being aborted. This avoids a race where the flag could be set too early during the drain phase.setGatewayRestarting(false)) to avoid stale restart state after the gateway is back up.chat.tserror handler surfaces "Gateway restarting. Please retry your message." instead of the misleading raw error.This preserves existing retry semantics for transient failures while preventing unintended full-run re-execution during restart windows.
Test Plan
kill -USR1 $(pgrep -f "openclaw gateway")during active chat -- verify 0 retries in logsGreptile Summary
Adds an abort classifier (
classifyAbort) that distinguishes restart, user-cancel, timeout, and transient abort causes, and wires it into both retry layers (retryAsyncandrecoverPendingDeliveries) so that restart- and user-induced aborts fail immediately without costly retries. ThegatewayRestartingflag is set just beforeserver?.close()and cleared at the start of each run-loop iteration, avoiding both the early-flag race and stale-flag problems.src/infra/abort-classifier.ts— New module withclassifyAbort(),isRestartAbort(), andisNonRetryableAbort()plus internal helpers for undici message detection, user-cancel cause inspection, and timeout cause matching.src/infra/restart.ts— AddsgatewayRestartingflag withisGatewayRestarting()/setGatewayRestarting()accessors; flag is included in__testing.resetSigusr1State().src/cli/gateway-cli/run-loop.ts— Sets flag totrueimmediately beforeserver?.close()(post-drain); clears tofalseat the top of each while-loop iteration.src/infra/retry.ts— BothretryAsyncoverloads now break immediately on non-retryable aborts.src/infra/outbound/delivery-queue.ts—recoverPendingDeliveriesmoves non-retryable aborts straight tofailed/instead of incrementing retry count.src/gateway/server-methods/chat.ts— Surfaces user-friendly messages ("Gateway restarting" / "Request cancelled") for restart/user aborts instead of the generic error.src/infra/abort-classifier.test.ts— 13 test cases covering all classification paths.Confidence Score: 4/5
Last reviewed commit: daed892