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 3c4850ee72
commit 530aa2ff53

View File

@ -45,6 +45,7 @@ let inactivityTimeout: NodeJS.Timeout | null = null;
let isOwnerConnected = false;
const containers: Record<string, Sandbox> = {};
const connections: Record<string, number> = {};
const terminals: Record<string, Terminal> = {};
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();