Awesome Reviewers

Reviewer · Concurrency

Safe concurrent state handling

Code that runs under threads or asyncio must treat shared mutable state as unsafe by default: either protect it with explicit synchronization, isolate it per task, or bound it — and never assume a "best effort" snapshot or identity check is a correctness guarantee.

Instruction

Code that runs under threads or asyncio must treat shared mutable state as unsafe by default: either protect it with explicit synchronization, isolate it per task, or bound it — and never assume a “best effort” snapshot or identity check is a correctness guarantee.

  1. Guard check-then-act on shared collections. A membership check followed by a mutation (if name not in seen: seen.add(name)) is not atomic across threads. Wrap it in a lock: ```python _warned_lock = threading.Lock()

def _warn_once(name): with _warned_lock: if name in _warned: return _warned.add(name) logger.warning(…)

If you instead rely on a cheap snapshot (e.g. `self.__dict__.copy()`) to avoid a crash, document clearly that it only prevents the crash and is not a consistency guarantee — don't let callers assume more than that.

2. **Isolate state per concurrent task instead of sharing an instance.** When the same object (agent, tool target, helper) is invoked from multiple threads/requests concurrently, give each invocation its own state (clone the object, or route per-request writes to new instances) rather than mutating shared instance attributes. When isolation depends on an explicit list of "stateful helpers," keep that list next to the owning class (e.g. a classvar or hook) and add a test that fails if a new stateful helper is added without updating it — don't let isolation correctness depend on an easily-forgotten hardcoded list.

3. **Let cancellation propagate in async code.** Driving generators/coroutines manually must catch `Exception`, not `BaseException`, so `asyncio.CancelledError`, `KeyboardInterrupt`, and `SystemExit` are never captured and re-thrown into application logic — that can convert a real cancellation into normal completion and prevent timeouts from working.

4. **Always clean up spawned tasks, including on cancellation.** When you fan out work with `asyncio.ensure_future`/`gather`, a `finally` block must cancel and await any tasks that are still pending if the awaiting coroutine itself is cancelled — otherwise child tasks keep running after the caller believes the operation is done, writing into state that gets reused/reset on the next call:
```python
try:
    await asyncio.gather(*runs, return_exceptions=True)
finally:
    for run in runs:
        if not run.done():
            run.cancel()
    await asyncio.gather(*runs, return_exceptions=True)
  1. Bound anything fed by concurrent producers. A drain loop or buffer consuming from a queue shared with another thread (e.g. a steering/message queue) must have a cap on how much it consumes or accumulates per cycle, so a fast or bursty producer cannot grow memory/context unboundedly.

Add a test for each synchronization/isolation/cancellation guarantee you introduce — these bugs are not caught by accident and silently regress under refactors or renames.