fix: close Pi worker rebuild races

This commit is contained in:
2026-08-22 23:42:22 +08:00
parent 9f55eec9aa
commit e79aeffffa
3 changed files with 99 additions and 3 deletions

View File

@@ -470,9 +470,25 @@ export class PiWorkerPool {
if (this.shuttingDown) throw new Error('Pi worker pool is shutting down');
const pendingPrepare = this.prepareFlights.get(conversationId);
if (pendingPrepare) await pendingPrepare;
const record = this.workers.get(conversationId);
let record = this.workers.get(conversationId);
if (!record) throw new Error('Conversation worker is not prepared');
this.cancelConversationRuns(conversationId, new Error('Conversation worker is recovering'));
while (record.rebuildFlight) {
const existingFlight = record.rebuildFlight;
try {
return this.publicState(await existingFlight);
} catch (error) {
if (this.shuttingDown) throw error;
const current = this.workers.get(conversationId);
if (current && current !== record) {
record = current;
continue;
}
if (record.rebuildFlight !== existingFlight) continue;
record.rebuildFlight = undefined;
break;
}
}
record.rebuildFlight = this.beginRebuild(record, this.revisions.current);
return this.publicState(await record.rebuildFlight);
}
@@ -673,6 +689,7 @@ export class PiWorkerPool {
generation,
state: this.publicState(replacement),
});
await this.trimIdleWorkers();
return replacement;
} catch (error) {
record.state = 'crashed';