lock-or-tx-held-across-io (EFF106)¶
Family: Execution Effects (EFF) · Heuristic · Opt-in · Severity error (a bug) · No automatic fix
Opt-in: await or blocking network I/O while holding a threading lock or a DB transaction.
Heuristic
This rule is heuristic: a premise of its claim can be open ("What a report states" lists them), so a report can be wrong about the code. It runs only when select or extend-select names its code or name. It has not been measured yet. See Rule Precision.
What it does¶
Checks for an await (also the implicit ones of async for and async with), or a call
reaching blocking network I/O (directly or through project helpers, including the getter of a
property read), while a with block holds a synchronous lock or a database transaction.
The held resource must be type-certain:
- an instance of
threading.Lock,RLock,Semaphore,BoundedSemaphore, orCondition, created in__init__(self._lock = threading.Lock()), at module level, locally, or given by an annotated parameter; - a transaction started by
sqlalchemy.orm.Session.begin()/begin_nested(),Connection.begin()/begin_nested(),Engine.begin(),sqlmodel.Session.begin(),django.db.transaction.atomic(), orpsycopg.Connection.transaction().
The rule reports the await or the call while the resource is certainly held (on every path),
with the call chain to the network I/O as related information. Leaving the with block
releases what that block acquired: a re-entrant lock entered again by a nested with stays held
until the outer block exits.
It does not report:
async withand async locks (asyncio.Lock,anyio.Lock, ...);- objects whose type is not known, or not a registered lock or transaction;
- work after the
withblock, and handlers reached only after the block exited; - calls offloaded to a thread (
asyncio.to_thread,loop.run_in_executor, ...); - database queries inside a transaction, local file I/O, or
time.sleep; - CLI commands, ASGI lifespan handlers, and tests, as for
EFF101.
What a report states¶
A report states that, while a with block holds a synchronous lock or a database transaction, the
statement awaits or reaches blocking network I/O.
Its premises:
- Holds: the resource is a
threadinglock or a transaction of the kinds listed above. Established: its type is certain (constructed, or an annotated parameter). - Inside the block: the
awaitor the call is in the body of thewithblock. Established. - Resolves: each call on the chain is to the function shown, through the calls heki resolves. Open through a method call: one candidate implementation that leads on is enough.
- Reaches: each function on the chain reaches the next call. Open: one path is enough, and a branch heki cannot decide counts as taken; only the branches the caller's arguments switch off, and the cases listed above, are left out.
Why is this bad?¶
A synchronous lock held across an await blocks every thread that wants it until the coroutine
resumes, and a second coroutine acquiring it on the event loop thread deadlocks the loop. Held
across a network request, the lock serializes every waiting thread behind that request. A
transaction held across a request or an await keeps its pooled connection checked out and its
row locks taken, which exhausts the pool under load.
Example¶
import asyncio
import threading
class Cache:
def __init__(self) -> None:
self._lock = threading.Lock()
self._data: dict[str, bytes] = {}
async def refresh(self, key: str) -> None:
with self._lock:
self._data[key] = await fetch(key) # EFF106
Use instead:
async def refresh(self, key: str) -> None:
value = await fetch(key)
with self._lock: # hold the lock only for the update
self._data[key] = value
Fix safety¶
No automatic fix.
Options¶
lint.extend-select = ["EFF106"](or--extend-select EFF106) enables the rule. It is opt-in: selecting theEFFprefix does not enable it.lint.relax-async-in-testsapplies as forEFF101.