From 530aa2ff5326d6d7430326f7a9a94f6683bba47c Mon Sep 17 00:00:00 2001 From: James Murdza Date: Fri, 14 Jun 2024 13:32:55 -0400 Subject: [PATCH] fix: ensure container remains open until all owner connections are closed --- backend/server/src/index.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/backend/server/src/index.ts b/backend/server/src/index.ts index da51a46..22e4f56 100644 --- a/backend/server/src/index.ts +++ b/backend/server/src/index.ts @@ -45,6 +45,7 @@ let inactivityTimeout: NodeJS.Timeout | null = null; let isOwnerConnected = false; const containers: Record = {}; +const connections: Record = {}; const terminals: Record = {}; const dirName = path.join(__dirname, ".."); @@ -113,6 +114,7 @@ io.on("connection", async (socket) => { if (data.isOwner) { isOwnerConnected = true; + connections[data.sandboxId] = (connections[data.sandboxId] ?? 0) + 1; } else { if (!isOwnerConnected) { socket.emit("disableAccess", "The sandbox owner is not connected."); @@ -432,6 +434,10 @@ io.on("connection", async (socket) => { socket.on("disconnect", async () => { if (data.isOwner) { + connections[data.sandboxId]--; + } + + if (data.isOwner && connections[data.sandboxId] <= 0) { await Promise.all( Object.entries(terminals).map(async ([key, terminal]) => { await terminal.kill();