Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .agents/skills/shell-codestyle/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ This applies to every shell and Python script. `CODESTYLE.md` "Interrupts in Pyt
<!-- include: CODESTYLE.md > Interrupts in Shell -->

- **Cleanup of temporary paths and locks runs in the script's `EXIT` trap, set before the `INT` and `TERM` traps.** Bash runs that trap when SIGINT or SIGTERM ends the script, as well as on a normal exit. Set after the `TERM` trap, it does not run when that trap's re-sent SIGTERM ends the script.
- **Every script sets an `INT` trap and a `TERM` trap, which stop its children and re-raise the signal.** Each one sends SIGTERM with `kill` to the children the script started, then waits for them with `wait`. It then prints the line `CODESTYLE.md` "Interrupts" requires. Last, it clears its own trap with `trap - INT` or `trap - TERM` and re-sends the signal to the script with `kill -s INT "$$"` or `kill -s TERM "$$"`. It ends with `exit 130` or `exit 143`, which runs only where the re-sent signal does not end the script, such as PID 1 in a container. The children's process IDs are held in an array, such as `pids`, set to `()` before the traps are set. A trap skips the children's `kill` and the `wait` while `${#pids[@]}` is 0, a test `set -u` allows on an empty array in every Bash version. Under `set -u`, an unset one would end the trap before the line prints. The children's `kill` takes `2>/dev/null`, since a child that already exited makes it print an error. Both take `|| :`, since under `set -e` a non-zero status from either ends the trap before the line prints. SIGTERM is the signal sent because a background child ignores SIGINT. Other cleanup stays in the `EXIT` trap. A script that the `exec` case in `CODESTYLE.md` "Interrupts" frees from owing a trap sets neither trap.
- **Every script sets an `INT` trap and a `TERM` trap, which stop its children and re-raise the signal.** Each one sends SIGTERM with `kill` to the children the script started, then waits for them for at most a bound, such as 10 seconds. A child that ignores SIGTERM would otherwise hold the trap, so at Ctrl-C the script would hang. It polls them with `kill -0`, which succeeds while any of them still runs, and runs `sleep 0.1` between polls. Where one still runs at the bound, it sends SIGKILL to them with `kill -s KILL`. Then `wait "${pids[@]}"` reaps them. A bare `wait` would also wait on every process substitution, which the bound never reaches. It then prints the line `CODESTYLE.md` "Interrupts" requires. Last, it clears its own trap with `trap - INT` or `trap - TERM` and re-sends the signal to the script with `kill -s INT "$$"` or `kill -s TERM "$$"`. It ends with `exit 130` or `exit 143`, which runs only where the re-sent signal does not end the script, such as PID 1 in a container. The children's process IDs are held in an array, such as `pids`, set to `()` before the traps are set. Each child's ID is stored at an index that only increases, as `pids[n]=$!` then `n=$((n + 1))`, with `n` starting at 0. Once the script's own `wait` on that child returns, `unset 'pids[i]'` removes its ID, `i` being the index it was stored at. The array's length cannot stand in for `n`, since an `unset` leaves the array sparse. A trap then never signals a process ID the system may have reused. A trap skips stopping the children while `${#pids[@]}` is 0, a test `set -u` allows on an empty array in every Bash version. Under `set -u`, an unset one would end the trap before the line prints. Each `kill` sent to the children takes `2>/dev/null`, since a child that already exited makes it print an error. Each `kill` but the polling one takes `|| :`, and so do the `sleep` and the `wait`. Under `set -e`, a non-zero status from any of them would end the trap before the line prints. A second Ctrl-C ends the `sleep` with one. The polling `kill -0` is the loop's condition, which `set -e` does not read. SIGTERM is the signal sent because a background child ignores SIGINT. Other cleanup stays in the `EXIT` trap. A script that the `exec` case in `CODESTYLE.md` "Interrupts" frees from owing a trap sets neither trap.
- **A long-running external command runs in the background, and the script waits for it with `wait "$pid"`, so its exit status reaches `set -e`.** Bash runs a trap only after a foreground command returns, while a trapped signal ends a `wait` at once. Only the script's own shell starts such a child. One that a subshell or a pipeline element starts never reaches the script's array or its `wait`. A background child that reads standard input takes `<&0`, since bash otherwise gives it `/dev/null`. The trap's `kill` reaches only the process `$!` names. So a function, a group, a subshell, a pipeline, or `sh -c` runs in the foreground instead. There the trap runs once the command it interrupted returns, so nothing the script started is orphaned. A launcher, such as `uvx`, `uv run`, `uv pip install`, `dotnet`, or `xargs`, may run in the foreground too. One that does not stop the tool it starts on SIGTERM must run in the foreground. `uv pip install` is one when it builds an editable install or an sdist through a build backend that outlives uv on SIGTERM. A background child starts with SIGINT ignored, and the tool a launcher starts inherits that. So at Ctrl-C the tool runs on until the trap's SIGTERM reaches it through the launcher. In the foreground, Ctrl-C reaches the whole process group, the tool included. A SIGTERM sent to the script alone runs its `TERM` trap only once that foreground command returns. So a stop that must be prompt signals the process group. That group holds only the script and its command where the script leads it, as `setsid` starts one. The `exec` case in `CODESTYLE.md` "Interrupts" is the exception. The process a script `exec`s into replaces it, and a child handed across that `exec` is not waited for.
- **A deliberately POSIX `#!/bin/sh` script, such as a git hook, adapts the bullets above to what dash lacks.** Dash runs no `EXIT` trap when the re-sent signal ends the script. So the `INT` and `TERM` traps release the temporary paths and locks themselves, and the `EXIT` trap still covers an ordinary exit. A `/bin/sh` that is bash runs the `EXIT` trap after the signal trap too. So a release that cannot run twice, such as a lock's, sets its variable back to `""`. POSIX sh has no arrays, so the script runs one background child at a time. It holds that child's process ID in a scalar, such as `child`, set to `""` again after its `wait`. Every variable a trap reads, `child` and each temporary path included, is set to `""` before the traps are set. Under `set -u`, an unset one would end the trap before the signal is re-sent. A trap skips the child's `kill` and `wait` while `[ -n "$child" ]` fails, and skips the removal of a path still empty.

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22ec60627cbc6dab
ba7300272f1e14aa
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ This applies to every shell and Python script. `CODESTYLE.md` "Interrupts in Pyt
<!-- include: CODESTYLE.md > Interrupts in Shell -->

- **Cleanup of temporary paths and locks runs in the script's `EXIT` trap, set before the `INT` and `TERM` traps.** Bash runs that trap when SIGINT or SIGTERM ends the script, as well as on a normal exit. Set after the `TERM` trap, it does not run when that trap's re-sent SIGTERM ends the script.
- **Every script sets an `INT` trap and a `TERM` trap, which stop its children and re-raise the signal.** Each one sends SIGTERM with `kill` to the children the script started, then waits for them with `wait`. It then prints the line `CODESTYLE.md` "Interrupts" requires. Last, it clears its own trap with `trap - INT` or `trap - TERM` and re-sends the signal to the script with `kill -s INT "$$"` or `kill -s TERM "$$"`. It ends with `exit 130` or `exit 143`, which runs only where the re-sent signal does not end the script, such as PID 1 in a container. The children's process IDs are held in an array, such as `pids`, set to `()` before the traps are set. A trap skips the children's `kill` and the `wait` while `${#pids[@]}` is 0, a test `set -u` allows on an empty array in every Bash version. Under `set -u`, an unset one would end the trap before the line prints. The children's `kill` takes `2>/dev/null`, since a child that already exited makes it print an error. Both take `|| :`, since under `set -e` a non-zero status from either ends the trap before the line prints. SIGTERM is the signal sent because a background child ignores SIGINT. Other cleanup stays in the `EXIT` trap. A script that the `exec` case in `CODESTYLE.md` "Interrupts" frees from owing a trap sets neither trap.
- **Every script sets an `INT` trap and a `TERM` trap, which stop its children and re-raise the signal.** Each one sends SIGTERM with `kill` to the children the script started, then waits for them for at most a bound, such as 10 seconds. A child that ignores SIGTERM would otherwise hold the trap, so at Ctrl-C the script would hang. It polls them with `kill -0`, which succeeds while any of them still runs, and runs `sleep 0.1` between polls. Where one still runs at the bound, it sends SIGKILL to them with `kill -s KILL`. Then `wait "${pids[@]}"` reaps them. A bare `wait` would also wait on every process substitution, which the bound never reaches. It then prints the line `CODESTYLE.md` "Interrupts" requires. Last, it clears its own trap with `trap - INT` or `trap - TERM` and re-sends the signal to the script with `kill -s INT "$$"` or `kill -s TERM "$$"`. It ends with `exit 130` or `exit 143`, which runs only where the re-sent signal does not end the script, such as PID 1 in a container. The children's process IDs are held in an array, such as `pids`, set to `()` before the traps are set. Each child's ID is stored at an index that only increases, as `pids[n]=$!` then `n=$((n + 1))`, with `n` starting at 0. Once the script's own `wait` on that child returns, `unset 'pids[i]'` removes its ID, `i` being the index it was stored at. The array's length cannot stand in for `n`, since an `unset` leaves the array sparse. A trap then never signals a process ID the system may have reused. A trap skips stopping the children while `${#pids[@]}` is 0, a test `set -u` allows on an empty array in every Bash version. Under `set -u`, an unset one would end the trap before the line prints. Each `kill` sent to the children takes `2>/dev/null`, since a child that already exited makes it print an error. Each `kill` but the polling one takes `|| :`, and so do the `sleep` and the `wait`. Under `set -e`, a non-zero status from any of them would end the trap before the line prints. A second Ctrl-C ends the `sleep` with one. The polling `kill -0` is the loop's condition, which `set -e` does not read. SIGTERM is the signal sent because a background child ignores SIGINT. Other cleanup stays in the `EXIT` trap. A script that the `exec` case in `CODESTYLE.md` "Interrupts" frees from owing a trap sets neither trap.
- **A long-running external command runs in the background, and the script waits for it with `wait "$pid"`, so its exit status reaches `set -e`.** Bash runs a trap only after a foreground command returns, while a trapped signal ends a `wait` at once. Only the script's own shell starts such a child. One that a subshell or a pipeline element starts never reaches the script's array or its `wait`. A background child that reads standard input takes `<&0`, since bash otherwise gives it `/dev/null`. The trap's `kill` reaches only the process `$!` names. So a function, a group, a subshell, a pipeline, or `sh -c` runs in the foreground instead. There the trap runs once the command it interrupted returns, so nothing the script started is orphaned. A launcher, such as `uvx`, `uv run`, `uv pip install`, `dotnet`, or `xargs`, may run in the foreground too. One that does not stop the tool it starts on SIGTERM must run in the foreground. `uv pip install` is one when it builds an editable install or an sdist through a build backend that outlives uv on SIGTERM. A background child starts with SIGINT ignored, and the tool a launcher starts inherits that. So at Ctrl-C the tool runs on until the trap's SIGTERM reaches it through the launcher. In the foreground, Ctrl-C reaches the whole process group, the tool included. A SIGTERM sent to the script alone runs its `TERM` trap only once that foreground command returns. So a stop that must be prompt signals the process group. That group holds only the script and its command where the script leads it, as `setsid` starts one. The `exec` case in `CODESTYLE.md` "Interrupts" is the exception. The process a script `exec`s into replaces it, and a child handed across that `exec` is not waited for.
- **A deliberately POSIX `#!/bin/sh` script, such as a git hook, adapts the bullets above to what dash lacks.** Dash runs no `EXIT` trap when the re-sent signal ends the script. So the `INT` and `TERM` traps release the temporary paths and locks themselves, and the `EXIT` trap still covers an ordinary exit. A `/bin/sh` that is bash runs the `EXIT` trap after the signal trap too. So a release that cannot run twice, such as a lock's, sets its variable back to `""`. POSIX sh has no arrays, so the script runs one background child at a time. It holds that child's process ID in a scalar, such as `child`, set to `""` again after its `wait`. Every variable a trap reads, `child` and each temporary path included, is set to `""` before the traps are set. Under `set -u`, an unset one would end the trap before the signal is re-sent. A trap skips the child's `kill` and `wait` while `[ -n "$child" ]` fails, and skips the removal of a path still empty.

Expand Down
2 changes: 1 addition & 1 deletion .github/skills/shell-codestyle/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ This applies to every shell and Python script. `CODESTYLE.md` "Interrupts in Pyt
<!-- include: CODESTYLE.md > Interrupts in Shell -->

- **Cleanup of temporary paths and locks runs in the script's `EXIT` trap, set before the `INT` and `TERM` traps.** Bash runs that trap when SIGINT or SIGTERM ends the script, as well as on a normal exit. Set after the `TERM` trap, it does not run when that trap's re-sent SIGTERM ends the script.
- **Every script sets an `INT` trap and a `TERM` trap, which stop its children and re-raise the signal.** Each one sends SIGTERM with `kill` to the children the script started, then waits for them with `wait`. It then prints the line `CODESTYLE.md` "Interrupts" requires. Last, it clears its own trap with `trap - INT` or `trap - TERM` and re-sends the signal to the script with `kill -s INT "$$"` or `kill -s TERM "$$"`. It ends with `exit 130` or `exit 143`, which runs only where the re-sent signal does not end the script, such as PID 1 in a container. The children's process IDs are held in an array, such as `pids`, set to `()` before the traps are set. A trap skips the children's `kill` and the `wait` while `${#pids[@]}` is 0, a test `set -u` allows on an empty array in every Bash version. Under `set -u`, an unset one would end the trap before the line prints. The children's `kill` takes `2>/dev/null`, since a child that already exited makes it print an error. Both take `|| :`, since under `set -e` a non-zero status from either ends the trap before the line prints. SIGTERM is the signal sent because a background child ignores SIGINT. Other cleanup stays in the `EXIT` trap. A script that the `exec` case in `CODESTYLE.md` "Interrupts" frees from owing a trap sets neither trap.
- **Every script sets an `INT` trap and a `TERM` trap, which stop its children and re-raise the signal.** Each one sends SIGTERM with `kill` to the children the script started, then waits for them for at most a bound, such as 10 seconds. A child that ignores SIGTERM would otherwise hold the trap, so at Ctrl-C the script would hang. It polls them with `kill -0`, which succeeds while any of them still runs, and runs `sleep 0.1` between polls. Where one still runs at the bound, it sends SIGKILL to them with `kill -s KILL`. Then `wait "${pids[@]}"` reaps them. A bare `wait` would also wait on every process substitution, which the bound never reaches. It then prints the line `CODESTYLE.md` "Interrupts" requires. Last, it clears its own trap with `trap - INT` or `trap - TERM` and re-sends the signal to the script with `kill -s INT "$$"` or `kill -s TERM "$$"`. It ends with `exit 130` or `exit 143`, which runs only where the re-sent signal does not end the script, such as PID 1 in a container. The children's process IDs are held in an array, such as `pids`, set to `()` before the traps are set. Each child's ID is stored at an index that only increases, as `pids[n]=$!` then `n=$((n + 1))`, with `n` starting at 0. Once the script's own `wait` on that child returns, `unset 'pids[i]'` removes its ID, `i` being the index it was stored at. The array's length cannot stand in for `n`, since an `unset` leaves the array sparse. A trap then never signals a process ID the system may have reused. A trap skips stopping the children while `${#pids[@]}` is 0, a test `set -u` allows on an empty array in every Bash version. Under `set -u`, an unset one would end the trap before the line prints. Each `kill` sent to the children takes `2>/dev/null`, since a child that already exited makes it print an error. Each `kill` but the polling one takes `|| :`, and so do the `sleep` and the `wait`. Under `set -e`, a non-zero status from any of them would end the trap before the line prints. A second Ctrl-C ends the `sleep` with one. The polling `kill -0` is the loop's condition, which `set -e` does not read. SIGTERM is the signal sent because a background child ignores SIGINT. Other cleanup stays in the `EXIT` trap. A script that the `exec` case in `CODESTYLE.md` "Interrupts" frees from owing a trap sets neither trap.
- **A long-running external command runs in the background, and the script waits for it with `wait "$pid"`, so its exit status reaches `set -e`.** Bash runs a trap only after a foreground command returns, while a trapped signal ends a `wait` at once. Only the script's own shell starts such a child. One that a subshell or a pipeline element starts never reaches the script's array or its `wait`. A background child that reads standard input takes `<&0`, since bash otherwise gives it `/dev/null`. The trap's `kill` reaches only the process `$!` names. So a function, a group, a subshell, a pipeline, or `sh -c` runs in the foreground instead. There the trap runs once the command it interrupted returns, so nothing the script started is orphaned. A launcher, such as `uvx`, `uv run`, `uv pip install`, `dotnet`, or `xargs`, may run in the foreground too. One that does not stop the tool it starts on SIGTERM must run in the foreground. `uv pip install` is one when it builds an editable install or an sdist through a build backend that outlives uv on SIGTERM. A background child starts with SIGINT ignored, and the tool a launcher starts inherits that. So at Ctrl-C the tool runs on until the trap's SIGTERM reaches it through the launcher. In the foreground, Ctrl-C reaches the whole process group, the tool included. A SIGTERM sent to the script alone runs its `TERM` trap only once that foreground command returns. So a stop that must be prompt signals the process group. That group holds only the script and its command where the script leads it, as `setsid` starts one. The `exec` case in `CODESTYLE.md` "Interrupts" is the exception. The process a script `exec`s into replaces it, and a child handed across that `exec` is not waited for.
- **A deliberately POSIX `#!/bin/sh` script, such as a git hook, adapts the bullets above to what dash lacks.** Dash runs no `EXIT` trap when the re-sent signal ends the script. So the `INT` and `TERM` traps release the temporary paths and locks themselves, and the `EXIT` trap still covers an ordinary exit. A `/bin/sh` that is bash runs the `EXIT` trap after the signal trap too. So a release that cannot run twice, such as a lock's, sets its variable back to `""`. POSIX sh has no arrays, so the script runs one background child at a time. It holds that child's process ID in a scalar, such as `child`, set to `""` again after its `wait`. Every variable a trap reads, `child` and each temporary path included, is set to `""` before the traps are set. Under `set -u`, an unset one would end the trap before the signal is re-sent. A trap skips the child's `kill` and `wait` while `[ -n "$child" ]` fails, and skips the removal of a path still empty.

Expand Down
Loading
Loading