Skip to content
Back to Blog
Search
Python / asyncio• 21 Aug 2026• 4 min read

The missing await that made my Python loader read old data

While auditing a scheduling service, I found a loader that started work in run_in_executor and read shared state before that work finished.

The first call returned incomplete data. Later calls looked fine because they could see data left by earlier runs.

1. Fifteen lines that reproduce it

This reproduces the timing without the production code:

import asyncio, time
from concurrent.futures import ThreadPoolExecutor

state: dict[str, str] = {}

def load(key: str) -> None:          # stands in for a DB query
    time.sleep(0.05)
    state[key] = "loaded"

async def build_state(loop, ex) -> dict[str, str]:
    loop.run_in_executor(ex, load, "agents")      # <- no await
    loop.run_in_executor(ex, load, "equipment")   # <- no await
    return dict(state)                            # returns while threads still run

async def main():
    ex = ThreadPoolExecutor(4)
    for i in range(5):
        print(f"run {i+1}: {await build_state(asyncio.get_running_loop(), ex)}")
        await asyncio.sleep(0.06)

asyncio.run(main())

Output from the example:

run 1: {}
run 2: {'agents': 'loaded', 'equipment': 'loaded'}
run 3: {'agents': 'loaded', 'equipment': 'loaded'}
run 4: {'agents': 'loaded', 'equipment': 'loaded'}
run 5: {'agents': 'loaded', 'equipment': 'loaded'}

2. Why Python stays quiet

An unawaited coroutine can warn. run_in_executor returns a Future; if the job succeeds, dropping that Future produces no equivalent warning.

What you forgot to awaitWhat Python tells you
a coroutineRuntimeWarning: coroutine ... was never awaited
run_in_executor(...)nothing at all

I got the same behavior on Python 3.9 and 3.14.

A failed job can report Future exception was never retrieved when the Future is collected. That warning does not help when the job succeeds too late.

3. Why nobody catches it in review

The bug then seems to heal itself. state is a module-level global, so the second call reads what the first call’s threads wrote after it had already returned.

Start each call with fresh state and the bug reproduces every time in this example:

state GLOBAL (kept between calls):  [0, 2, 2, 2]   <- only the first call is wrong
state LOCAL  (reset every call):    [0, 0, 0, 0]   <- wrong every time

That is why it never became a ticket: a reload makes it work, and the empty first read comes back only after a cold start in production.

4. Awaiting the Futures fixes only the timing

Collect and await the Futures:

futures = [
    loop.run_in_executor(ex, load, "agents"),
    loop.run_in_executor(ex, load, "equipment"),
]
await asyncio.gather(*futures)
return dict(state)

That fixes the timing. If a loader raises after another one writes, the shared object can still be half updated.

5. Build beside, then swap

Build the next state separately and replace the live reference only after every job succeeds:

async def build_state(loop, ex) -> dict[str, str]:
    global state
    new_state: dict[str, str] = {}                  # build beside the live one

    def load_into(target, key):
        time.sleep(0.05)
        target[key] = "loaded"

    await asyncio.gather(
        loop.run_in_executor(ex, load_into, new_state, "agents"),
        loop.run_in_executor(ex, load_into, new_state, "equipment"),
    )

    state = new_state                               # swap only after all succeed
    return dict(state)

If gather() raises, the assignment never runs and readers keep the previous state.

6. The two causes

Two things made this bug: work that was started and never joined, and shared state that outlived the call that produced it. Either one alone would have done little harm here; together they produced wrong data and no error. Building beside and swapping is copy-on-write, the same idea as writing a temp file and calling os.rename().

7. How to check your own code

  • Find bare run_in_executor calls. Check who awaits or otherwise handles the returned Future.
  • Check who owns tasks created with asyncio.create_task() or ensure_future().
  • Test with fresh state, then make one loader fail. A failed refresh should leave the previous state intact.

The report I wrote at the time said “add the missing await”. It took a second pass to notice that the missing await was the smaller half of the problem.

Python / asyncioRSSBack to Blog