Skip to content

Commit df21256

Browse files
fix(js-sdk): only treat the stream cancellation from stop() as a clean end
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 0dd46b9 commit df21256

2 files changed

Lines changed: 29 additions & 11 deletions

File tree

‎packages/js-sdk/src/sandbox/filesystem/watchHandle.ts‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ export interface FilesystemEvent {
7777
* Use {@link WatchHandle.stop} to stop watching the directory.
7878
*/
7979
export class WatchHandle {
80-
/**
81-
* Set by {@link WatchHandle.stop} so the stream abort it triggers is
82-
* treated as a clean, user-initiated end rather than an error.
83-
*/
8480
private stopped = false
8581

8682
constructor(
@@ -96,7 +92,7 @@ export class WatchHandle {
9692
/**
9793
* Stop watching the directory.
9894
*
99-
* Calls `onExit` with no error after the watch stops.
95+
* `onExit` is then called asynchronously, with no error.
10096
*/
10197
async stop() {
10298
this.stopped = true
@@ -113,6 +109,10 @@ export class WatchHandle {
113109
}
114110
}
115111
} catch (err) {
112+
// `stop()` aborts the stream, so a stream error after it is a clean end.
113+
if (this.stopped) {
114+
return
115+
}
116116
throw await handleRpcErrorWithHealthCheck(err, this.checkHealth)
117117
}
118118
}
@@ -139,12 +139,7 @@ export class WatchHandle {
139139
})
140140
}
141141
} catch (err) {
142-
// `stop()` aborts the stream, which surfaces here as an error (a
143-
// Connect cancellation, mapped to `TimeoutError`). A user-initiated
144-
// stop is a clean end, so it must not reach `onExit` as an error.
145-
if (!this.stopped) {
146-
iterationError = err as Error
147-
}
142+
iterationError = err as Error
148143
}
149144

150145
try {

‎packages/js-sdk/tests/sandbox/files/watchHandle.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,27 @@ describe('WatchHandle', () => {
188188
})
189189
expect(exitArgs).toEqual([failure])
190190
})
191+
192+
it('still reports onEvent errors raised after the watch is stopped', async () => {
193+
const failure = new Error('onEvent failed')
194+
let handle: WatchHandle | undefined
195+
let exitArgs: unknown[] | undefined
196+
197+
handle = new WatchHandle(
198+
() => {},
199+
events([filesystemEvent('a.txt')]),
200+
async () => {
201+
await handle?.stop()
202+
throw failure
203+
},
204+
(...args: unknown[]) => {
205+
exitArgs = args
206+
}
207+
)
208+
209+
await vi.waitFor(() => {
210+
expect(exitArgs).toBeDefined()
211+
})
212+
expect(exitArgs).toEqual([failure])
213+
})
191214
})

0 commit comments

Comments
 (0)