Skip to content

Commit ef5e0de

Browse files
authored
Merge c3930bc into 21af1f8
2 parents 21af1f8 + c3930bc commit ef5e0de

9 files changed

Lines changed: 447 additions & 19 deletions

File tree

‎.changeset/watch-handle-fixes.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@e2b/python-sdk": patch
3+
"e2b": patch
4+
---
5+
6+
Fix three filesystem watch handle bugs:
7+
8+
- **JS**: `WatchHandle` now awaits async `onEvent`/`onExit` callbacks. A rejecting async `onEvent` is routed to `onExit` and stops the watch instead of becoming an unhandled promise rejection that can crash Node, and async callbacks get backpressure/ordering — matching `CommandHandle`.
9+
- **Python (sync)**: `WatchHandle.get_new_events()` and `stop()` now send a request timeout (default 60s, overridable via `request_timeout`) so a stalled call can't hang the thread forever, and include the authentication header so the polling/stop calls aren't sent unauthenticated on older envd.
10+
- **Python (async)**: `AsyncWatchHandle` now invokes `on_exit` when the stream ends cleanly (with `None`) and when `stop()` is called, in addition to on error — matching the JS SDK.

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,42 @@ export class WatchHandle {
109109
}
110110

111111
private async handleEvents() {
112+
let iterationError: Error | undefined
112113
try {
113114
for await (const event of this.iterateEvents()) {
114115
const eventType = mapEventType(event.value.type)
115116
if (eventType === undefined) {
116117
continue
117118
}
118119

119-
this.onEvent?.({
120+
// Await the callback so an async `onEvent` that rejects is routed to
121+
// `onExit` below (instead of becoming an unhandled promise rejection
122+
// that can crash Node) and so the user can apply backpressure. Mirrors
123+
// `CommandHandle.handleEvents`.
124+
await this.onEvent?.({
120125
name: event.value.name,
121126
type: eventType,
122127
entry: event.value.entry
123128
? mapEntryInfo(event.value.entry)
124129
: undefined,
125130
})
126131
}
127-
this.onExit?.()
128132
} catch (err) {
129-
this.onExit?.(err as Error)
133+
iterationError = err as Error
134+
}
135+
136+
try {
137+
// Invoke `onExit` exactly once: with the error when the watch ended
138+
// because of one, or with no argument on a clean end.
139+
if (iterationError) {
140+
await this.onExit?.(iterationError)
141+
} else {
142+
await this.onExit?.()
143+
}
144+
} catch {
145+
// `onExit` is the terminal callback; an error it throws has nowhere to
146+
// propagate in this detached handler, so it's swallowed to avoid an
147+
// unhandled promise rejection (the failure mode this guards against).
130148
} finally {
131149
this.handleStop()
132150
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { describe, expect, it, vi } from 'vitest'
2+
3+
import { EventType } from '../../../src/envd/filesystem/filesystem_pb'
4+
import {
5+
FilesystemEventType,
6+
WatchHandle,
7+
} from '../../../src/sandbox/filesystem/watchHandle'
8+
9+
function filesystemEvent(name: string, type: EventType = EventType.WRITE) {
10+
return {
11+
event: {
12+
case: 'filesystem' as const,
13+
value: { name, type, entry: undefined },
14+
},
15+
}
16+
}
17+
18+
function events(items: ReturnType<typeof filesystemEvent>[]) {
19+
async function* gen() {
20+
for (const item of items) {
21+
yield item as any
22+
}
23+
}
24+
return gen()
25+
}
26+
27+
describe('WatchHandle', () => {
28+
it('awaits an async onEvent before finishing and firing onExit', async () => {
29+
let callbackStarted = false
30+
let releaseCallback: (() => void) | undefined
31+
const callbackBlocked = new Promise<void>((resolve) => {
32+
releaseCallback = resolve
33+
})
34+
35+
const onEvent = async () => {
36+
callbackStarted = true
37+
await callbackBlocked
38+
}
39+
40+
let exitCalled = false
41+
const onExit = () => {
42+
exitCalled = true
43+
}
44+
45+
new WatchHandle(
46+
() => {},
47+
events([filesystemEvent('a.txt')]),
48+
onEvent,
49+
onExit
50+
)
51+
52+
await vi.waitFor(() => {
53+
expect(callbackStarted).toBe(true)
54+
})
55+
// onExit must not fire while onEvent is still pending.
56+
expect(exitCalled).toBe(false)
57+
58+
releaseCallback?.()
59+
await vi.waitFor(() => {
60+
expect(exitCalled).toBe(true)
61+
})
62+
})
63+
64+
it('routes a rejecting async onEvent to onExit and stops the watch', async () => {
65+
const error = new Error('callback failed')
66+
67+
let stopped = false
68+
let exitErr: Error | undefined | 'unset' = 'unset'
69+
70+
new WatchHandle(
71+
() => {
72+
stopped = true
73+
},
74+
events([filesystemEvent('a.txt')]),
75+
async () => {
76+
throw error
77+
},
78+
(err) => {
79+
exitErr = err ?? undefined
80+
}
81+
)
82+
83+
await vi.waitFor(() => {
84+
expect(exitErr).not.toBe('unset')
85+
})
86+
expect(exitErr).toBe(error)
87+
expect(stopped).toBe(true)
88+
})
89+
90+
it('calls onExit with no argument when the stream ends cleanly', async () => {
91+
const received: FilesystemEventType[] = []
92+
let exitArgs: unknown[] | undefined
93+
94+
new WatchHandle(
95+
() => {},
96+
events([filesystemEvent('a.txt')]),
97+
(event) => {
98+
received.push(event.type)
99+
},
100+
(...args: unknown[]) => {
101+
exitArgs = args
102+
}
103+
)
104+
105+
await vi.waitFor(() => {
106+
expect(exitArgs).toBeDefined()
107+
})
108+
// No error is passed on a clean exit — onExit is called with zero args.
109+
expect(exitArgs).toEqual([])
110+
expect(received).toEqual([FilesystemEventType.WRITE])
111+
})
112+
113+
it('does not let a throwing onExit become an unhandled rejection', async () => {
114+
let stopped = false
115+
116+
new WatchHandle(
117+
() => {
118+
stopped = true
119+
},
120+
events([filesystemEvent('a.txt')]),
121+
() => {},
122+
() => {
123+
throw new Error('onExit failed')
124+
}
125+
)
126+
127+
// handleStop runs after onExit, so observing the stop confirms the loop
128+
// ran the throwing onExit to completion. A leaked rejection would fail the
129+
// test run instead.
130+
await vi.waitFor(() => {
131+
expect(stopped).toBe(true)
132+
})
133+
})
134+
})

‎packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ async def watch_dir(
639639
self,
640640
path: str,
641641
on_event: OutputHandler[FilesystemEvent],
642-
on_exit: Optional[OutputHandler[Exception]] = None,
642+
on_exit: Optional[OutputHandler[Optional[Exception]]] = None,
643643
user: Optional[Username] = None,
644644
request_timeout: Optional[float] = None,
645645
timeout: Optional[float] = 60,
@@ -652,7 +652,7 @@ async def watch_dir(
652652
653653
:param path: Path to a directory to watch
654654
:param on_event: Callback to call on each event in the directory
655-
:param on_exit: Callback to call when the watching ends
655+
:param on_exit: Callback to call when the watching ends. It receives the error that ended the watch, or `None` on a clean end or when `stop()` is called
656656
:param user: Run the operation as this user
657657
:param request_timeout: Timeout for the request in **seconds**
658658
:param timeout: Timeout for the watch operation in **seconds**. Using `0` will not limit the watch time

‎packages/python-sdk/e2b/sandbox_async/filesystem/watch_handle.py‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(
2121
self,
2222
events: AsyncGenerator[WatchDirResponse, Any],
2323
on_event: OutputHandler[FilesystemEvent],
24-
on_exit: Optional[OutputHandler[Exception]] = None,
24+
on_exit: Optional[OutputHandler[Optional[Exception]]] = None,
2525
check_health: Optional[Callable[[], Awaitable[Optional[bool]]]] = None,
2626
):
2727
self._events = events
@@ -60,14 +60,38 @@ async def _iterate_events(self):
6060
except Exception as e:
6161
raise await ahandle_rpc_exception_with_health(e, self._check_health)
6262

63+
async def _call_on_exit(self, error: Optional[Exception]):
64+
if self._on_exit is None:
65+
return
66+
try:
67+
cb = self._on_exit(error)
68+
if inspect.isawaitable(cb):
69+
await cb
70+
except Exception:
71+
# `on_exit` is the terminal callback; an error it raises has nowhere
72+
# to propagate in this background task, so it's swallowed to avoid an
73+
# "Task exception was never retrieved" warning. A `CancelledError`
74+
# (a `BaseException`) is intentionally not caught here.
75+
pass
76+
6377
async def _handle_events(self):
78+
error: Optional[Exception] = None
6479
try:
6580
async for event in self._iterate_events():
6681
cb = self._on_event(event)
6782
if inspect.isawaitable(cb):
6883
await cb
84+
except asyncio.CancelledError:
85+
# `stop()` cancels this task to end the watch. Treat it as a clean,
86+
# user-initiated end: fire `on_exit` (with no error), then propagate
87+
# the cancellation so the task still finishes as cancelled.
88+
await self._call_on_exit(None)
89+
raise
6990
except Exception as e:
70-
if self._on_exit:
71-
cb = self._on_exit(e)
72-
if inspect.isawaitable(cb):
73-
await cb
91+
error = e
92+
93+
# `on_exit` fires exactly once when the watch ends — with the error when
94+
# the stream failed, or with `None` on a clean end. This matches the JS
95+
# SDK, which calls `onExit()` after the loop completes and `onExit(err)`
96+
# on error.
97+
await self._call_on_exit(error)

‎packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,5 +700,8 @@ def watch_dir(
700700
return WatchHandle(
701701
lambda: self._rpc,
702702
r.watcher_id,
703+
self._connection_config,
704+
self._envd_version,
705+
user,
703706
lambda: check_sandbox_health(self._envd_api),
704707
)

‎packages/python-sdk/e2b/sandbox_sync/filesystem/watch_handle.py‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from typing import Callable, List, Optional
22

3+
from packaging.version import Version
4+
35
from e2b import SandboxException
6+
from e2b.connection_config import ConnectionConfig, Username
47
from e2b.envd.filesystem import filesystem_connect
58
from e2b.envd.filesystem.filesystem_pb2 import (
69
GetWatcherEventsRequest,
710
RemoveWatcherRequest,
811
)
9-
from e2b.envd.rpc import handle_rpc_exception_with_health
12+
from e2b.envd.rpc import authentication_header, handle_rpc_exception_with_health
1013
from e2b.sandbox.filesystem.filesystem import map_entry_info
1114
from e2b.sandbox.filesystem.watch_handle import FilesystemEvent, map_event_type
1215

@@ -23,39 +26,59 @@ def __init__(
2326
self,
2427
get_rpc: Callable[[], filesystem_connect.FilesystemClient],
2528
watcher_id: str,
29+
connection_config: ConnectionConfig,
30+
envd_version: Version,
31+
user: Optional[Username] = None,
2632
check_health: Optional[Callable[[], Optional[bool]]] = None,
2733
):
2834
self._get_rpc = get_rpc
2935
self._watcher_id = watcher_id
36+
self._connection_config = connection_config
37+
self._envd_version = envd_version
38+
self._user = user
3039
self._check_health = check_health
3140
self._closed = False
3241

33-
def stop(self):
42+
def stop(self, request_timeout: Optional[float] = None):
3443
"""
3544
Stop watching the directory.
3645
After you stop the watcher you won't be able to get the events anymore.
46+
47+
:param request_timeout: Timeout for the request in **seconds**
3748
"""
3849
try:
3950
self._get_rpc().remove_watcher(
40-
RemoveWatcherRequest(watcher_id=self._watcher_id)
51+
RemoveWatcherRequest(watcher_id=self._watcher_id),
52+
request_timeout=self._connection_config.get_request_timeout(
53+
request_timeout
54+
),
55+
headers=authentication_header(self._envd_version, self._user),
4156
)
4257
except Exception as e:
4358
raise handle_rpc_exception_with_health(e, self._check_health)
4459

4560
self._closed = True
4661

47-
def get_new_events(self) -> List[FilesystemEvent]:
62+
def get_new_events(
63+
self, request_timeout: Optional[float] = None
64+
) -> List[FilesystemEvent]:
4865
"""
4966
Get the latest events that have occurred in the watched directory since the last call, or from the beginning of the watching, up until now.
5067
68+
:param request_timeout: Timeout for the request in **seconds**
69+
5170
:return: List of filesystem events
5271
"""
5372
if self._closed:
5473
raise SandboxException("The watcher is already stopped")
5574

5675
try:
5776
r = self._get_rpc().get_watcher_events(
58-
GetWatcherEventsRequest(watcher_id=self._watcher_id)
77+
GetWatcherEventsRequest(watcher_id=self._watcher_id),
78+
request_timeout=self._connection_config.get_request_timeout(
79+
request_timeout
80+
),
81+
headers=authentication_header(self._envd_version, self._user),
5982
)
6083
except Exception as e:
6184
raise handle_rpc_exception_with_health(e, self._check_health)

‎packages/python-sdk/tests/sync/sandbox_sync/test_thread_local_clients.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,22 +277,24 @@ def test_sync_pty_rpc_clients_are_bound_per_calling_thread(monkeypatch, test_api
277277
assert pty._rpc is main_rpc
278278

279279

280-
def test_sync_watch_handle_uses_calling_thread_rpc():
280+
def test_sync_watch_handle_uses_calling_thread_rpc(test_api_key):
281281
class FakeRpc:
282282
def __init__(self, name):
283283
self.name = name
284284
self.calls = []
285285

286-
def remove_watcher(self, request):
286+
def remove_watcher(self, request, **opts):
287287
self.calls.append(("remove", request.watcher_id))
288288

289+
config = ConnectionConfig(api_key=test_api_key)
290+
289291
main_rpc = FakeRpc("main")
290292
worker_rpc = FakeRpc("worker")
291-
handle = WatchHandle(lambda: main_rpc, "watcher-id")
293+
handle = WatchHandle(lambda: main_rpc, "watcher-id", config, ENVD_VERSION)
292294

293295
handle.stop()
294296
assert main_rpc.calls == [("remove", "watcher-id")]
295297

296-
handle = WatchHandle(lambda: worker_rpc, "watcher-id")
298+
handle = WatchHandle(lambda: worker_rpc, "watcher-id", config, ENVD_VERSION)
297299
run_in_worker_thread(handle.stop)
298300
assert worker_rpc.calls == [("remove", "watcher-id")]

0 commit comments

Comments
 (0)