fix: ensure container remains open until all owner connections are closed

This commit is contained in:
James Murdza 2024-06-14 13:32:55 -04:00
parent 7353e88567
commit 869ae6c148

View File

@ -45,6 +45,7 @@ let inactivityTimeout: NodeJS.Timeout | null = null;
let isOwnerConnected = false; let isOwnerConnected = false;
const containers: Record<string, Sandbox> = {}; const containers: Record<string, Sandbox> = {};
const connections: Record<string, number> = {};
const terminals: Record<string, Terminal> = {}; const terminals: Record<string, Terminal> = {};
const dirName = path.join(__dirname, ".."); const dirName = path.join(__dirname, "..");
@ -113,6 +114,7 @@ io.on("connection", async (socket) => {
if (data.isOwner) { if (data.isOwner) {
isOwnerConnected = true; isOwnerConnected = true;
connections[data.sandboxId] = (connections[data.sandboxId] ?? 0) + 1;
} else { } else {
if (!isOwnerConnected) { if (!isOwnerConnected) {
socket.emit("disableAccess", "The sandbox owner is not connected."); socket.emit("disableAccess", "The sandbox owner is not connected.");
@ -432,6 +434,10 @@ io.on("connection", async (socket) => {
socket.on("disconnect", async () => { socket.on("disconnect", async () => {
if (data.isOwner) { if (data.isOwner) {
connections[data.sandboxId]--;
}
if (data.isOwner && connections[data.sandboxId] <= 0) {
await Promise.all( await Promise.all(
Object.entries(terminals).map(async ([key, terminal]) => { Object.entries(terminals).map(async ([key, terminal]) => {
await terminal.kill(); await terminal.kill();