diff --git a/src/host-sweep.ts b/src/host-sweep.ts index 09c82ac76..93a7e8729 100644 --- a/src/host-sweep.ts +++ b/src/host-sweep.ts @@ -256,7 +256,7 @@ export function _resetStuckProcessingRowsForTesting( session: Session, reason: string, ): void { - resetStuckProcessingRows(inDb, outDb, session, reason); + resetStuckProcessingRows(inDb, outDb, session, reason, outDb); } function resetStuckProcessingRows( @@ -264,6 +264,7 @@ function resetStuckProcessingRows( outDb: Database.Database, session: Session, reason: string, + writableOutDb?: Database.Database, ): void { const claims = getProcessingClaims(outDb); const now = Date.now(); @@ -300,19 +301,17 @@ function resetStuckProcessingRows( // would re-read them, see the old status_changed timestamp, conclude the // freshly respawned container is stuck, and SIGKILL it before its // agent-runner has a chance to run clearStaleProcessingAcks() on startup. - // We're safe to write outbound.db here because we just killed the container - // that owned it (or it crashed and left no writer behind). - // outDb was opened readonly for reads above; reopen with write access for this delete. - let outDbRw: Database.Database | null = null; + const ownsDb = !writableOutDb; + let useDb: Database.Database | null = writableOutDb ?? null; try { - outDbRw = openOutboundDbRw(session.agent_group_id, session.id); - const cleared = deleteOrphanProcessingClaims(outDbRw); + if (!useDb) useDb = openOutboundDbRw(session.agent_group_id, session.id); + const cleared = deleteOrphanProcessingClaims(useDb); if (cleared > 0) { log.info('Cleared orphan processing claims', { sessionId: session.id, cleared, reason }); } } catch (err) { log.warn('Failed to clear orphan processing claims', { sessionId: session.id, err }); } finally { - outDbRw?.close(); + if (ownsDb) useDb?.close(); } }