black icon indicating copy to clipboard operation
black copied to clipboard

black --check exits 0 and prints All done when a multi file run is interrupted

Open jiteshgulati opened this issue 1 month ago • 0 comments

Describe the bug

if black is formatting more than one file and the run gets interrupted with ctrl+c or SIGTERM it prints Aborted! but then still prints All done and exits 0

so black --check passes even though some files never got checked. only the files that finished before the signal end up in the report and the cancelled ones are just dropped

To Reproduce

make a folder with a bunch of already formatted files and one that isnt

mkdir demo && cd demo && git init -q
python - <<'EOF'
from pathlib import Path
src = "".join(f"def f_{i}(a, b):\n    return a + b * {i}\n\n\n" for i in range(3000)).rstrip() + "\n"
for i in range(1, 400):
    Path(f"a_{i:03}.py").write_text(src)
Path("zz_unformatted.py").write_text("x  =  1\n")
EOF

run it normally and it fails like it should

$ black --check .
would reformat /path/to/demo/zz_unformatted.py

Oh no! 💥 💔 💥
1 file would be reformatted, 399 files would be left unchanged.
$ echo $?
1

run it again and hit ctrl+c a few seconds in

$ black --check .
Aborted!
All done! ✨ 🍰 ✨
19 files would be left unchanged.
$ echo $?
0

kill -TERM <pid of black> does the same thing. it also means something like black --check . && git push keeps going after ctrl+c because black exited 0

with a single file it works fine, ctrl+c gives Aborted! and exit 1

Expected behavior

an interrupted run shouldnt look like a success. it should exit non zero like the single file case does and not print All done

Environment

  • Black's version: 26.10.1 (compiled: yes) and current main (11cff13)
  • OS and Python version: Linux (WSL2) with Python 3.11 and 3.14

Additional context

looks like the cancelled tasks in schedule_formatting in src/black/concurrency.py get collected but never counted anywhere and the function returns normally, so main() builds the exit code only from the files that finished. I could reproduce it back to 22.1.0

depending on timing a worker can get killed in the middle of a file and then you get 123 instead, but most of my runs ended with 0

I have a fix with a test on my fork (raise after the cleanup so it exits 1 like the single file path) and can open a PR if that sounds ok

jiteshgulati avatar Oct 14 '26 10:10 jiteshgulati