|
5 | 5 | * Dashboard API Handlers |
6 | 6 | */ |
7 | 7 |
|
8 | | -// REVISION: dashboards-v6-link-sync-with-rbac |
| 8 | +// REVISION: dashboards-v8-teardown-without-snapshot |
9 | 9 |
|
10 | 10 | import type { Env, Dashboard, DashboardItem, DashboardEdge } from '../types'; |
11 | 11 | import { syncItemToLinked, syncEdgeToLinked } from '../links/handler'; |
12 | 12 | import { populateFromTemplate } from '../templates/handler'; |
13 | 13 | import type { EnvWithDriveCache } from '../storage/drive-cache'; |
14 | 14 | import { FlyMachinesClient } from '../sandbox/fly-machines'; |
| 15 | +import { SandboxClient } from '../sandbox/client'; |
15 | 16 | import { preProvisionDashboardSandbox } from '../sessions/handler'; |
16 | 17 |
|
17 | 18 | function generateId(): string { |
@@ -282,21 +283,38 @@ export async function deleteDashbоard( |
282 | 283 | return Response.json({ error: 'E79304: Not found or not owner' }, { status: 404 }); |
283 | 284 | } |
284 | 285 |
|
285 | | - // Stop any active sessions first so their PTYs are killed in the sandbox. |
286 | | - // CASCADE-deleting the session rows would otherwise orphan live processes |
287 | | - // (shells, and agent children like node/chromium) in the VM. Mirrors deleteItem. |
| 286 | + // Mark active sessions stopped. Deliberately NOT via stopSession(). |
| 287 | + // |
| 288 | + // stopSession is the "user closed a terminal, keep the dashboard usable" path: |
| 289 | + // when it stops the last session it captures a workspace snapshot into R2 for |
| 290 | + // recovery, and deletes that session's PTY. Both are wrong when the dashboard |
| 291 | + // itself is going away — the snapshot is written moments before its dashboard |
| 292 | + // ceases to exist and nothing ever collects it, so every deletion leaked an |
| 293 | + // R2 object. |
| 294 | + // |
| 295 | + // It was also called in a sequential loop, one round trip per terminal, each |
| 296 | + // able to block for the full 15s sandbox timeout. That made deletion slowest |
| 297 | + // in exactly the case where it matters most — a wedged sandbox, which is the |
| 298 | + // state a user is trying to clean up. The single bounded deleteSession below |
| 299 | + // replaces all of it: destroying the sandbox session tears down every PTY it |
| 300 | + // owns, so the per-PTY deletes were redundant anyway. |
288 | 301 | try { |
289 | | - const activeSessions = await env.DB.prepare(` |
290 | | - SELECT id FROM sessions WHERE dashboard_id = ? AND status IN ('creating', 'active') |
291 | | - `).bind(dashboardId).all<{ id: string }>(); |
292 | | - if (activeSessions.results.length > 0) { |
293 | | - const { stоpSessiоn } = await import('../sessions/handler'); |
294 | | - for (const session of activeSessions.results) { |
295 | | - await stоpSessiоn(env as EnvWithDriveCache, session.id, userId); |
296 | | - } |
297 | | - } |
| 302 | + const now = new Date().toISOString(); |
| 303 | + await env.DB.prepare(` |
| 304 | + UPDATE sessions SET status = 'stopped', stopped_at = ? |
| 305 | + WHERE dashboard_id = ? AND status IN ('creating', 'active') |
| 306 | + `).bind(now, dashboardId).run(); |
298 | 307 | } catch { |
299 | | - // Best-effort — don't block dashboard deletion if session cleanup fails. |
| 308 | + // Best-effort — don't block dashboard deletion if the status update fails. |
| 309 | + } |
| 310 | + |
| 311 | + // Drop the dashboard's R2 objects (workspace snapshot, mirror manifests). |
| 312 | + // Nothing cascades these; without this they outlive the dashboard forever. |
| 313 | + try { |
| 314 | + const { purgeDashbоardStorage } = await import('../sessions/handler'); |
| 315 | + await purgeDashbоardStorage(env as EnvWithDriveCache, dashboardId); |
| 316 | + } catch (e) { |
| 317 | + console.error(`[deleteDashboard] Storage purge failed for ${dashboardId}: ${e}`); |
300 | 318 | } |
301 | 319 |
|
302 | 320 | // Delete dependent records that don't have ON DELETE CASCADE |
@@ -332,12 +350,55 @@ export async function deleteDashbоard( |
332 | 350 | .bind(dashboardId) |
333 | 351 | .run(); |
334 | 352 |
|
| 353 | + // Read the sandbox row once: both the session release below and the Fly |
| 354 | + // teardown after it need it. |
| 355 | + const sandboxRow = await env.DB.prepare(` |
| 356 | + SELECT sandbox_session_id, sandbox_machine_id, fly_volume_id FROM dashboard_sandboxes WHERE dashboard_id = ? |
| 357 | + `).bind(dashboardId).first<{ |
| 358 | + sandbox_session_id: string; |
| 359 | + sandbox_machine_id: string; |
| 360 | + fly_volume_id: string; |
| 361 | + }>(); |
| 362 | + |
| 363 | + // Release the sandbox session — UNCONDITIONALLY, not just on Fly. |
| 364 | + // |
| 365 | + // A sandbox session owns real resources inside the VM: its PTYs and a full |
| 366 | + // Chromium/Xvfb/x11vnc stack, several hundred MB each. Session.Close() is |
| 367 | + // reachable only via DELETE /sessions/:id, and this was previously called |
| 368 | + // only from the Fly-gated block below plus a create-race dedup. On any |
| 369 | + // deployment sharing one sandbox (local dev, desktop, self-hosted), deleting |
| 370 | + // a dashboard therefore freed its D1 rows and nothing else, so every |
| 371 | + // dashboard ever created leaked a browser stack until the VM exhausted its |
| 372 | + // memory — at which point the Go server was starved badly enough that even |
| 373 | + // GET /health timed out, and every subsequent session create failed with |
| 374 | + // "fetch error: The operation was aborted". |
| 375 | + // |
| 376 | + // Destroying the Fly machine (below) implicitly reclaims everything, so this |
| 377 | + // is redundant there and merely tidy; it is load-bearing everywhere else. |
| 378 | + // |
| 379 | + // Best-effort: an unreachable or already-wedged sandbox must not block |
| 380 | + // deleting the dashboard, or the user cannot clean up the very state that is |
| 381 | + // wedging it. |
| 382 | + if (sandboxRow?.sandbox_session_id && env.SANDBOX_URL) { |
| 383 | + try { |
| 384 | + const sandbox = new SandboxClient(env.SANDBOX_URL, env.SANDBOX_INTERNAL_TOKEN); |
| 385 | + await sandbox.deleteSession( |
| 386 | + sandboxRow.sandbox_session_id, |
| 387 | + sandboxRow.sandbox_machine_id || undefined |
| 388 | + ); |
| 389 | + console.log( |
| 390 | + `[deleteDashboard] Released sandbox session ${sandboxRow.sandbox_session_id} for dashboard ${dashboardId}` |
| 391 | + ); |
| 392 | + } catch (e) { |
| 393 | + console.error( |
| 394 | + `[deleteDashboard] Failed to release sandbox session ${sandboxRow.sandbox_session_id}: ${e}` |
| 395 | + ); |
| 396 | + } |
| 397 | + } |
| 398 | + |
335 | 399 | // Destroy Fly machine and volume for this dashboard (best-effort) |
336 | 400 | if (env.FLY_API_TOKEN && env.FLY_APP_NAME) { |
337 | 401 | try { |
338 | | - const sandboxRow = await env.DB.prepare(` |
339 | | - SELECT sandbox_machine_id, fly_volume_id FROM dashboard_sandboxes WHERE dashboard_id = ? |
340 | | - `).bind(dashboardId).first<{ sandbox_machine_id: string; fly_volume_id: string }>(); |
341 | 402 | if (sandboxRow?.sandbox_machine_id) { |
342 | 403 | const fly = new FlyMachinesClient(env.FLY_APP_NAME, env.FLY_API_TOKEN); |
343 | 404 | try { |
|
0 commit comments