From cc8e0ce18725dbdc8205a1b89886c7e2b9298f90 Mon Sep 17 00:00:00 2001 From: James Murdza Date: Sat, 19 Oct 2024 05:44:30 -0600 Subject: [PATCH] fix: close all E2B terminals when a sandbox is closed --- backend/server/src/index.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/backend/server/src/index.ts b/backend/server/src/index.ts index f659c7f..9f08b3b 100644 --- a/backend/server/src/index.ts +++ b/backend/server/src/index.ts @@ -72,7 +72,6 @@ let isOwnerConnected = false const containers: Record = {} const connections: Record = {} -const terminals: Record = {} const dirName = "/home/user" @@ -210,6 +209,8 @@ io.on("connection", async (socket) => { } ) + const terminals: Record = {} + const sandboxFiles = await getSandboxFiles(data.sandboxId) const projectDirectory = path.posix.join( dirName, @@ -929,6 +930,14 @@ io.on("connection", async (socket) => { connections[data.sandboxId]-- } + // Close all terminals for this connection + await Promise.all( + Object.entries(terminals).map(async ([key, terminal]) => { + await terminal.close() + delete terminals[key] + }) + ) + // Stop watching file changes in the container Promise.all( fileWatchers.map(async (handle: WatchHandle) => {