fix: apply strict-mode abort guard to SSE message handlers#362
Open
atilafassina wants to merge 2 commits intomainfrom
Open
fix: apply strict-mode abort guard to SSE message handlers#362atilafassina wants to merge 2 commits intomainfrom
atilafassina wants to merge 2 commits intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR prevents late SSE envelopes from mutating React hook state after the owning AbortController has been aborted (notably under React StrictMode’s double-mount), avoiding transient user-visible errors caused by server “final error” messages emitted during abort-driven cleanup.
Changes:
- Add an
AbortSignal.abortedearly-return guard touseAnalyticsQuery’sonMessagehandler (mirroring the existingonErrorguard). - Add the same abort guard to
useGenieChat’s three SSEonMessagecall sites (fetchConversationPage,sendMessage,pollPendingMessage). - Add a new
useAnalyticsQuerytest suite covering lateerror/result/arrowenvelopes after abort (including ensuring no Arrow fetch occurs), plus basic happy-path and unmount behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/appkit-ui/src/react/hooks/use-analytics-query.ts | Drops SSE messages after abort to prevent post-cleanup state updates/errors under StrictMode. |
| packages/appkit-ui/src/react/hooks/tests/use-analytics-query.test.ts | Adds regression tests ensuring late envelopes are ignored after abort and verifying unmount abort behavior. |
| packages/appkit-ui/src/react/genie/use-genie-chat.ts | Applies the same “ignore messages after abort” guard across Genie SSE message handlers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…ler in useAnalyticsQuery useAnalyticsQuery's `onError` callback bails silently when `abortController.signal.aborted` is true, but `onMessage` had no matching guard. Under React StrictMode's double-mount, the first mount's cleanup aborted controller A, but the server-side SSE writer still emitted a final `event: error` envelope on the already-open stream (cancellation hand-off). That envelope arrived at the hook's onMessage, sailed past the missing guard, hit the `parsed.type === "error"` branch, and surfaced a user-visible error before the second mount's data arrived. Production builds (StrictMode no-op) didn't see it; dev did. Mirror the `onError` guard at the top of `onMessage`. Once the controller is aborted, drop ALL events from that stream uniformly (`result`, `arrow`, `error`) — the next mount/refetch creates a fresh controller and runs cleanly. The `arrow` branch matters specifically because it triggers a side-effectful `ArrowClient.fetchArrow` network call, which the consumer has already given up on. Port of d8a3bc3 from analytics-exp, which applied the same fix to useMetricView. The remaining cross-mount race (cache singleflight propagating mount-1's AbortError to mount-2's awaited promise), server-side AbortError mis-classification as UPSTREAM_ERROR, and the analogous gap in useGenieChat are tracked as follow-up PRs. Tests: new file use-analytics-query.test.ts covering the regression (late `error`, `result`, `arrow` envelopes after abort all dropped), the happy paths (normal `error` and `result` envelopes still set state), URL/payload assertions, and the unmount-aborts-controller contract. 8 tests; 276 appkit-ui tests pass; build, biome, typecheck green. Co-authored-by: Isaac Signed-off-by: Atila Fassina <atila@fassina.eu>
0147624 to
a4b6d9c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
signal.abortedearly-return fromonErrorintoonMessageforuseAnalyticsQuery(use-analytics-query.ts:121) anduseGenieChat's three SSE call sites (sendMessage,pollPendingMessage,fetchConversationPage). Without this, a final SSE error envelope emitted by the server in response to our own cleanup-driven abort lands on the still-attached handler under React StrictMode's double-mount and surfaces a transient user-visible error before the second mount's data arrives.d8a3bc30from theanalytics-expbranch (which applied the same fix touseMetricView); v0.30.0 ships with the gap onmain.sql-warehouse/client.ts+stream-manager.ts), cache singleflight AbortError resilience (cache/index.ts), and auseGenieChatguard for the early-error envelope. Each addresses an independently-mergeable layer; this PR closes the user-visible client-side gap.Test plan
__tests__/use-analytics-query.test.tscovers the regression: lateerror,result, andarrowenvelopes aftersignal.aborted = trueare all dropped (no state mutation, noArrowClient.fetchArrowissued); plus happy paths and the unmount-aborts-controller contract. 8 tests.pnpm test(appkit-ui suite) — 276/276 green.pnpm build && pnpm docs:build— clean.pnpm check:fix && pnpm -r typecheck— clean (1 pre-existing biome warning unrelated to this PR)./analytics, confirm no transient error flash on first paint.This pull request and its description were written by Isaac.