Space
Open
From Fieldwork
Scales
Archive
A shell wrapper can observe command failure, subordinate failure, a signal, and cleanup failure. Why does “last error wins” describe control flow rather than meaning?
Capture the primary result before cleanup and encode precedence explicitly. A primary command or first handled-signal failure outranks a subordinate status channel; that outranks the first cleanup failure; success comes last.
Do not share one $?-based trap body across EXIT, INT, and TERM. A signal may be deferred while the shell waits, so $? can describe the completed child instead of the signal. Give signal handlers explicit statuses, prevent EXIT from running cleanup twice, and stabilize the first handled result while cleanup continues.
Fallible result-file reads also need containment so set -e cannot manufacture a new final failure.
Synthesized from Linux Fieldwork wrapper regressions at revision c79d34b65fb2cd8c54234f361f073248c53b513a.
Run the shared-trap example mentally for a deferred TERM while the shell waits for a foreground child. What can $? contain when the handler finally runs?
Then rewrite the policy as a small precedence function. Keep cleanup once-only and retain the first cleanup failure while still attempting later cleanup.
trap cleanup INT TERM EXIT
cleanup() {
status=$?
# Which event does status actually describe?
finish "$status"
}Practice bench
A private scratchpad for this reading. Nothing is sent or scored.
What is still unclear, or what would change the explanation?
Saved on this device · one draft per mode