Skip to content

Commit c5fea7d

Browse files
mishushakovclaude
andcommitted
Disconnect from fire-and-forget background commands
Call .disconnect() after background commands.run calls whose output is never read, so the SDK stops holding the event stream open while the process keeps running. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 9ec6c56 commit c5fea7d

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

packages/js-sdk/src/sandbox.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,10 @@ export class Sandbox extends SandboxBase {
467467
* @param fileOrUrl - The file or URL to open.
468468
*/
469469
async open(fileOrUrl: string): Promise<void> {
470-
await this.commands.run(`xdg-open ${fileOrUrl}`, {
470+
const handle = await this.commands.run(`xdg-open ${fileOrUrl}`, {
471471
background: true,
472472
})
473+
await handle.disconnect()
473474
}
474475

475476
/**
@@ -511,10 +512,14 @@ export class Sandbox extends SandboxBase {
511512
* @param uri - The URI to open in the application.
512513
*/
513514
async launch(application: string, uri?: string): Promise<void> {
514-
await this.commands.run(`gtk-launch ${application} ${uri ?? ''}`, {
515-
background: true,
516-
timeoutMs: 0,
517-
})
515+
const handle = await this.commands.run(
516+
`gtk-launch ${application} ${uri ?? ''}`,
517+
{
518+
background: true,
519+
timeoutMs: 0,
520+
}
521+
)
522+
await handle.disconnect()
518523
}
519524

520525
protected async _start(display: string, opts?: SandboxOpts): Promise<void> {
@@ -523,11 +528,12 @@ export class Sandbox extends SandboxBase {
523528
this.stream = new VNCServer(this)
524529

525530
const [width, height] = opts?.resolution ?? [1024, 768]
526-
await this.commands.run(
531+
const xvfbHandle = await this.commands.run(
527532
`Xvfb ${display} -ac -screen 0 ${width}x${height}x24 ` +
528533
`-retro -dpi ${opts?.dpi ?? 96} -nolisten tcp -nolisten unix`,
529534
{ background: true, timeoutMs: 0 }
530535
)
536+
await xvfbHandle.disconnect()
531537

532538
const hasStarted = await this.waitAndVerify(
533539
`xdpyinfo -display ${display}`,
@@ -559,6 +565,7 @@ export class Sandbox extends SandboxBase {
559565
timeoutMs: 0,
560566
})
561567
this.lastXfce4Pid = result.pid
568+
await result.disconnect()
562569
}
563570
}
564571

packages/python-sdk/e2b_desktop/main.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,13 @@ def create(
260260

261261
sbx._display = display
262262
width, height = resolution or (1024, 768)
263-
sbx.commands.run(
263+
xvfb_handle = sbx.commands.run(
264264
f"Xvfb {display} -ac -screen 0 {width}x{height}x24"
265265
f" -retro -dpi {dpi or 96} -nolisten tcp -nolisten unix",
266266
background=True,
267267
timeout=0,
268268
)
269+
xvfb_handle.disconnect()
269270

270271
if not sbx._wait_and_verify(
271272
f"xdpyinfo -display {display}", lambda r: r.exit_code == 0
@@ -306,9 +307,11 @@ def _start_xfce4(self):
306307
f"ps aux | grep {self._last_xfce4_pid} | grep -v grep | head -n 1"
307308
).stdout.strip()
308309
):
309-
self._last_xfce4_pid = self.commands.run(
310+
xfce4_handle = self.commands.run(
310311
"startxfce4", background=True, timeout=0
311-
).pid
312+
)
313+
self._last_xfce4_pid = xfce4_handle.pid
314+
xfce4_handle.disconnect()
312315

313316
@property
314317
def stream(self) -> _VNCServer:
@@ -511,7 +514,8 @@ def open(self, file_or_url: str):
511514
512515
:param file_or_url: The file or URL to open.
513516
"""
514-
self.commands.run(f"xdg-open {file_or_url}", background=True)
517+
handle = self.commands.run(f"xdg-open {file_or_url}", background=True)
518+
handle.disconnect()
515519

516520
def get_current_window_id(self) -> str:
517521
"""
@@ -539,6 +543,7 @@ def launch(self, application: str, uri: Optional[str] = None):
539543
"""
540544
Launch an application.
541545
"""
542-
self.commands.run(
546+
handle = self.commands.run(
543547
f"gtk-launch {application} {uri or ''}", background=True, timeout=0
544548
)
549+
handle.disconnect()

0 commit comments

Comments
 (0)