Back to all reviewers

Verify state before execution

openai/codex
Based on 2 comments
Typescript

When scheduling asynchronous operations in concurrent environments, always verify the system state hasn't changed before executing critical operations to prevent race conditions. This is especially important in interactive applications where user actions (like cancellations) might occur between the time an operation is scheduled and when it executes.

Concurrency Typescript

Reviewer Prompt

When scheduling asynchronous operations in concurrent environments, always verify the system state hasn’t changed before executing critical operations to prevent race conditions. This is especially important in interactive applications where user actions (like cancellations) might occur between the time an operation is scheduled and when it executes.

To implement this pattern:

  • Use appropriate timing mechanisms (setTimeout, queueMicrotask) based on requirements
  • Always double-check cancellation flags and system state immediately before performing operations
  • Consider using nested timing approaches for different aspects of the operation when needed

Example:

// Instead of this:
setTimeout(() => {
  this.onItem(item);
  staged[idx] = undefined;
}, delay);

// Do this:
setTimeout(() => {
  // Double-check cancellation state right before executing
  if (
    thisGeneration === this.generation &&
    !this.canceled &&
    !this.hardAbort.signal.aborted
  ) {
    this.onItem(item);
    staged[idx] = undefined;
  }
}, delay);

This pattern helps maintain consistency between internal state and UI, prevents “ghost” operations from occurring after cancellation, and makes your concurrent code more robust against race conditions.

2
Comments Analyzed
Typescript
Primary Language
Concurrency
Category

Source Discussions