---
title: "A review bot reports a race condition: find the interleaving first | Proofline"
description: "A race condition claim holds only if some second operation can run between two steps of the first. Find that point in the code."
url: https://proofline.sh/learn/ai-review-race-condition-comment/
---

[Learning center](/learn/)

# A review bot reports a race condition: find the interleaving first

A race condition claim holds only if some second operation can run between two steps of the first. Find that point in the code. If it exists, change the code and add a test that forces the interleaving. If nothing can run there, because the runtime is single-threaded and the steps have no await between them, dismiss the comment with that reason, and check it again whenever someone adds an await.

Code review The Proofline team November 22, 2026 4 min read

## Name the two operations and the gap

A useful race comment names three things: the shared state, the two operations that touch it, and the place where one can start before the other finishes. Many bot comments name only the first two. Before you answer, write down the third yourself.

The gap depends on the runtime:

-   With threads, as in Java, Go, C++ or Rust, the gap can be between any two machine instructions unless a lock, an atomic or a channel orders them.
-   In JavaScript, Python's `asyncio` or a Dart isolate, one thread runs your code. Another task can run only where the current one yields: at an `await`, a callback or a `yield`. Dart's documentation says each isolate has "its own memory and a single thread running an event loop" ([Dart concurrency](https://dart.dev/language/concurrency#isolates)).
-   Between processes or hosts, nothing in your code orders the steps. A lock in memory does not help there.

So "is there a race?" becomes "is there a yield point, a thread switch or another process between the read and the write?" You can usually answer that by reading the function.

## Follow a race claim through a Flutter fix

A September 2026 pull request in `flutter/packages` fixed two Windows preference stores that deleted each other's data, because both wrote the whole of one JSON file from their own cached copy. The fix re-read the file before each write. gemini-code-assist marked a comment on it critical: "Re-reading the file before every write introduces a severe race condition when multiple writes are performed concurrently" ([review comment](https://github.com/flutter/packages/pull/12867#discussion_r4008511887)). Its example was two writes started together, such as with `Future.wait`, each reading the file before either wrote back.

The author agreed: "Thanks — the concurrency point was right, and the first version of this patch did regress it" ([author's reply](https://github.com/flutter/packages/pull/12867#issuecomment-5670664815)). Instead of the suggested lock, they removed every `await` between the read, the update and the write, and explained why that is enough: "so in a single-threaded isolate the whole read-modify-write is already atomic against every other write in the process". The same reply said the change did not make writes atomic across processes, and pointed to flutter/flutter#89211, which tracks that.

That reasoning held for as long as the function had no yield point. Later commits added a retry when another process was mid-write, first with a synchronous `sleep`. A maintainer objected that this blocked the UI isolate and asked for "a per-file async queue/lock" ([maintainer's comment](https://github.com/flutter/packages/pull/12867#discussion_r4648025142)). In November 2026 the author replied: "The retry now waits with `await Future<void>.delayed(...)`", and "updates of the same file go through a per-file queue" ([author's reply](https://github.com/flutter/packages/pull/12867#discussion_r4648941854)). The new await reopened the gap the bot had described, so the queue became necessary.

The branch includes a test for that interleaving. It makes the first write's read come back empty so the code has to retry, then starts two writes with `Future.wait`. Its comment reads: "The first write's read yields to the event loop to retry, which is where the second write would get in if the two were not queued." ([diff](https://patch-diff.githubusercontent.com/raw/flutter/packages/pull/12867.diff)) When we last checked, on 22 November 2026, the pull request was still open after a 20 November review that requested changes.

## Force the interleaving in a test

You rarely need a stress test to settle a single-threaded race. Put a yield where the gap would be and run the operations together. This Python sketch uses `asyncio.sleep(0)` in place of a retry delay:

```
async def update_no_await(key):
    prefs = dict(store)
    prefs[key] = 1
    store.clear(); store.update(prefs)

async def update_with_await(key):
    prefs = dict(store)
    await asyncio.sleep(0)
    prefs[key] = 1
    store.clear(); store.update(prefs)
```

Four concurrent calls through `asyncio.gather` leave `['k0', 'k1', 'k2', 'k3']` in the store for the first version and only `['k3']` for the second. The test that fails on the second version is the one to keep: it breaks when someone adds an await to the first.

With real threads, a deterministic test is harder. Use the tools that observe the interleaving for you: Go's [race detector](https://go.dev/doc/articles/race_detector) with `go test -race`, or [ThreadSanitizer](https://clang.llvm.org/docs/ThreadSanitizer.html) for C, C++ and Rust. They report only races that a test run actually executes, so a clean run proves nothing about code the tests never reach together.

## Decide from what you found

| What the code shows | Answer |
| --- | --- |
| A yield point, thread switch or unsynchronized shared write between the read and the write | Change the code, and add a test that forces that interleaving |
| Threads share the state, and you cannot build a deterministic test | Change the code, and run the tests under a race detector |
| One thread, and no await, callback or yield between the steps | Dismiss, naming the runtime and the absent yield point; add a test if a later edit could add one |
| The gap is between processes or hosts | Defer to an issue if the fix needs a file lock or a database constraint, as the Flutter author did |
| The comment names no operations and no gap | Ask, or dismiss it as unsupported |

The third row is where most bot race comments on JavaScript and Python code land, and it is where the Flutter thread shows the cost of a bare dismissal. "Single-threaded" was true of the code that day and stopped being true two months later. Write the reason as a comment beside the code, or better, as the test above, so the next change that adds an await fails a check instead of a user's data. For a race that already reached users, the guides on [out-of-order search responses](/learn/stale-search-results-race/) and [double-submitted forms](/learn/duplicate-submit-double-click/) show how to prove the interleaving from a browser.

## One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. [Eligibility and fair-use terms](/free-code-review/).

[Start free with GitHub](https://proofline.sh/orgs/plan?offer=free_month)

## Related reading

-   ### [Search shows results for an earlier query: prove the responses arrived out of order](/learn/stale-search-results-race/)
    
    Network outages The Proofline team November 20, 2026 4 min read
    
-   ### [Check a missing await comment against what the call returns](/learn/ai-review-missing-await-comment/)
    
    Code review The Proofline team November 22, 2026 4 min read
