async-blocking-fs (EFF105)¶
Family: Execution Effects (EFF) · Heuristic · Opt-in · Severity warning (slower code) · No automatic fix
Opt-in: blocking local filesystem I/O reachable from async def.
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 async def functions that reach blocking local filesystem I/O: open(...),
pathlib.Path.read_text / write_text / read_bytes / write_bytes / open, os.listdir,
os.scandir, iterating os.walk(...), shutil copies and removals, file metadata (os.stat,
os.path.exists / isfile / isdir / realpath, pathlib.Path.exists / stat / is_file /
is_dir / resolve, Starlette's StaticFiles.lookup_path), and the pandas file readers
(pd.read_csv(path); one given a URL literal downloads it and is EFF101, one given an
io.StringIO / io.BytesIO buffer does no I/O). It follows the same boundary,
offload, and guard rules as EFF101, generators and offloading decorators included: await
in_thread(self.open) if isinstance(self._input, PathLike) else self.open() is not reported
when open only opens a file under isinstance(self._input, PathLike), and neither is
save(text, io.StringIO()) when save opens its argument only where hasattr(buf, "write")
is false, or save(text) when it opens it only where isinstance(buf, str) holds after
if buf is None: buf = io.StringIO().
This rule is opt-in: enable it with --extend-select EFF105 or extend-select = ["EFF105"].
Selecting the EFF prefix does not enable it.
What a report states¶
A report states that the async def reaches the blocking filesystem call at the end of the chain on
the event loop.
Its premises:
- 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.
- Filesystem I/O: the last call reads or writes the filesystem, as the ecosystem catalog describes it.
Why is this bad?¶
Local file I/O is usually fast, which is why it is opt-in, but on network file systems, large files, or busy disks it blocks the event loop like any other blocking call.
Example¶
Use instead:
import asyncio
from pathlib import Path
async def read_config(path: str) -> str:
return await asyncio.to_thread(Path(path).read_text)
Fix safety¶
No automatic fix.
Options¶
lint.extend-select = ["EFF105"]enables the rule.lint.relax-async-in-testsapplies as forEFF101.