-
Notifications
You must be signed in to change notification settings - Fork 1
fix: stream auto-reconnect and preserve scene entities during disconnect #823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Devin T. Currie (DTCurrie)
wants to merge
8
commits into
main
Choose a base branch
from
claude/fix-scene-drops-fresh
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7074589
fix: stream auto-reconnect and preserve scene entities during disconnect
github-actions[bot] 75355b0
Merge branch 'main' into claude/fix-scene-drops-fresh
DTCurrie 7120c0e
fix: bump svelte-sdk to 1.2.3, clear stale entities on retry, fix sle…
github-actions[bot] 0edd6d2
Merge branch 'main' into claude/fix-scene-drops-fresh
DTCurrie 77c8f44
Merge branch 'main' into claude/fix-scene-drops-fresh
DTCurrie 6b82207
test: clarify misleading retry-stream test name
github-actions[bot] 2e103b0
Merge branch 'main' into claude/fix-scene-drops-fresh
DTCurrie 4e925fb
test: add MAX_DELAY_MS cap and pre-aborted signal tests for retryStream
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@viamrobotics/motion-tools': patch | ||
| --- | ||
|
|
||
| fix: stream auto-reconnect for draw service and preserve scene entities during disconnect |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import { retryStream } from '../retry-stream' | ||
|
|
||
| describe('retryStream', () => { | ||
| it('calls run and resolves when run succeeds', async () => { | ||
| const run = vi.fn().mockResolvedValue(undefined) | ||
| const controller = new AbortController() | ||
|
|
||
| // run resolves once, retryStream will call it again — abort after first call | ||
| run.mockImplementation(async () => { | ||
| controller.abort() | ||
| }) | ||
|
|
||
| await retryStream(run, controller.signal) | ||
|
|
||
| expect(run).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('retries when run throws', async () => { | ||
| vi.useFakeTimers() | ||
|
|
||
| const controller = new AbortController() | ||
| let callCount = 0 | ||
|
|
||
| const run = vi.fn().mockImplementation(async () => { | ||
| callCount++ | ||
| if (callCount < 3) { | ||
| throw new Error('stream error') | ||
| } | ||
| controller.abort() | ||
| }) | ||
|
|
||
| const promise = retryStream(run, controller.signal) | ||
| // Advance through the backoff delays | ||
| await vi.advanceTimersByTimeAsync(1_000) | ||
| await vi.advanceTimersByTimeAsync(2_000) | ||
|
|
||
| await promise | ||
|
|
||
| expect(run).toHaveBeenCalledTimes(3) | ||
|
|
||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| it('stops retrying when signal is aborted', async () => { | ||
| vi.useFakeTimers() | ||
|
|
||
| const controller = new AbortController() | ||
| const run = vi.fn().mockRejectedValue(new Error('stream error')) | ||
| const onRetry = vi.fn() | ||
|
|
||
| const promise = retryStream(run, controller.signal, onRetry) | ||
|
|
||
| // First call fails immediately, then waits for backoff | ||
| await vi.advanceTimersByTimeAsync(0) | ||
| expect(run).toHaveBeenCalledTimes(1) | ||
|
|
||
| // Abort during backoff wait | ||
| controller.abort() | ||
| await vi.advanceTimersByTimeAsync(1_000) | ||
|
|
||
| await promise | ||
|
|
||
| // Should have called onRetry once, but not retried run | ||
| expect(onRetry).toHaveBeenCalledTimes(1) | ||
|
DTCurrie marked this conversation as resolved.
|
||
| expect(run).toHaveBeenCalledTimes(1) | ||
|
|
||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| it('calls onRetry with the current delay', async () => { | ||
| vi.useFakeTimers() | ||
|
|
||
| const controller = new AbortController() | ||
| let callCount = 0 | ||
|
|
||
| const run = vi.fn().mockImplementation(async () => { | ||
| callCount++ | ||
| if (callCount < 3) { | ||
| throw new Error('stream error') | ||
| } | ||
| controller.abort() | ||
| }) | ||
|
|
||
| const onRetry = vi.fn() | ||
| const promise = retryStream(run, controller.signal, onRetry) | ||
|
|
||
| await vi.advanceTimersByTimeAsync(1_000) | ||
| await vi.advanceTimersByTimeAsync(2_000) | ||
|
|
||
| await promise | ||
|
|
||
| expect(onRetry).toHaveBeenCalledTimes(2) | ||
| expect(onRetry).toHaveBeenNthCalledWith(1, 1_000) | ||
| expect(onRetry).toHaveBeenNthCalledWith(2, 2_000) | ||
|
|
||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| it('does not call onRetry and restarts immediately on clean stream end', async () => { | ||
| const controller = new AbortController() | ||
| let callCount = 0 | ||
|
|
||
| const run = vi.fn().mockImplementation(async () => { | ||
| callCount++ | ||
| if (callCount === 1) return // clean end — server closed the stream | ||
| controller.abort() | ||
| }) | ||
|
|
||
| const onRetry = vi.fn() | ||
| await retryStream(run, controller.signal, onRetry) | ||
|
|
||
| expect(run).toHaveBeenCalledTimes(2) | ||
| expect(onRetry).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('resets delay after a successful run', async () => { | ||
| vi.useFakeTimers() | ||
|
|
||
| const controller = new AbortController() | ||
| let callCount = 0 | ||
|
|
||
| const run = vi.fn().mockImplementation(async () => { | ||
| callCount++ | ||
| // First call: fail | ||
| if (callCount === 1) throw new Error('fail') | ||
| // Second call: succeed (stream ended cleanly) | ||
| if (callCount === 2) return | ||
| // Third call: fail | ||
| if (callCount === 3) throw new Error('fail') | ||
| // Fourth call: abort | ||
| controller.abort() | ||
| }) | ||
|
|
||
| const onRetry = vi.fn() | ||
| const promise = retryStream(run, controller.signal, onRetry) | ||
|
|
||
| // First failure + 1s backoff | ||
| await vi.advanceTimersByTimeAsync(1_000) | ||
| // Second call succeeds, delay resets. Third call fails, should use 1s again | ||
| await vi.advanceTimersByTimeAsync(1_000) | ||
| // Fourth call - abort | ||
| await vi.advanceTimersByTimeAsync(2_000) | ||
|
|
||
| await promise | ||
|
|
||
| // Both retries should have used 1000ms (reset after success) | ||
| expect(onRetry).toHaveBeenNthCalledWith(1, 1_000) | ||
| expect(onRetry).toHaveBeenNthCalledWith(2, 1_000) | ||
|
|
||
| vi.useRealTimers() | ||
| }) | ||
| }) | ||
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| const INITIAL_DELAY_MS = 1_000 | ||
| const MAX_DELAY_MS = 30_000 | ||
|
|
||
| /** | ||
| * Calls `run` in a loop, retrying with exponential backoff when it throws. | ||
| * - Clean stream end (server closed it): restarts immediately, delay resets. | ||
| * - Error: calls `onRetry`, waits with exponential backoff, then retries. | ||
| * Stops when the signal is aborted. | ||
| */ | ||
| export const retryStream = async ( | ||
| run: (signal: AbortSignal) => Promise<void>, | ||
| signal: AbortSignal, | ||
| onRetry?: (delay: number) => void | ||
| ): Promise<void> => { | ||
| let delay = INITIAL_DELAY_MS | ||
|
|
||
| while (!signal.aborted) { | ||
| let errored = false | ||
| try { | ||
| await run(signal) | ||
| // Stream ended cleanly (server closed it) — restart immediately. | ||
| delay = INITIAL_DELAY_MS | ||
| } catch (error) { | ||
| if (signal.aborted) return | ||
| errored = true | ||
| console.warn('Stream error, retrying in', delay, 'ms:', error) | ||
| } | ||
|
|
||
| if (signal.aborted) return | ||
|
|
||
| if (errored) { | ||
| onRetry?.(delay) | ||
| await sleep(delay, signal) | ||
| delay = Math.min(delay * 2, MAX_DELAY_MS) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const sleep = (ms: number, signal: AbortSignal): Promise<void> => { | ||
| return new Promise((resolve) => { | ||
| const timer = setTimeout(resolve, ms) | ||
|
claude[bot] marked this conversation as resolved.
Outdated
|
||
| signal.addEventListener( | ||
| 'abort', | ||
| () => { | ||
| clearTimeout(timer) | ||
| resolve() | ||
| }, | ||
| { once: true } | ||
| ) | ||
| }) | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing test: delay cap at
MAX_DELAY_MS(30 s) is untested.The exponential doubling is exercised up to 2 s but never hits the 30 s ceiling. Worth one parameterized test that runs enough failures to saturate the cap and verifies
onRetrystops receiving larger values.Also missing: a test for what happens when
signalis already aborted beforeretryStreamis called (thewhile (!signal.aborted)check on the first iteration). The current suite only aborts during an in-flight operation or backoff.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Claude (@claude) add these tests
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Claude finished Devin T. Currie (@DTCurrie)'s task in 4m 12s —— View job
Both missing tests have been added in commit
4e925fb:caps delay at MAX_DELAY_MS (30s) and does not double beyond it— runs 6 consecutive failures to drive the backoff through 1s → 2s → 4s → 8s → 16s → 30s (capped), then asserts the 6thonRetrycall received30_000rather than32_000.does not call run when signal is already aborted before retryStream is called— pre-aborts the controller before passing the signal, assertsrunis never invoked (tests thewhile (!signal.aborted)guard on first entry).· Branch