Repository navigation
fix: clean up merged config in scripts that replace the lib EXIT trap - #134
Merged
Merged
Conversation
lib-memory-dir.sh registers a chaining EXIT trap to remove its
${TMPDIR}/remember-config-$$.json, but save-session.sh and
run-consolidation.sh install their own plain 'trap ... EXIT' afterwards.
Bash keeps a single EXIT trap, so the lib's cleanup was discarded and one
merged-config file was orphaned per run — accumulating thousands of stray
files in TMPDIR on active installs.
Register $REMEMBER_CONFIG in save-session.sh's CLEANUP_FILES and append
it to run-consolidation.sh's lock-cleanup trap. Regression tests run both
scripts in an isolated TMPDIR and assert no remember-config-* survives.
There was a problem hiding this comment.
Pull request overview
This PR fixes a TMPDIR leak where lib-memory-dir.sh creates remember-config-<pid>.json and installs a cleanup EXIT trap, but save-session.sh and run-consolidation.sh later overwrite that trap and orphan the merged-config temp file.
Changes:
save-session.sh: adds$REMEMBER_CONFIGto its existingCLEANUP_FILESso the script’scleanup()removes the merged-config temp file.run-consolidation.sh: extends its lock cleanup EXIT trap to also remove$REMEMBER_CONFIG.- Adds
tests/test_tmpdir_cleanup.pyto regression-test that both scripts leave noremember-config-*.jsonbehind in an isolatedTMPDIR.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/test_tmpdir_cleanup.py | New regression tests that run real scripts under an isolated TMPDIR and assert no merged-config temp file remains. |
| scripts/save-session.sh | Ensures $REMEMBER_CONFIG is included in the script’s cleanup list (but see lock-cleanup bug comment). |
| scripts/run-consolidation.sh | Ensures $REMEMBER_CONFIG is removed by the script’s EXIT trap along with its lock file. |
Comments suppressed due to low confidence (1)
scripts/save-session.sh:75
trap cleanup EXITis installed before the lock acquisition check. On the lock-held early-exit path (exit 0),cleanup()currently removes$LOCK_FILEunconditionally, which can delete another process' live lock and allow concurrent runs. Since this change touches cleanup behavior (now also removing$REMEMBER_CONFIG), it’s a good opportunity to make lock removal conditional on ownership (lock file containing$$).
CLEANUP_FILES+=("$REMEMBER_CONFIG")
# Remove lock file and all accumulated temp files on exit.
cleanup() { rm -f "$LOCK_FILE" "${CLEANUP_FILES[@]}"; }
trap cleanup EXIT
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
4 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
lib-memory-dir.shcreates${TMPDIR}/remember-config-$$.jsonat source time and registers a chaining EXIT trap to remove it (it even parsestrap -p EXITspecifically to avoid clobbering an existing trap). But two callers source the lib first and install their own plaintrap ... EXITafterwards — and bash keeps a single EXIT trap, so the lib's cleanup is silently discarded:save-session.sh:72—trap cleanup EXIT, andcleanup()removes only$LOCK_FILE+${CLEANUP_FILES[@]}; the merged config is never registered thererun-consolidation.sh:55—trap 'rm -f "$LOCK_FILE"' EXITResult: one orphaned
remember-config-<pid>.json(a 3-byte{}in a default install with noconfig.json) per run, on every exit taken after the script's trap is installed — forsave-session.shthat includes the cooldown-skip and lock-skip paths. We found ~13k stray files in$TMPDIRon one long-running machine. (Orthogonal to #125 — that affects how often these scripts are forked; this fixes the per-run cleanup.)Changes
save-session.sh: register$REMEMBER_CONFIGinCLEANUP_FILESso the existingcleanup()removes itrun-consolidation.sh: append$REMEMBER_CONFIGto the lock-cleanup traptests/test_tmpdir_cleanup.py: regression tests running both real scripts in an isolatedTMPDIR, asserting noremember-config-*survives (both fail before this fix, pass after)Test plan
pytestpasses locally (504 passed, 41 skipped; macOS, bash 5.3.12)tests/test_tmpdir_cleanup.py, red before / green after)save-session.shon the lock-skip and cooldown-skip paths andrun-consolidation.shon the no-staging path; noremember-config-*file remained, exit codes and logs unchangedChecklist
README.md,config.example.json) — N/A, none added.claude-plugin/plugin.jsonif shipping a release — N/A, not a releaseREADME.md— not included; happy to add if you'd like it in this PR