armbian-next: still fighting tee leaking under duress, and I think I won

- call `check_and_close_fd_13()` directly when `exit_with_error()` called, don't wait for trap
- store PID spawned by `do_with_logging()` under `$global_tee_pid`
- `check_and_close_fd_13()`:
  - sprinkle with `sync` to give a chance for stuff to die gracefully
  - check if `$global_tee_pid` is set and actually a running PID, if so, kill it
  - do not blindly "wait" for stuff, instead, kill it and wait for it to die -- 100% more likely to not hang ;-)
This commit is contained in:
Ricardo Pardini
2023-01-18 00:02:22 +01:00
parent ce47db1e0a
commit 62d948b1b4
3 changed files with 25 additions and 6 deletions

View File

@@ -102,14 +102,24 @@ function print_current_asset_log_base_file() {
function check_and_close_fd_13() {
if [[ -e /proc/self/fd/13 ]]; then
display_alert "Closing fd 13" "log still open" "cleanup"
exec 13>&- || true # close the file descriptor, lest sed keeps running forever.
display_alert "Waiting for tee to finish" "if it's still running" "cleanup"
wait || true # wait for the tee process to finish
display_alert "Done waiting for tee/fd13" "it's gone" "cleanup"
sync # let the disk catch up
display_alert "Closing fd 13" "log still open" "cleanup" # no reason to be alarmed
exec 13>&- || true # close the file descriptor, lest sed keeps running forever.
sync # make sure the file is written to disk
sleep 1 # give it a second to die.
else
display_alert "fd 13 is not open" "nothing to do" "cleanup"
display_alert "Not closing fd 13" "log already closed" "cleanup"
fi
display_alert "Checking if global_tee_pid is set and running" "global_tee_pid: ${global_tee_pid}" "cleanup"
if [[ -n "${global_tee_pid}" && ${global_tee_pid} -gt 1 ]] && ps -p "${global_tee_pid}" > /dev/null; then
display_alert "Killing global_tee_pid" "${global_tee_pid}" "cleanup"
kill "${global_tee_pid}" && wait "${global_tee_pid}"
sync # wait for the disk to catch up
else
display_alert "Not killing global_tee_pid" "${global_tee_pid} not running" "cleanup"
fi
}
function discard_logs_tmp_dir() {

View File

@@ -49,6 +49,9 @@ function do_with_logging() {
prefix_sed_contents="$(logging_echo_prefix_for_pv "tool") $(echo -n -e "${tool_color}")" # spaces are significant
local prefix_sed_cmd="s/^/${prefix_sed_contents}/;"
# global var to store the pid of spawned logging process.
declare -g -i global_tee_pid=0
# Create a 13rd file descriptor sending it to sed. https://unix.stackexchange.com/questions/174849/redirecting-stdout-to-terminal-and-file-without-using-a-pipe
# Also terrible: don't hold a reference to cwd by changing to SRC always
# There's handling of this fd 13 in check_and_close_fd_13()
@@ -57,6 +60,9 @@ function do_with_logging() {
# First, log to file, then add prefix via sed for what goes to screen.
tee -a "${CURRENT_LOGFILE}" | sed -u -e "${prefix_sed_cmd}"
)
# get the pid of the spawned process, so we can kill it later.
global_tee_pid=$!
"$@" >&13
exec 13>&- # close the file descriptor, lest sed keeps running forever.
else

View File

@@ -163,6 +163,9 @@ function exit_with_error() {
#exec {FD}> /var/lock/armbian-debootstrap-losetup
#flock -u "${FD}"
# let's try early to close tee/sed and fd 13 which might be opened if this happened in a logging section
check_and_close_fd_13
exit 43
}