multiCMD 1.60__tar.gz → 1.61__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. {multicmd-1.60 → multicmd-1.61}/PKG-INFO +53 -3
  2. {multicmd-1.60 → multicmd-1.61}/README.md +52 -2
  3. {multicmd-1.60 → multicmd-1.61}/multiCMD.egg-info/PKG-INFO +53 -3
  4. {multicmd-1.60 → multicmd-1.61}/multiCMD.egg-info/SOURCES.txt +1 -0
  5. {multicmd-1.60 → multicmd-1.61}/multiCMD.py +149 -20
  6. multicmd-1.61/tests/test_cli_closed_stdout.py +449 -0
  7. {multicmd-1.60 → multicmd-1.61}/tests/test_display.py +48 -0
  8. {multicmd-1.60 → multicmd-1.61}/tests/test_handover.py +38 -0
  9. {multicmd-1.60 → multicmd-1.61}/tests/test_process_groups.py +27 -1
  10. {multicmd-1.60 → multicmd-1.61}/LICENSE +0 -0
  11. {multicmd-1.60 → multicmd-1.61}/multiCMD.egg-info/dependency_links.txt +0 -0
  12. {multicmd-1.60 → multicmd-1.61}/multiCMD.egg-info/entry_points.txt +0 -0
  13. {multicmd-1.60 → multicmd-1.61}/multiCMD.egg-info/top_level.txt +0 -0
  14. {multicmd-1.60 → multicmd-1.61}/scripts/multiCMD +0 -0
  15. {multicmd-1.60 → multicmd-1.61}/setup.cfg +0 -0
  16. {multicmd-1.60 → multicmd-1.61}/setup.py +0 -0
  17. {multicmd-1.60 → multicmd-1.61}/tests/test_api.py +0 -0
  18. {multicmd-1.60 → multicmd-1.61}/tests/test_async_executor.py +0 -0
  19. {multicmd-1.60 → multicmd-1.61}/tests/test_cli.py +0 -0
  20. {multicmd-1.60 → multicmd-1.61}/tests/test_cli_input.py +0 -0
  21. {multicmd-1.60 → multicmd-1.61}/tests/test_cli_summary.py +0 -0
  22. {multicmd-1.60 → multicmd-1.61}/tests/test_embed.py +0 -0
  23. {multicmd-1.60 → multicmd-1.61}/tests/test_env.py +0 -0
  24. {multicmd-1.60 → multicmd-1.61}/tests/test_expand_ranges.py +0 -0
  25. {multicmd-1.60 → multicmd-1.61}/tests/test_helpers.py +0 -0
  26. {multicmd-1.60 → multicmd-1.61}/tests/test_interrupts.py +0 -0
  27. {multicmd-1.60 → multicmd-1.61}/tests/test_packaging.py +0 -0
  28. {multicmd-1.60 → multicmd-1.61}/tests/test_parsing.py +0 -0
  29. {multicmd-1.60 → multicmd-1.61}/tests/test_pass_fds.py +0 -0
  30. {multicmd-1.60 → multicmd-1.61}/tests/test_ping.py +0 -0
  31. {multicmd-1.60 → multicmd-1.61}/tests/test_pool.py +0 -0
  32. {multicmd-1.60 → multicmd-1.61}/tests/test_pty.py +0 -0
  33. {multicmd-1.60 → multicmd-1.61}/tests/test_reader.py +0 -0
  34. {multicmd-1.60 → multicmd-1.61}/tests/test_regressions.py +0 -0
  35. {multicmd-1.60 → multicmd-1.61}/tests/test_run_command.py +0 -0
  36. {multicmd-1.60 → multicmd-1.61}/tests/test_run_commands.py +0 -0
  37. {multicmd-1.60 → multicmd-1.61}/tests/test_sudo.py +0 -0
  38. {multicmd-1.60 → multicmd-1.61}/tests/test_supervision.py +0 -0
  39. {multicmd-1.60 → multicmd-1.61}/tests/test_task.py +0 -0
  40. {multicmd-1.60 → multicmd-1.61}/tests/test_windows.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: multiCMD
3
- Version: 1.60
3
+ Version: 1.61
4
4
  Summary: Run commands simultaneously
5
5
  Home-page: https://github.com/yufei-pan/multiCMD
6
6
  Author: Yufei Pan
@@ -119,6 +119,7 @@ exit status:
119
119
  N several commands: how many failed, capped at 101
120
120
  2 usage error
121
121
  130 interrupted with Ctrl-C ( 128+S after SIGTERM / SIGHUP )
122
+ 141 stdout closed ( `mcmd ... | head` ): commands stopped, ended by SIGPIPE ( 1 on Windows, untested there )
122
123
  ```
123
124
 
124
125
  ### Examples
@@ -192,6 +193,7 @@ mcmd --shell -m 4 "dmesg | tail -n 5" "journalctl -n 5 > /tmp/j.log"
192
193
  | Several commands | the number that failed, capped at `101` |
193
194
  | Usage error (bad option, unbalanced quote, no commands) | `2` |
194
195
  | Ctrl-C | `130` (`128+N` after SIGTERM / SIGHUP) |
196
+ | stdout has no reader (`mcmd ... \| head`) | ended by SIGPIPE, which a shell reports as `141`; `1` on Windows (untested there) |
195
197
 
196
198
  ### Input files and templates
197
199
 
@@ -441,7 +443,11 @@ Signals sent to your program's group do not reach the commands, so multiCMD stop
441
443
  Ctrl-C (or a SIGTERM handler's `sys.exit()`) in a call waiting on the main thread, `join_threads()`
442
444
  and `AsyncExecutor.wait()` included, stops every command, skips the queued ones and
443
445
  re-raises, giving them up to 5 s to exit; a second Ctrl-C ends that wait at once. `mcmd` stops its
444
- commands on SIGTERM / SIGHUP (unless started with them ignored, as under `nohup`). At interpreter exit
446
+ commands on SIGTERM / SIGHUP (unless started with them ignored, as under `nohup`), and when its stdout
447
+ has no reader (`mcmd ... | head`): it stops them as a Ctrl-C does, and ends by SIGPIPE, as `yes | head` does, with
448
+ nothing on stderr. It learns that stdout has no reader only when a write to it fails, so a command that prints nothing
449
+ more runs to its end first (`mcmd 'sleep 4' | head -2` takes 4 s, as `sleep 4 | head -2` does). A program that imports
450
+ multiCMD still gets the `BrokenPipeError`. At interpreter exit
445
451
  the commands still running are killed, and a command queued on one of multiCMD's threads fails (-1,
446
452
  "not started: the interpreter is exiting") instead of outliving the program; one run from an `atexit`
447
453
  handler after that, on the main thread, runs as usual. A program that handles SIGTERM itself, or runs
@@ -470,7 +476,11 @@ on any thread). Measured at 0.02 to 0.42 % of the Ctrl-Cs that land during a 0.2
470
476
  1.59. In the first milliseconds of a fire-and-forget batch (`wait_for_return=False`) a first Ctrl-C can also be
471
477
  dropped, about 1 in 700 of those that land there on 3.12 and 3.14 (1.59: 1 in 1,400, and it hung on 3.14): Python
472
478
  ignores an exception raised in a weakref callback and prints "Exception ignored", the program goes on, and the next
473
- Ctrl-C works.
479
+ Ctrl-C works. A serial run can drop one too, in `Popen`'s cleanup (`Popen.__del__`) of a command that has finished (12 of
480
+ about 6,000 aimed Ctrl-Cs with 1.60, 3 with 1.61): the same message, and the next Ctrl-C works. On Python 3.6-3.10 a
481
+ Ctrl-C that arrives as a command starts waits for the exec wrapper's `Popen`, normally about 1 ms (2-5 ms at p99); if the
482
+ exec of the Python binary itself hangs (a venv on an NFS mount that stopped answering), it does nothing until the mount
483
+ answers.
474
484
  These and the other known limits are in [docs/limitations.md](docs/limitations.md).
475
485
 
476
486
  ## Key functions and objects
@@ -522,6 +532,46 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
522
532
 
523
533
  `test_expand_ranges_perf.py` is a manual performance microbenchmark and is not collected by pytest.
524
534
 
535
+ ## Upgrading to 1.61 from 1.60
536
+
537
+ - Nothing needs doing to upgrade.
538
+ - Fix: `mcmd ... | head` (a reader that quits early) ends quietly. It printed a traceback or an "Exception ignored" message
539
+ and exited 120 (Python's status for a stdout it cannot flush), and the commands were left to run on: a quiet one,
540
+ or one that ignores SIGPIPE, ran to its end or for ever, with `mcmd` waiting for it. Now `mcmd` stops the commands as a
541
+ Ctrl-C does, and ends by SIGPIPE, as `yes | head` does: a shell reports 141, and stderr stays empty (exit status 1 on
542
+ Windows, untested: a closed pipe may show there as an `OSError` that `mcmd` does not catch yet, planned for 1.62). It
543
+ covers the commands' output, the summary, `--dry-run`, `-h` and `-V`. The library's functions still raise
544
+ `BrokenPipeError` when `sys.stdout` fails. A thread that finds stdout closed only says so, and the main thread's
545
+ waits stop the commands at their next step (at most 0.1 s later), while a command not started yet is skipped;
546
+ `mcmd` leaves SIGPIPE ignored, as Python does, so no signal handler's raise can land inside `Popen` or where Python
547
+ drops it. `mcmd` learns that stdout has no reader only when a write to it fails: a command that prints nothing after
548
+ the reader quit (`mcmd 'sleep 4' | head -2`, a `-q` run, one under `-t`) runs to its end first, as it did, and then
549
+ `mcmd` ends 141. Planned for 1.62: asking the pipe instead of waiting for a write.
550
+ - Changed: a closed stderr with a working stdout can now end `mcmd` by SIGPIPE (141) when `mcmd` itself prints an error
551
+ there; it ended 120 with 1.60. Measured: on 3.9 and 3.10 an unbalanced quote, a usage error and "no commands given" all
552
+ end 141; on 3.12 and 3.14 only the unbalanced quote does, as argparse's own errors stay 120; on 3.6 nothing changes
553
+ (all 120). The error of a command that cannot run ends as before on every Python.
554
+ - Fix, Python 3.6 to 3.10: a stop (a Ctrl-C, `stop()`, `kill_all(signal.SIGINT)`) that lands as a command starts no
555
+ longer crashes the exec wrapper's Python. A stop sends SIGINT first, and a Python that SIGINT reaches while it
556
+ initializes aborts with a core dump on 3.6 and 3.7 ("Fatal Python error: ... can't initialize sys standard streams")
557
+ and exits 1 with that message on 3.8 to 3.10; a little later in its start it prints a `KeyboardInterrupt` traceback.
558
+ Each landed in that command's stderr, and each core dump in the host's crash handler (systemd-coredump, apport, abrt).
559
+ Aimed at random into 400 starts each with 1.60: 80 aborted on 3.6 (1.59 the same) and 63 on 3.7; 54, 41 and 42 exited
560
+ with "Fatal Python error" on 3.8, 3.9 and 3.10; with 1.61 every one ended by SIGINT or ran to its end, without a
561
+ word. It is not new: every version with the exec wrapper (1.52 on) had it. The wrapper now starts with SIGINT
562
+ blocked and unblocks it once it has the command, so such a stop ends it by SIGINT, as it ends the command; a command
563
+ still inherits SIGINT blocked from a calling thread that has it blocked. On GitHub's runners the core dumps of
564
+ 1.60's new stop test (5,000 stops in one process) each started an apport process, which drove the load to 250 and
565
+ timed the 3.6 job out.
566
+ - Known: on Python 3.6 to 3.10 a Ctrl-C that arrives as a command starts waits for the exec wrapper's `Popen` (the fork,
567
+ the exec of the Python binary, the read of its error pipe), normally about 1 ms (2 to 5 ms at p99), where 1.60 stopped
568
+ at once. If the exec of the Python binary itself hangs (a venv on an NFS mount that stopped answering), Ctrl-C does
569
+ nothing until the mount answers; 1.60 exited 130. See [docs/limitations.md](docs/limitations.md).
570
+ - Known: a Ctrl-C that lands in `Popen`'s cleanup (`Popen.__del__`) of a command that has finished, in a serial run, is
571
+ dropped (12 of about 6,000 aimed Ctrl-Cs with 1.60, 3 with 1.61): Python prints "Exception ignored" and the run goes
572
+ on; press it again. It is the dropped Ctrl-C of 1.60's note below, in serial runs too. See
573
+ [docs/limitations.md](docs/limitations.md).
574
+
525
575
  ## Upgrading to 1.60 from 1.59
526
576
 
527
577
  - Nothing needs doing to upgrade.
@@ -93,6 +93,7 @@ exit status:
93
93
  N several commands: how many failed, capped at 101
94
94
  2 usage error
95
95
  130 interrupted with Ctrl-C ( 128+S after SIGTERM / SIGHUP )
96
+ 141 stdout closed ( `mcmd ... | head` ): commands stopped, ended by SIGPIPE ( 1 on Windows, untested there )
96
97
  ```
97
98
 
98
99
  ### Examples
@@ -166,6 +167,7 @@ mcmd --shell -m 4 "dmesg | tail -n 5" "journalctl -n 5 > /tmp/j.log"
166
167
  | Several commands | the number that failed, capped at `101` |
167
168
  | Usage error (bad option, unbalanced quote, no commands) | `2` |
168
169
  | Ctrl-C | `130` (`128+N` after SIGTERM / SIGHUP) |
170
+ | stdout has no reader (`mcmd ... \| head`) | ended by SIGPIPE, which a shell reports as `141`; `1` on Windows (untested there) |
169
171
 
170
172
  ### Input files and templates
171
173
 
@@ -415,7 +417,11 @@ Signals sent to your program's group do not reach the commands, so multiCMD stop
415
417
  Ctrl-C (or a SIGTERM handler's `sys.exit()`) in a call waiting on the main thread, `join_threads()`
416
418
  and `AsyncExecutor.wait()` included, stops every command, skips the queued ones and
417
419
  re-raises, giving them up to 5 s to exit; a second Ctrl-C ends that wait at once. `mcmd` stops its
418
- commands on SIGTERM / SIGHUP (unless started with them ignored, as under `nohup`). At interpreter exit
420
+ commands on SIGTERM / SIGHUP (unless started with them ignored, as under `nohup`), and when its stdout
421
+ has no reader (`mcmd ... | head`): it stops them as a Ctrl-C does, and ends by SIGPIPE, as `yes | head` does, with
422
+ nothing on stderr. It learns that stdout has no reader only when a write to it fails, so a command that prints nothing
423
+ more runs to its end first (`mcmd 'sleep 4' | head -2` takes 4 s, as `sleep 4 | head -2` does). A program that imports
424
+ multiCMD still gets the `BrokenPipeError`. At interpreter exit
419
425
  the commands still running are killed, and a command queued on one of multiCMD's threads fails (-1,
420
426
  "not started: the interpreter is exiting") instead of outliving the program; one run from an `atexit`
421
427
  handler after that, on the main thread, runs as usual. A program that handles SIGTERM itself, or runs
@@ -444,7 +450,11 @@ on any thread). Measured at 0.02 to 0.42 % of the Ctrl-Cs that land during a 0.2
444
450
  1.59. In the first milliseconds of a fire-and-forget batch (`wait_for_return=False`) a first Ctrl-C can also be
445
451
  dropped, about 1 in 700 of those that land there on 3.12 and 3.14 (1.59: 1 in 1,400, and it hung on 3.14): Python
446
452
  ignores an exception raised in a weakref callback and prints "Exception ignored", the program goes on, and the next
447
- Ctrl-C works.
453
+ Ctrl-C works. A serial run can drop one too, in `Popen`'s cleanup (`Popen.__del__`) of a command that has finished (12 of
454
+ about 6,000 aimed Ctrl-Cs with 1.60, 3 with 1.61): the same message, and the next Ctrl-C works. On Python 3.6-3.10 a
455
+ Ctrl-C that arrives as a command starts waits for the exec wrapper's `Popen`, normally about 1 ms (2-5 ms at p99); if the
456
+ exec of the Python binary itself hangs (a venv on an NFS mount that stopped answering), it does nothing until the mount
457
+ answers.
448
458
  These and the other known limits are in [docs/limitations.md](docs/limitations.md).
449
459
 
450
460
  ## Key functions and objects
@@ -496,6 +506,46 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
496
506
 
497
507
  `test_expand_ranges_perf.py` is a manual performance microbenchmark and is not collected by pytest.
498
508
 
509
+ ## Upgrading to 1.61 from 1.60
510
+
511
+ - Nothing needs doing to upgrade.
512
+ - Fix: `mcmd ... | head` (a reader that quits early) ends quietly. It printed a traceback or an "Exception ignored" message
513
+ and exited 120 (Python's status for a stdout it cannot flush), and the commands were left to run on: a quiet one,
514
+ or one that ignores SIGPIPE, ran to its end or for ever, with `mcmd` waiting for it. Now `mcmd` stops the commands as a
515
+ Ctrl-C does, and ends by SIGPIPE, as `yes | head` does: a shell reports 141, and stderr stays empty (exit status 1 on
516
+ Windows, untested: a closed pipe may show there as an `OSError` that `mcmd` does not catch yet, planned for 1.62). It
517
+ covers the commands' output, the summary, `--dry-run`, `-h` and `-V`. The library's functions still raise
518
+ `BrokenPipeError` when `sys.stdout` fails. A thread that finds stdout closed only says so, and the main thread's
519
+ waits stop the commands at their next step (at most 0.1 s later), while a command not started yet is skipped;
520
+ `mcmd` leaves SIGPIPE ignored, as Python does, so no signal handler's raise can land inside `Popen` or where Python
521
+ drops it. `mcmd` learns that stdout has no reader only when a write to it fails: a command that prints nothing after
522
+ the reader quit (`mcmd 'sleep 4' | head -2`, a `-q` run, one under `-t`) runs to its end first, as it did, and then
523
+ `mcmd` ends 141. Planned for 1.62: asking the pipe instead of waiting for a write.
524
+ - Changed: a closed stderr with a working stdout can now end `mcmd` by SIGPIPE (141) when `mcmd` itself prints an error
525
+ there; it ended 120 with 1.60. Measured: on 3.9 and 3.10 an unbalanced quote, a usage error and "no commands given" all
526
+ end 141; on 3.12 and 3.14 only the unbalanced quote does, as argparse's own errors stay 120; on 3.6 nothing changes
527
+ (all 120). The error of a command that cannot run ends as before on every Python.
528
+ - Fix, Python 3.6 to 3.10: a stop (a Ctrl-C, `stop()`, `kill_all(signal.SIGINT)`) that lands as a command starts no
529
+ longer crashes the exec wrapper's Python. A stop sends SIGINT first, and a Python that SIGINT reaches while it
530
+ initializes aborts with a core dump on 3.6 and 3.7 ("Fatal Python error: ... can't initialize sys standard streams")
531
+ and exits 1 with that message on 3.8 to 3.10; a little later in its start it prints a `KeyboardInterrupt` traceback.
532
+ Each landed in that command's stderr, and each core dump in the host's crash handler (systemd-coredump, apport, abrt).
533
+ Aimed at random into 400 starts each with 1.60: 80 aborted on 3.6 (1.59 the same) and 63 on 3.7; 54, 41 and 42 exited
534
+ with "Fatal Python error" on 3.8, 3.9 and 3.10; with 1.61 every one ended by SIGINT or ran to its end, without a
535
+ word. It is not new: every version with the exec wrapper (1.52 on) had it. The wrapper now starts with SIGINT
536
+ blocked and unblocks it once it has the command, so such a stop ends it by SIGINT, as it ends the command; a command
537
+ still inherits SIGINT blocked from a calling thread that has it blocked. On GitHub's runners the core dumps of
538
+ 1.60's new stop test (5,000 stops in one process) each started an apport process, which drove the load to 250 and
539
+ timed the 3.6 job out.
540
+ - Known: on Python 3.6 to 3.10 a Ctrl-C that arrives as a command starts waits for the exec wrapper's `Popen` (the fork,
541
+ the exec of the Python binary, the read of its error pipe), normally about 1 ms (2 to 5 ms at p99), where 1.60 stopped
542
+ at once. If the exec of the Python binary itself hangs (a venv on an NFS mount that stopped answering), Ctrl-C does
543
+ nothing until the mount answers; 1.60 exited 130. See [docs/limitations.md](docs/limitations.md).
544
+ - Known: a Ctrl-C that lands in `Popen`'s cleanup (`Popen.__del__`) of a command that has finished, in a serial run, is
545
+ dropped (12 of about 6,000 aimed Ctrl-Cs with 1.60, 3 with 1.61): Python prints "Exception ignored" and the run goes
546
+ on; press it again. It is the dropped Ctrl-C of 1.60's note below, in serial runs too. See
547
+ [docs/limitations.md](docs/limitations.md).
548
+
499
549
  ## Upgrading to 1.60 from 1.59
500
550
 
501
551
  - Nothing needs doing to upgrade.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: multiCMD
3
- Version: 1.60
3
+ Version: 1.61
4
4
  Summary: Run commands simultaneously
5
5
  Home-page: https://github.com/yufei-pan/multiCMD
6
6
  Author: Yufei Pan
@@ -119,6 +119,7 @@ exit status:
119
119
  N several commands: how many failed, capped at 101
120
120
  2 usage error
121
121
  130 interrupted with Ctrl-C ( 128+S after SIGTERM / SIGHUP )
122
+ 141 stdout closed ( `mcmd ... | head` ): commands stopped, ended by SIGPIPE ( 1 on Windows, untested there )
122
123
  ```
123
124
 
124
125
  ### Examples
@@ -192,6 +193,7 @@ mcmd --shell -m 4 "dmesg | tail -n 5" "journalctl -n 5 > /tmp/j.log"
192
193
  | Several commands | the number that failed, capped at `101` |
193
194
  | Usage error (bad option, unbalanced quote, no commands) | `2` |
194
195
  | Ctrl-C | `130` (`128+N` after SIGTERM / SIGHUP) |
196
+ | stdout has no reader (`mcmd ... \| head`) | ended by SIGPIPE, which a shell reports as `141`; `1` on Windows (untested there) |
195
197
 
196
198
  ### Input files and templates
197
199
 
@@ -441,7 +443,11 @@ Signals sent to your program's group do not reach the commands, so multiCMD stop
441
443
  Ctrl-C (or a SIGTERM handler's `sys.exit()`) in a call waiting on the main thread, `join_threads()`
442
444
  and `AsyncExecutor.wait()` included, stops every command, skips the queued ones and
443
445
  re-raises, giving them up to 5 s to exit; a second Ctrl-C ends that wait at once. `mcmd` stops its
444
- commands on SIGTERM / SIGHUP (unless started with them ignored, as under `nohup`). At interpreter exit
446
+ commands on SIGTERM / SIGHUP (unless started with them ignored, as under `nohup`), and when its stdout
447
+ has no reader (`mcmd ... | head`): it stops them as a Ctrl-C does, and ends by SIGPIPE, as `yes | head` does, with
448
+ nothing on stderr. It learns that stdout has no reader only when a write to it fails, so a command that prints nothing
449
+ more runs to its end first (`mcmd 'sleep 4' | head -2` takes 4 s, as `sleep 4 | head -2` does). A program that imports
450
+ multiCMD still gets the `BrokenPipeError`. At interpreter exit
445
451
  the commands still running are killed, and a command queued on one of multiCMD's threads fails (-1,
446
452
  "not started: the interpreter is exiting") instead of outliving the program; one run from an `atexit`
447
453
  handler after that, on the main thread, runs as usual. A program that handles SIGTERM itself, or runs
@@ -470,7 +476,11 @@ on any thread). Measured at 0.02 to 0.42 % of the Ctrl-Cs that land during a 0.2
470
476
  1.59. In the first milliseconds of a fire-and-forget batch (`wait_for_return=False`) a first Ctrl-C can also be
471
477
  dropped, about 1 in 700 of those that land there on 3.12 and 3.14 (1.59: 1 in 1,400, and it hung on 3.14): Python
472
478
  ignores an exception raised in a weakref callback and prints "Exception ignored", the program goes on, and the next
473
- Ctrl-C works.
479
+ Ctrl-C works. A serial run can drop one too, in `Popen`'s cleanup (`Popen.__del__`) of a command that has finished (12 of
480
+ about 6,000 aimed Ctrl-Cs with 1.60, 3 with 1.61): the same message, and the next Ctrl-C works. On Python 3.6-3.10 a
481
+ Ctrl-C that arrives as a command starts waits for the exec wrapper's `Popen`, normally about 1 ms (2-5 ms at p99); if the
482
+ exec of the Python binary itself hangs (a venv on an NFS mount that stopped answering), it does nothing until the mount
483
+ answers.
474
484
  These and the other known limits are in [docs/limitations.md](docs/limitations.md).
475
485
 
476
486
  ## Key functions and objects
@@ -522,6 +532,46 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
522
532
 
523
533
  `test_expand_ranges_perf.py` is a manual performance microbenchmark and is not collected by pytest.
524
534
 
535
+ ## Upgrading to 1.61 from 1.60
536
+
537
+ - Nothing needs doing to upgrade.
538
+ - Fix: `mcmd ... | head` (a reader that quits early) ends quietly. It printed a traceback or an "Exception ignored" message
539
+ and exited 120 (Python's status for a stdout it cannot flush), and the commands were left to run on: a quiet one,
540
+ or one that ignores SIGPIPE, ran to its end or for ever, with `mcmd` waiting for it. Now `mcmd` stops the commands as a
541
+ Ctrl-C does, and ends by SIGPIPE, as `yes | head` does: a shell reports 141, and stderr stays empty (exit status 1 on
542
+ Windows, untested: a closed pipe may show there as an `OSError` that `mcmd` does not catch yet, planned for 1.62). It
543
+ covers the commands' output, the summary, `--dry-run`, `-h` and `-V`. The library's functions still raise
544
+ `BrokenPipeError` when `sys.stdout` fails. A thread that finds stdout closed only says so, and the main thread's
545
+ waits stop the commands at their next step (at most 0.1 s later), while a command not started yet is skipped;
546
+ `mcmd` leaves SIGPIPE ignored, as Python does, so no signal handler's raise can land inside `Popen` or where Python
547
+ drops it. `mcmd` learns that stdout has no reader only when a write to it fails: a command that prints nothing after
548
+ the reader quit (`mcmd 'sleep 4' | head -2`, a `-q` run, one under `-t`) runs to its end first, as it did, and then
549
+ `mcmd` ends 141. Planned for 1.62: asking the pipe instead of waiting for a write.
550
+ - Changed: a closed stderr with a working stdout can now end `mcmd` by SIGPIPE (141) when `mcmd` itself prints an error
551
+ there; it ended 120 with 1.60. Measured: on 3.9 and 3.10 an unbalanced quote, a usage error and "no commands given" all
552
+ end 141; on 3.12 and 3.14 only the unbalanced quote does, as argparse's own errors stay 120; on 3.6 nothing changes
553
+ (all 120). The error of a command that cannot run ends as before on every Python.
554
+ - Fix, Python 3.6 to 3.10: a stop (a Ctrl-C, `stop()`, `kill_all(signal.SIGINT)`) that lands as a command starts no
555
+ longer crashes the exec wrapper's Python. A stop sends SIGINT first, and a Python that SIGINT reaches while it
556
+ initializes aborts with a core dump on 3.6 and 3.7 ("Fatal Python error: ... can't initialize sys standard streams")
557
+ and exits 1 with that message on 3.8 to 3.10; a little later in its start it prints a `KeyboardInterrupt` traceback.
558
+ Each landed in that command's stderr, and each core dump in the host's crash handler (systemd-coredump, apport, abrt).
559
+ Aimed at random into 400 starts each with 1.60: 80 aborted on 3.6 (1.59 the same) and 63 on 3.7; 54, 41 and 42 exited
560
+ with "Fatal Python error" on 3.8, 3.9 and 3.10; with 1.61 every one ended by SIGINT or ran to its end, without a
561
+ word. It is not new: every version with the exec wrapper (1.52 on) had it. The wrapper now starts with SIGINT
562
+ blocked and unblocks it once it has the command, so such a stop ends it by SIGINT, as it ends the command; a command
563
+ still inherits SIGINT blocked from a calling thread that has it blocked. On GitHub's runners the core dumps of
564
+ 1.60's new stop test (5,000 stops in one process) each started an apport process, which drove the load to 250 and
565
+ timed the 3.6 job out.
566
+ - Known: on Python 3.6 to 3.10 a Ctrl-C that arrives as a command starts waits for the exec wrapper's `Popen` (the fork,
567
+ the exec of the Python binary, the read of its error pipe), normally about 1 ms (2 to 5 ms at p99), where 1.60 stopped
568
+ at once. If the exec of the Python binary itself hangs (a venv on an NFS mount that stopped answering), Ctrl-C does
569
+ nothing until the mount answers; 1.60 exited 130. See [docs/limitations.md](docs/limitations.md).
570
+ - Known: a Ctrl-C that lands in `Popen`'s cleanup (`Popen.__del__`) of a command that has finished, in a serial run, is
571
+ dropped (12 of about 6,000 aimed Ctrl-Cs with 1.60, 3 with 1.61): Python prints "Exception ignored" and the run goes
572
+ on; press it again. It is the dropped Ctrl-C of 1.60's note below, in serial runs too. See
573
+ [docs/limitations.md](docs/limitations.md).
574
+
525
575
  ## Upgrading to 1.60 from 1.59
526
576
 
527
577
  - Nothing needs doing to upgrade.
@@ -11,6 +11,7 @@ scripts/multiCMD
11
11
  tests/test_api.py
12
12
  tests/test_async_executor.py
13
13
  tests/test_cli.py
14
+ tests/test_cli_closed_stdout.py
14
15
  tests/test_cli_input.py
15
16
  tests/test_cli_summary.py
16
17
  tests/test_display.py
@@ -28,7 +28,7 @@ import traceback
28
28
  from pprint import pformat
29
29
 
30
30
  #%% global vars
31
- version = '1.60'
31
+ version = '1.61'
32
32
  __version__ = version
33
33
  COMMIT_DATE = '2026-11-11'
34
34
  # fire-and-forget threads (run_commands with wait_for_return=False) that join_threads() waits on. multiCMD takes no
@@ -63,7 +63,10 @@ _HAS_PROCESS_GROUP = sys.version_info >= (3, 11)
63
63
  # like included ). It runs the command only once the caller has it registered: its second argument is the read end of a
64
64
  # pipe ( passed to it alone ) on which the caller writes one byte then ( see _popen ). A caller interrupted inside Popen
65
65
  # once it has forked never has the command, and closes the pipe: the wrapper reads its end and exits 125 without running
66
- # anything. Its first argument is '-' ( the command inherits the wrapper's environment as it is ) or N:
66
+ # anything. It starts with SIGINT blocked, as a Python that a SIGINT reaches while it initializes aborts with a core dump
67
+ # ( 3.6, 3.7 ) or exits 1 with 'Fatal Python error' ( 3.8 - 3.10 ), and a stop sends SIGINT first: the byte b'2' has it
68
+ # unblock SIGINT ( b'1': the caller had it blocked, and so does the command ).
69
+ # Its first argument is '-' ( the command inherits the wrapper's environment as it is ) or N:
67
70
  # the command's own environment is then the N NAME=VALUE entries in the wrapper's variables
68
71
  # _MULTICMD_ENV_0 .. _MULTICMD_ENV_<N-1>, handed over by exec itself ( nothing can be cut short ), and it
69
72
  # runs with exactly those, found in their PATH like Python 3.11+'s Popen does. A missing or malformed entry
@@ -81,8 +84,14 @@ _PGROUP_EXEC = (
81
84
  "a=sys.argv\n"
82
85
  # handed over or not: the byte, or the end of the pipe ( 125, as env and timeout fail on their own )
83
86
  "h=int(a[2])\n"
84
- "if not posix.read(h,1):posix._exit(125)\n"
87
+ "b=posix.read(h,1)\n"
88
+ "if not b:posix._exit(125)\n"
85
89
  "posix.close(h)\n"
90
+ # b'2': SIGINT was blocked for the start only ( see _spawn_process ). A stop's SIGINT that came meanwhile ends the
91
+ # wrapper now, as it would end the command; an ignored SIGINT stays ignored
92
+ "if b==b'2':\n"
93
+ "\tif _signal.getsignal(_signal.SIGINT)!=_signal.SIG_IGN:_signal.signal(_signal.SIGINT,_signal.SIG_DFL)\n"
94
+ "\t_signal.pthread_sigmask(_signal.SIG_UNBLOCK,(_signal.SIGINT,))\n"
86
95
  "c=a[3]\n"
87
96
  "v=None\n"
88
97
  "if a[1]!='-':\n"
@@ -428,6 +437,14 @@ def _check_started(threads):
428
437
  if not isinstance(thread, _Thread) and thread.ident is None:
429
438
  raise RuntimeError('cannot join thread before it is started')
430
439
 
440
+ class _StdoutClosed(BaseException):
441
+ '''
442
+ The CLI's stdout has no reader any more ( `mcmd ... | head` ): raised on the main thread, where multiCMD takes it as
443
+ any interrupt a signal handler raises ( every command stopped ) and main() ends the process ( internal )
444
+ It is raised by the main thread's own code, never by a signal handler: a handler's raise lands wherever the main thread
445
+ is, inside Popen once it has forked, or in a __del__ or a weakref callback, where Python drops it, and the stop is lost.
446
+ '''
447
+
431
448
  class _Display:
432
449
  '''
433
450
  Writes multiCMD's live output to sys.stdout, one write per call under one lock ( internal )
@@ -435,11 +452,35 @@ class _Display:
435
452
  Command output lines are written newline-first, so the cursor stays at the end of the last
436
453
  line. redrawable is True when that last line ended with '\r': the next line that replaces a
437
454
  '\r' line, from any command, then overwrites it in place, and anything else starts a new line.
455
+
456
+ A write that finds stdout without a reader ( BrokenPipeError ) raises it, as any write does, unless on_closed is set,
457
+ as the CLI does ( main() ): then it is closed for good, on_closed is called once, in the thread that found it, and
458
+ every later write is dropped.
459
+ @attributes:
460
+ on_closed: Called, without arguments, by the thread whose write found stdout closed ( None: the write raises )
461
+ closed: Whether a write found stdout closed with on_closed set
438
462
  '''
439
463
  def __init__(self):
440
464
  self.lock = threading.Lock()
441
465
  self.redrawable = False
442
466
  self.at_line_start = True
467
+ self.on_closed = None
468
+ self.closed = False
469
+
470
+ def _write(self, text):
471
+ '''
472
+ Write text to stdout and flush it, with the lock held ( internal )
473
+ '''
474
+ if self.closed:
475
+ return
476
+ try:
477
+ sys.stdout.write(text)
478
+ sys.stdout.flush()
479
+ except BrokenPipeError:
480
+ if self.on_closed is None:
481
+ raise
482
+ self.closed = True
483
+ self.on_closed()
443
484
 
444
485
  def lines(self, texts, decor=('', '', ''), replace_first=False, ends_with_cr=False):
445
486
  '''
@@ -460,8 +501,7 @@ class _Display:
460
501
  parts.append('\n' + prefix + start + text + end)
461
502
  self.redrawable = ends_with_cr
462
503
  self.at_line_start = False
463
- sys.stdout.write(''.join(parts))
464
- sys.stdout.flush()
504
+ self._write(''.join(parts))
465
505
 
466
506
  def message(self, text, decor=('', '', '')):
467
507
  '''
@@ -474,8 +514,7 @@ class _Display:
474
514
  with self.lock:
475
515
  lead = '' if self.at_line_start else '\n'
476
516
  body = '\n'.join(prefix + start + line + end for line in text.split('\n'))
477
- sys.stdout.write(lead + body + '\n')
478
- sys.stdout.flush()
517
+ self._write(lead + body + '\n')
479
518
  self.redrawable = False
480
519
  self.at_line_start = True
481
520
 
@@ -488,6 +527,17 @@ class _Display:
488
527
 
489
528
  _display = _Display()
490
529
 
530
+ def _raise_if_stdout_closed():
531
+ '''
532
+ Raise _StdoutClosed in the main thread if a write found stdout closed, as the CLI's waits for its commands do ( internal )
533
+ A reader or pool worker that finds it only sets _display.closed ( see _stdout_closed ); the main thread, which is the one
534
+ that stops the commands, looks at each step of its waits ( _watch, _drain, the pool's join loop ). The waits of a stop do
535
+ not look: it is raised once, before the stop begins. A command not started yet is skipped meanwhile ( __run_command: before
536
+ its header and again after it, as that write raises nothing on a worker thread ).
537
+ '''
538
+ if _display.closed and _thread.get_ident() == threading.main_thread().ident:
539
+ raise _StdoutClosed()
540
+
491
541
  class AsyncExecutor:
492
542
  def __init__(self, max_threads=1,semaphore=...,timeout=0,quiet=True,dry_run=False,parse=False,shell=False,prefix=False,color=None,pty=False,env=None,extra_env=None,pass_fds=()):
493
543
  '''
@@ -1249,7 +1299,7 @@ def _handover_pipe():
1249
1299
  os.close(fd)
1250
1300
  return r, w
1251
1301
 
1252
- def _popen(argv, started=None, grouped=False, handover=None, **kwargs):
1302
+ def _popen(argv, started=None, grouped=False, handover=None, go=b'1', **kwargs):
1253
1303
  '''
1254
1304
  Start a process with subprocess.Popen ( internal )
1255
1305
  @params:
@@ -1261,6 +1311,7 @@ def _popen(argv, started=None, grouped=False, handover=None, **kwargs):
1261
1311
  handover: For the exec wrapper, the index in argv of its pipe's read end, filled in here; None otherwise. One
1262
1312
  byte on the pipe, once the command is in started, lets the wrapper run it; both ends are closed here however
1263
1313
  Popen ends, and a wrapper that got no byte exits 125 ( see _PGROUP_EXEC )
1314
+ go: That byte: b'2' has the wrapper unblock SIGINT first, b'1' leaves the mask as it is
1264
1315
  @returns:
1265
1316
  ( subprocess.Popen, int | None ): The process and its process group id ( None without a group of its own )
1266
1317
  '''
@@ -1276,7 +1327,7 @@ def _popen(argv, started=None, grouped=False, handover=None, **kwargs):
1276
1327
  started.append(command)
1277
1328
  if pipe is not None:
1278
1329
  # last in the try: a Ctrl-C that Python 3.6 - 3.9 raise between a try and its finally comes after it
1279
- os.write(pipe[1], b'1')
1330
+ os.write(pipe[1], go)
1280
1331
  finally:
1281
1332
  if pipe is not None:
1282
1333
  os.close(pipe[0])
@@ -1292,7 +1343,8 @@ def _spawn_process(argv, stdout, stderr, env=None, explicit=False, executable=No
1292
1343
  terminal from its background group the read fails with EIO instead of the kernel stopping it.
1293
1344
  The calling thread's mask is set back as it was however the start ends, a Ctrl-C anywhere included.
1294
1345
  Python 3.11+ uses Popen(process_group=0); older Pythons run the command through the exec
1295
- wrapper _PGROUP_EXEC. Windows gets a plain Popen.
1346
+ wrapper _PGROUP_EXEC, which also starts with SIGINT blocked, until it has the command: a stop's
1347
+ SIGINT could otherwise reach its Python as it initializes. Windows gets a plain Popen.
1296
1348
 
1297
1349
  The wrapper itself always starts in this process's environment. A command given an environment of
1298
1350
  its own ( explicit ) gets it as reserved variables of the wrapper's, which the wrapper unpacks and
@@ -1348,9 +1400,13 @@ def _spawn_process(argv, stdout, stderr, env=None, explicit=False, executable=No
1348
1400
  # the mask as it is, read with a call that changes nothing; the block is in the try, as a Ctrl-C can come out of it
1349
1401
  # with the mask already set ( see _sigmask )
1350
1402
  old_mask = _sigmask(signal.SIG_BLOCK, ())
1403
+ block, go = _TTY_SIGNALS, b'1'
1404
+ if handover is not None and signal.SIGINT not in old_mask:
1405
+ # the wrapper's Python starts with SIGINT blocked and unblocks it once handed over ( see _PGROUP_EXEC )
1406
+ block, go = _TTY_SIGNALS | {signal.SIGINT}, b'2'
1351
1407
  try:
1352
- _sigmask(signal.SIG_BLOCK, _TTY_SIGNALS)
1353
- command = _popen(argv, started, grouped, handover, **kwargs)
1408
+ _sigmask(signal.SIG_BLOCK, block)
1409
+ command = _popen(argv, started, grouped, handover, go, **kwargs)
1354
1410
  # ( here too: Python 3.6 - 3.9 can raise a Ctrl-C between the end of a try and its finally, which then never runs )
1355
1411
  _sigmask(signal.SIG_SETMASK, old_mask)
1356
1412
  finally:
@@ -1711,6 +1767,7 @@ def _watch(task, proc, timeout):
1711
1767
  while proc.poll() is None: # while the process is still running
1712
1768
  if task.stop:
1713
1769
  return 'stop'
1770
+ _raise_if_stdout_closed()
1714
1771
  if timeout > 0 and time.monotonic() - task._last_output > timeout:
1715
1772
  return 'timeout'
1716
1773
  time.sleep(sleep_time)
@@ -1754,6 +1811,7 @@ def _drain(task, readers, timeout):
1754
1811
  while _any_alive(readers):
1755
1812
  if task.stop:
1756
1813
  return 'stop'
1814
+ _raise_if_stdout_closed()
1757
1815
  for reader in readers:
1758
1816
  _join(reader, 0.05)
1759
1817
  # tested after a step, not on entry: a command that exits just past the threshold, before _watch() looked
@@ -1974,10 +2032,11 @@ def _execute(task, timeout, quiet, decor, use_pty=False):
1974
2032
  running in the background on purpose.
1975
2033
  Sets task.returncode ( 124 after a timeout ), start_time and end_time. A KeyboardInterrupt is re-raised once the
1976
2034
  command is dead, the task finalized as stopped. So is anything else that is not an Exception ( a SIGTERM handler's
1977
- SystemExit ), and, once the command runs, whatever a signal handler raises: multiCMD then only waits for it
1978
- ( _watch, _drain: polls, sleeps and joins, no I/O of their own ). An Exception raised while it starts is a failure
1979
- of multiCMD's: the command is stopped and the caller records it ( -1 ). A second interrupt ends the wait for the
1980
- stop: the command is sent SIGTERM and stays registered, for kill_all() and the exit hook.
2035
+ SystemExit, the CLI's _StdoutClosed from _watch or _drain ), and, once the command runs, whatever a signal handler
2036
+ raises: multiCMD then only waits for it ( _watch, _drain: polls, sleeps and joins, no I/O of their own ). An
2037
+ Exception raised while it starts is a failure of multiCMD's: the command is stopped and the caller records it ( -1 ).
2038
+ A second interrupt ends the wait for the stop: the command is sent SIGTERM and stays registered, for kill_all() and
2039
+ the exit hook.
1981
2040
  The command is registered from the moment Popen has returned it ( see _popen ). An interrupt that Python raises
1982
2041
  inside Popen, once it has forked, loses it. Through the exec wrapper ( Python < 3.11 ) it then never runs: the
1983
2042
  wrapper waits until the command is registered. With Popen(process_group=) ( 3.11+ ) it runs on in a process group of
@@ -2145,8 +2204,9 @@ def __run_command(task,sem, timeout=60, quiet=False,dry_run=False,with_stdErr=Fa
2145
2204
  list[str]: The output of the command
2146
2205
  '''
2147
2206
  with sem:
2148
- if task.stop:
2149
- # stopped before it started: it stays unfinished, like a skipped queued command
2207
+ if task.stop or _display.closed:
2208
+ # stopped before it started, or stdout was found closed ( the CLI: its stop is on the way ): it stays unfinished,
2209
+ # like a skipped queued command
2150
2210
  task._env = task._extra_env = None
2151
2211
  return task.stdout + task.stderr if with_stdErr else task.stdout
2152
2212
  try:
@@ -2162,6 +2222,11 @@ def __run_command(task,sem, timeout=60, quiet=False,dry_run=False,with_stdErr=Fa
2162
2222
  if not quiet:
2163
2223
  _display.message('Running command: ' + _quote_command(task.command), decor)
2164
2224
  _display.message('-' * 100, decor)
2225
+ if _display.closed:
2226
+ # one of those writes found stdout closed, on a worker thread, where it raises nothing ( 1.60 raised, and the
2227
+ # start never happened ): skipped as at the top, unfinished ( the finally below marks it finalized, which
2228
+ # nothing reads for a task the CLI skips )
2229
+ return task.stdout + task.stderr if with_stdErr else task.stdout
2165
2230
  _execute(task, timeout, quiet, decor, use_pty)
2166
2231
  except Exception as e:
2167
2232
  if task._finalized:
@@ -2531,7 +2596,8 @@ def _run_pool(tasks, decors, sem, timeout, quiet, dry_run, max_threads, use_pty=
2531
2596
  # commands that must run at the same time may then wait on each other for good: say so
2532
2597
  _write_error(f'multiCMD: started {len(running)} of {wanted} threads: {error}')
2533
2598
  for thread in running:
2534
- _join(thread)
2599
+ while not _join(thread, 0.1):
2600
+ _raise_if_stdout_closed()
2535
2601
  except BaseException:
2536
2602
  # a Ctrl-C ( or anything else ) while the workers start or run: the commands not started yet never will
2537
2603
  _stop_tasks(tasks)
@@ -2752,7 +2818,8 @@ _EXIT_STATUS_HELP = '''exit status:
2752
2818
  N one command: its own exit code ( killed by signal S: 128+S )
2753
2819
  N several commands: how many failed, capped at 101
2754
2820
  2 usage error
2755
- 130 interrupted with Ctrl-C ( 128+S after SIGTERM / SIGHUP )'''
2821
+ 130 interrupted with Ctrl-C ( 128+S after SIGTERM / SIGHUP )
2822
+ 141 stdout closed ( `mcmd ... | head` ): commands stopped, ended by SIGPIPE ( 1 on Windows, untested there )'''
2756
2823
 
2757
2824
  def _exit_status(tasks):
2758
2825
  '''
@@ -2775,6 +2842,8 @@ def _install_cli_signal_handlers():
2775
2842
  SIGTTIN / SIGTTOU are ignored, which every command inherits through exec and through any
2776
2843
  shell ( dash clears the blocked mask _spawn sets ), so a command that reads the terminal from
2777
2844
  its background group fails at once instead of hanging.
2845
+ SIGPIPE stays ignored, as Python leaves it, and no handler of ours raises for it: the main thread would raise wherever it is,
2846
+ and a raise Python drops ( in a __del__ ) loses the stop ( see _StdoutClosed, _raise_if_stdout_closed ).
2778
2847
  '''
2779
2848
  def handler(signum, frame):
2780
2849
  kill_all(signum)
@@ -2786,6 +2855,37 @@ def _install_cli_signal_handlers():
2786
2855
  for signum in _TTY_SIGNALS:
2787
2856
  signal.signal(signum, signal.SIG_IGN)
2788
2857
 
2858
+ def _stdout_closed():
2859
+ '''
2860
+ What the CLI does when stdout has no reader ( _Display.on_closed ), in the thread whose write found it ( internal )
2861
+ The main thread raises _StdoutClosed. Another thread has set _display.closed by now, and that is all it does: the
2862
+ main thread's waits look at it ( _raise_if_stdout_closed ).
2863
+ '''
2864
+ if _thread.get_ident() == threading.main_thread().ident:
2865
+ raise _StdoutClosed()
2866
+
2867
+ def _end_by_closed_stdout():
2868
+ '''
2869
+ End the process as one that wrote to a closed pipe ends, as `yes | head` does: by SIGPIPE, which a shell reports as
2870
+ 141; Windows has no SIGPIPE: exit status 1 ( internal )
2871
+ The run has stopped its commands by now ( _StdoutClosed went through it as any interrupt ); the exit hook
2872
+ ( _kill_running_at_exit ) stops what is left, as when the interpreter exits. Then nothing
2873
+ runs: no traceback, and no flush of the output nobody reads, which Python would report as "Exception ignored" and
2874
+ exit status 120. signal.signal() needs the main thread, which is where main() calls this.
2875
+ '''
2876
+ _kill_running_at_exit()
2877
+ try:
2878
+ sys.stderr.flush()
2879
+ except Exception:
2880
+ pass
2881
+ sigpipe = getattr(signal, 'SIGPIPE', None)
2882
+ if sigpipe is None:
2883
+ os._exit(1)
2884
+ signal.signal(sigpipe, signal.SIG_DFL)
2885
+ os.kill(os.getpid(), sigpipe)
2886
+ # not reached, unless the calling thread blocks SIGPIPE: then the status alone says so
2887
+ os._exit(128 + sigpipe)
2888
+
2789
2889
  _RETURNCODE_LABELS = {124: 'timeout', 126: 'cannot execute', 127: 'not found'}
2790
2890
 
2791
2891
  def _format_returncode(task):
@@ -2905,6 +3005,35 @@ def _cli_commands(args):
2905
3005
  return commands
2906
3006
 
2907
3007
  def main():
3008
+ '''
3009
+ The command line ( see _run_cli ). Output that nobody reads any more ( `mcmd ... | head` ) is taken as an interrupt: every
3010
+ command is stopped, as a Ctrl-C stops them, and the process ends by SIGPIPE with nothing on stderr
3011
+ ( _end_by_closed_stdout ). The library's functions keep raising BrokenPipeError as they did.
3012
+ @returns:
3013
+ int: The exit status
3014
+ '''
3015
+ _display.on_closed = _stdout_closed
3016
+ try:
3017
+ try:
3018
+ status = _run_cli()
3019
+ finally:
3020
+ # what argparse's -h and -V left in the buffer: Python's own flush at exit would fail with a message and status 120
3021
+ if sys.stdout is not None:
3022
+ try:
3023
+ sys.stdout.flush()
3024
+ except BrokenPipeError:
3025
+ raise
3026
+ except OSError:
3027
+ # a full disk, an I/O error: Python's own flush at exit says so, as it did before the CLI flushed itself
3028
+ pass
3029
+ if _display.closed:
3030
+ # a reader found it closed as the run ended, and no wait looked again
3031
+ raise _StdoutClosed()
3032
+ except (BrokenPipeError, _StdoutClosed):
3033
+ _end_by_closed_stdout()
3034
+ return status
3035
+
3036
+ def _run_cli():
2908
3037
  parser = argparse.ArgumentParser(description='Run multiple commands in parallel', epilog=_EXIT_STATUS_HELP,
2909
3038
  formatter_class=argparse.RawDescriptionHelpFormatter)
2910
3039
  parser.add_argument('commands', metavar='command', type=str, nargs='*',help='commands to run ( with -a: templates, {} is replaced by each input line )')