1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 21:55:08 +00:00

fix(purge): save and restore caller traps to prevent state leak

- Save caller's INT/TERM traps before installing local cleanup trap
- Restore original traps after clean_project_artifacts completes
- Add test to verify trap restoration behavior

Fixes P3 issue: project.sh (line 825, 870)
This commit is contained in:
tw93
2026-02-21 20:21:54 +08:00
parent 7648bd9d12
commit 0597021fd4
2 changed files with 44 additions and 8 deletions

View File

@@ -800,6 +800,9 @@ clean_project_artifacts() {
local -a all_found_items=()
local -a safe_to_clean=()
local -a recently_modified=()
local previous_int_trap=""
local previous_term_trap=""
local trap_installed_by_this_call=false
# Set up cleanup on interrupt
# Note: Declared without 'local' so cleanup_scan trap can access them
scan_pids=()
@@ -820,12 +823,11 @@ clean_project_artifacts() {
echo ""
exit 130
}
# Set up signal handling only if not already set by parent script
# This prevents trap conflicts between purge.sh and project.sh
if [[ -z "${_MO_PURGE_TRAP_SET:-}" ]]; then
trap cleanup_scan INT TERM
export _MO_PURGE_TRAP_SET=1
fi
# Save caller traps and install local cleanup trap for this function call.
previous_int_trap=$(trap -p INT || true)
previous_term_trap=$(trap -p TERM || true)
trap cleanup_scan INT TERM
trap_installed_by_this_call=true
# Scanning is started from purge.sh with start_inline_spinner
# Launch all scans in parallel
for path in "${PURGE_SEARCH_PATHS[@]}"; do
@@ -866,8 +868,12 @@ clean_project_artifacts() {
rm -f "$scan_output"
fi
done
# Clean up trap
trap - INT TERM
# Restore caller traps after this function completes.
if [[ "$trap_installed_by_this_call" == "true" ]]; then
trap - INT TERM
[[ -n "$previous_int_trap" ]] && eval "$previous_int_trap"
[[ -n "$previous_term_trap" ]] && eval "$previous_term_trap"
fi
if [[ ${#all_found_items[@]} -eq 0 ]]; then
echo ""
echo -e "${GREEN}${ICON_SUCCESS}${NC} Great! No old project artifacts to clean"