fix: Move ensureWorkerRunning to start of hooks to prevent race condition
All hooks now call ensureWorkerRunning() BEFORE querying the database. This ensures the worker's orphaned session cleanup runs before hooks check for active sessions, preventing 404 errors when hooks try to use sessions that don't exist in worker memory after a restart. Hook order now: 1. ensureWorkerRunning() - starts worker, runs cleanup 2. Query DB - cleanup already marked orphaned sessions as failed 3. Use session - only valid sessions are processed Fixed in: - new-hook: Line 26, before DB queries - save-hook: Line 37, before DB queries - summary-hook: Line 24, before DB queries - cleanup-hook: Line 50, before DB queries This prevents the race condition where hooks would read session status before cleanup ran, then get 404 from worker after cleanup marked sessions failed.
This commit is contained in:
@@ -19,6 +19,13 @@ export async function summaryHook(input?: StopInput): Promise<void> {
|
||||
}
|
||||
|
||||
const { session_id } = input;
|
||||
|
||||
// Ensure worker is running first (runs cleanup if restarting)
|
||||
const workerReady = await ensureWorkerRunning();
|
||||
if (!workerReady) {
|
||||
throw new Error('Worker service failed to start or become healthy');
|
||||
}
|
||||
|
||||
const db = new SessionStore();
|
||||
const session = db.findActiveSDKSession(session_id);
|
||||
|
||||
@@ -38,12 +45,6 @@ export async function summaryHook(input?: StopInput): Promise<void> {
|
||||
const promptNumber = db.getPromptCounter(session.id);
|
||||
db.close();
|
||||
|
||||
// Ensure worker is running before requesting summary
|
||||
const workerReady = await ensureWorkerRunning();
|
||||
if (!workerReady) {
|
||||
throw new Error('Worker service failed to start or become healthy');
|
||||
}
|
||||
|
||||
logger.dataIn('HOOK', 'Stop: Requesting summary', {
|
||||
sessionId: session.id,
|
||||
workerPort: session.worker_port,
|
||||
|
||||
Reference in New Issue
Block a user