Skip to content

Commit ef26a93

Browse files
committed
docs: add detailed comment explaining MVCC-safe two-statement design in blockRunWithWaitpoint
1 parent 45e4887 commit ef26a93

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
@@ -366,6 +366,22 @@ export class WaitpointSystem {
366366

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

0 commit comments

Comments
 (0)