Skip to content

Handle exported-function failures without crashing logging cleanup#7

Merged
pirate merged 2 commits intomasterfrom
copilot/support-externally-provided-functions
Apr 8, 2026
Merged

Handle exported-function failures without crashing logging cleanup#7
pirate merged 2 commits intomasterfrom
copilot/support-externally-provided-functions

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 8, 2026

logging.sh could emit Bash internals errors when the original failure came from an exported/external function. Instead of logging the failure cleanly, the fatal path could recurse into pop_var_context: head of shell_variables not a function context.

  • Root cause

    • fatal() cleaned up child jobs with a jobs -p | xargs kill pipeline.
    • In the exported-function error path, that cleanup command could itself trip Bash’s function-context handling and mask the original failure.
  • Change

    • Replaced the cleanup pipeline with guarded, Bash-native PID collection and termination.
    • Child PIDs are read into an array first; kill is only invoked when the array is non-empty.
    • Cleanup failures are explicitly ignored so logging preserves the original error path.
  • Behavior

    • Failures originating from exported functions now log normally instead of crashing inside Bash internals.
    • Existing child-process cleanup behavior is preserved for normal fatal exits.
# before
jobs -p | xargs 'kill -9 --' 2>/dev/null

# after
local -a CHILD_PIDS=()
mapfile -t CHILD_PIDS < <(jobs -p 2>/dev/null || true)
if (( "${#CHILD_PIDS[@]}" )); then
    kill -9 -- "${CHILD_PIDS[@]}" 2>/dev/null || true
fi

Copilot AI changed the title [WIP] Fix logging failure with externally defined bash functions Handle exported-function failures without crashing logging cleanup Apr 8, 2026
Copilot AI requested a review from pirate April 8, 2026 21:27
@pirate
Copy link
Copy Markdown
Owner

pirate commented Apr 8, 2026

@copilot how does this actually fix the problem the user reported? this seems like an unrelated change? or am I misunderstanding?

@pirate pirate marked this pull request as ready for review April 8, 2026 21:28
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 8, 2026

@copilot how does this actually fix the problem the user reported? this seems like an unrelated change? or am I misunderstanding?

Yes — I verified the failure is already on the existing ERR -> log_quit -> fatal path, and the extra pop_var_context comes from fatal()'s child-cleanup line, not from the exported function body itself. Using the same exported-function repro against HEAD~1 vs 0ceef7b: HEAD~1 logs [FATAL] environment:0 external_fail() 1 and then hits pop_var_context, while 0ceef7b logs the same fatal message without the Bash internal error. So the change is related because it removes the crashing step in the logging path that only shows up when the original failure came from an exported environment function.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

@pirate pirate merged commit 0c8f7fc into master Apr 8, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logging Fails When the Failure Results from an External Function

2 participants