#1005972 sourced functions when run from set -e shells are suprising

#1005972#5
Date:
2022-02-18 12:39:16 UTC
From:
To:
Dear Maintainer,

Half of the initramfs-tools hook scripts on my system use "set -e"[0].
However if /usr/share/initramfs-tools/scripts/functions is sourced it
does not handle errors correctly when called from a "set -e" shell, for
example the function "configure_networking" runs ipconfig and if it
times out it will exit non-zero. S the "for ROUNDTTT" loop does not
complete, further ipconfig invocations are not attempted and the calling
hook script exists before completion.

The hook scripts on my system (fsck, resume and xfs) that call
scripts/functions don't use "set -e". However some scripts in the
archive do call configure_networking from a "set -e" shell, eg aoe[2].

I ran into this when writing a new custom hook script for clevis and was
surprised by the current behaviour. I think all functions that are
sourced should be audited to handle "set -e" and all initramfs-tools
eventually modified to use "set -e".

Kind Regards
Tom

#1005972#10
Date:
2022-02-19 04:36:03 UTC
From:
To:
Dear Maintainer,

There is a view among shell-script experts that adding "set -e"
anywhere is to be discouraged [1][2][3].

So I suggest that in any situation where remediation is required,
"set -e" should be removed, not added.

"set -e" is a fantasy to have "automatic" error handling in shell
scripts, but it breaks in all kinds of non-naive situations. The best
practice for shell scripts is to actually handle errors where they
occur:
 command || exit 1
This is not difficult, if that behaviour is desired.

It would be better if Debian policy was modified to be clearer about this.

Kind regards
David

[1] https://lists.gnu.org/archive/html/help-bash/2017-05/msg00020.html
[2] http://mywiki.wooledge.org/BashFAQ/105
[3] https://fvue.nl/wiki/Bash:_Error_handling

#1005972#15
Date:
2022-02-19 04:36:03 UTC
From:
To:
Dear Maintainer,

There is a view among shell-script experts that adding "set -e"
anywhere is to be discouraged [1][2][3].

So I suggest that in any situation where remediation is required,
"set -e" should be removed, not added.

"set -e" is a fantasy to have "automatic" error handling in shell
scripts, but it breaks in all kinds of non-naive situations. The best
practice for shell scripts is to actually handle errors where they
occur:
 command || exit 1
This is not difficult, if that behaviour is desired.

It would be better if Debian policy was modified to be clearer about this.

Kind regards
David

[1] https://lists.gnu.org/archive/html/help-bash/2017-05/msg00020.html
[2] http://mywiki.wooledge.org/BashFAQ/105
[3] https://fvue.nl/wiki/Bash:_Error_handling