Skip to content

Commit 39fa6d2

Browse files
committed
docs: add detailed comment explaining MVCC-safe two-statement design in blockRunWithWaitpoint
1 parent d2056fe commit 39fa6d2

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

internal-packages/run-engine/src/engine/systems/waitpointSystem.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,22 @@ export class WaitpointSystem {
364364

365365
/**
366366
* Prevents a run from continuing until the waitpoint is completed.
367+
*
368+
* This method uses two separate SQL statements intentionally:
369+
*
370+
* 1. A CTE that INSERTs TaskRunWaitpoint rows (blocking connections) and
371+
* _WaitpointRunConnections rows (historical connections).
372+
*
373+
* 2. A separate SELECT that checks if any of the requested waitpoints are still PENDING.
374+
*
375+
* These MUST be separate statements because of PostgreSQL MVCC in READ COMMITTED isolation:
376+
* each statement gets its own snapshot. If a concurrent `completeWaitpoint` commits between
377+
* the CTE starting and finishing, the CTE's snapshot won't see the COMPLETED status. By using
378+
* a separate SELECT, we get a fresh snapshot that reflects the latest committed state.
379+
*
380+
* The pending check queries ALL requested waitpoint IDs (not just the ones actually inserted
381+
* by the CTE). This is intentional: if a TaskRunWaitpoint row already existed (ON CONFLICT
382+
* DO NOTHING skipped the insert), a still-PENDING waitpoint should still count as blocking.
367383
*/
368384
async blockRunWithWaitpoint({
369385
runId,

0 commit comments

Comments
 (0)