Skip to content

Commit 5d75b45

Browse files
Merge df21256 into 3ba1d05
2 parents 3ba1d05 + df21256 commit 5d75b45

4 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'e2b': patch
3+
---
4+
5+
fix(js-sdk): treat `WatchHandle.stop()` as a clean watch end - `onExit` now fires with no error instead of a `TimeoutError` blaming `requestTimeoutMs`, matching the Python SDK behavior.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ export interface WatchOpts extends FilesystemRequestOpts {
339339
timeoutMs?: number
340340
/**
341341
* Callback to call when the watch operation stops.
342+
*
343+
* `err` is `undefined` after {@link WatchHandle.stop} or a clean stream end,
344+
* and is set only when the watch fails.
342345
*/
343346
onExit?: (err?: Error) => void | Promise<void>
344347
/**

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export interface FilesystemEvent {
7777
* Use {@link WatchHandle.stop} to stop watching the directory.
7878
*/
7979
export class WatchHandle {
80+
private stopped = false
81+
8082
constructor(
8183
private readonly handleStop: () => void,
8284
private readonly events: AsyncIterable<WatchDirResponse>,
@@ -89,8 +91,11 @@ export class WatchHandle {
8991

9092
/**
9193
* Stop watching the directory.
94+
*
95+
* `onExit` is then called asynchronously, with no error.
9296
*/
9397
async stop() {
98+
this.stopped = true
9499
this.handleStop()
95100
}
96101

@@ -104,6 +109,10 @@ export class WatchHandle {
104109
}
105110
}
106111
} catch (err) {
112+
// `stop()` aborts the stream, so a stream error after it is a clean end.
113+
if (this.stopped) {
114+
return
115+
}
107116
throw await handleRpcErrorWithHealthCheck(err, this.checkHealth)
108117
}
109118
}

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Code, ConnectError } from '@connectrpc/connect'
12
import { describe, expect, it, vi } from 'vitest'
23

34
import { EventType } from '../../../src/envd/filesystem/filesystem_pb'
@@ -131,4 +132,83 @@ describe('WatchHandle', () => {
131132
expect(stopped).toBe(true)
132133
})
133134
})
135+
136+
it('calls onExit with no argument when the watch is stopped by the user', async () => {
137+
// Simulates the transport: the stream stays open until `handleStop`
138+
// aborts it, then surfaces the abort as a Connect cancellation (which
139+
// `handleRpcError` would map to `TimeoutError`).
140+
let abortStream: (() => void) | undefined
141+
const stream = (async function* () {
142+
await new Promise<never>((_, reject) => {
143+
abortStream = () =>
144+
reject(new ConnectError('This operation was aborted', Code.Canceled))
145+
})
146+
})()
147+
148+
let exitArgs: unknown[] | undefined
149+
150+
const handle = new WatchHandle(
151+
() => abortStream?.(),
152+
stream,
153+
undefined,
154+
(...args: unknown[]) => {
155+
exitArgs = args
156+
}
157+
)
158+
159+
await handle.stop()
160+
161+
await vi.waitFor(() => {
162+
expect(exitArgs).toBeDefined()
163+
})
164+
// A user-initiated stop is a clean end - no error is passed to onExit.
165+
expect(exitArgs).toEqual([])
166+
})
167+
168+
it('still reports stream errors when the watch was not stopped by the user', async () => {
169+
const failure = new Error('stream broke')
170+
171+
const stream = (async function* () {
172+
throw failure
173+
})()
174+
175+
let exitArgs: unknown[] | undefined
176+
177+
new WatchHandle(
178+
() => {},
179+
stream,
180+
undefined,
181+
(...args: unknown[]) => {
182+
exitArgs = args
183+
}
184+
)
185+
186+
await vi.waitFor(() => {
187+
expect(exitArgs).toBeDefined()
188+
})
189+
expect(exitArgs).toEqual([failure])
190+
})
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+
})
134214
})

0 commit comments

Comments
 (0)