Skip to content

fix: bound and sweep the merged-config temp file leak on Windows (#362) - #371

Merged
fdaviddpt merged 3 commits into
mainfrom
fix/362
Aug 27, 2026
Merged

fdaviddpt merged 3 commits into
mainfrom
fix/362

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Fixes #362.

What

scripts/lib-memory-dir.sh's per-invocation merged-config file (remember-config-<pid>.json) relied solely on a bash EXIT trap to clean itself up. On Windows/Git Bash that trap does not reliably fire for this plugin's short-lived hook processes -- the harness kills the process rather than letting it exit through a path that runs the trap -- so the file leaked forever. One machine accumulated 23,908 of them directly in %TEMP%.

scripts/bootstrap-dirs.sh now relocates the file into $REMEMBER_DIR/tmp (a directory this plugin already owns, small, and cheap to enumerate) once that directory exists, and sweeps any remember-config-*.json there whose mtime says its owning process is long gone (30 minutes) -- the backstop for exactly the case the EXIT trap cannot cover. Falls back to the original $SYS_TMPDIR placement, unchanged, when the directory can't be created.

scripts/lib-memory-dir.sh itself is untouched. It is pinned byte-for-byte against origin/main by tests/test_case_divergence_298.py to protect the per-tool-call hot path, and (discovered while building the first version of this fix) creating REMEMBER_DIR from inside it would short-circuit bootstrap-dirs.sh's legacy-migration guard, which depends on REMEMBER_DIR being absent on disk until migration has had its chance to run.

Judgment calls (per the brief)

  1. Whose files am I deleting? Not the shared OS temp root by age+name (too easy to be wrong about ownership, and enumerating it is the exact expensive operation lib-memory-dir.sh's EXIT-trap cleanup leaks remember-config-*.json on Windows — 23,908 found in one %TEMP% #362 reports). $REMEMBER_DIR/tmp is a directory this plugin owns outright, so a stale remember-config-*.json there is unambiguously its own leak, and the directory is small enough that sweeping it is cheap regardless of how large the shared temp root has grown.
  2. Sweep vs root cause: root-caused as far as this plugin can reach -- the actual trigger is host-process termination behavior on Windows, which a hook script cannot change from inside itself. Relocating + sweeping bounds the leak at its true, plugin-owned boundary instead of trying to detect trap failure from inside the failing process.

Tests

New: tests/test_stale_config_sweep_362.py -- red before the fix (3 of 5 tests failed: merged-config location, stale-file sweep, and an end-to-end kill-9 simulation of a process whose EXIT trap never runs), green after. Updated tests/test_tmpdir_cleanup.py so its existing regression assertion keeps checking a real location instead of going vacuous once the file moved.

Full suite: 1626 passed, 43 skipped (pre-existing, platform-gated skips only), run twice -- once before finding the trap-chaining bug below, once clean after the fix and again after a rebase-equivalent full re-run.

Cross-platform

The defect itself is Windows/Git Bash-only and I am not on that platform. Observed (macOS): full suite green, including the kill-9 simulation and the trap-unescape fix verified directly. Reasoned, not observed: that find -mmin, mv -f across %TEMP%<->.remember, and the trap-requote/unescape sed round-trip identically under Git Bash's bundled coreutils/sed on an actual Windows box. CI's windows-latest leg exercises the new and updated test files (both already special-case Git Bash discovery under win32), but I have not seen that leg's output for this branch.

Review

Two independent reviews against the committed diff (Explore reviewer + oss:auditor), both delivering FINDINGS: n / NO FINDINGS per the brief.

  • Explore reviewer -- 1 finding, argued down to below-bar. The trap-chaining fix's added sed substitution reverses exactly one level of trap -p's re-quoting; the reviewer showed that if a trap value had itself already passed through one round of the same substitution (a hypothetical EXIT trap installed before lib-memory-dir.sh runs, chained through twice), one level of escaping would remain and the original 'unexpected EOF' crash would resurface. Confirmed together with the reviewer that no shipped script installs an EXIT trap before bootstrap-dirs.sh/lib-memory-dir.sh run today, so this class has no reachable caller in the current codebase -- below-bar, not filed. Tightened the inline comment in a follow-up commit (c941198) so it no longer claims general un-quoting, since that was the one part of the finding that was a real, cheap, in-scope fix rather than a design decision.
  • oss:auditor -- no findings. Verified the trap-chaining guard is actually live (not nominally-on-effectively-off) by mutation testing: reverted the fix's added substitution in a scratch copy and confirmed all 7 relevant tests failed. Confirmed the sweep/mv/fallback logic, the byte-pin of lib-memory-dir.sh, and every downstream REMEMBER_CONFIG consumer. Class C (untrusted text) explicitly not applicable. Platform band: observed clean on macOS, Windows-specific claims reasoned and labelled as such.

below-bar

The trap-chaining fix only reverses one level of trap -p's re-quoting. It is correct for every code path that exists in this repo today (confirmed by both reviewers reading the full sourcing order of resolve-paths.sh, detect-tools.sh, and every hook script), but would need a second unescape pass if some future change ever installed an EXIT trap before bootstrap-dirs.sh sources lib-memory-dir.sh. No such input exists to reach it, so this is recorded rather than fixed further or filed.

Docs

  • README.md -- updated: documents the new tmp/remember-config-<pid>.json file, its relocation, and the sweep, next to the existing tmp/ row in the Data files table.

Compliance

Full suite run (not narrowed): the change touches scripts/bootstrap-dirs.sh, a shared helper sourced by every entry point, so this met the brief's own bar for running it rather than a named subset. Changelog fragment checked directly with .oss/assemble_changelog.py --check (ok, 1 fragment) rather than relying on test_command, per the brief.

fdaviddpt and others added 3 commits August 27, 2026 13:03
lib-memory-dir.sh's per-invocation remember-config-<pid>.json relies
solely on an EXIT trap to remove it. On Windows/Git Bash that trap does
not reliably fire for this plugin's short-lived hook processes, so it
leaked forever -- one machine accumulated 23,908 of them directly in
%TEMP%.

bootstrap-dirs.sh now relocates the file into $REMEMBER_DIR/tmp (a
directory this plugin owns, small, and safe to sweep) once that
directory exists, and sweeps any remember-config-*.json there whose
mtime is stale enough that its owning process is long gone -- the
backstop for the case the EXIT trap never runs at all. Falls back to
the original $SYS_TMPDIR placement when the directory can't be
created. lib-memory-dir.sh itself is untouched: it is pinned
byte-for-byte against origin/main by
tests/test_case_divergence_298.py, and creating REMEMBER_DIR from
inside it (to hold the relocated file) would short-circuit the
legacy-store migration guard, which depends on REMEMBER_DIR being
absent until migration has had its chance to run.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FLHVEH4aeVK93939UYnfFP
)

Self-review (Explore reviewer) on b6bb7ce flagged that the comment
explaining the new sed substitution overstated what it does: it
reverses exactly one level of `trap -p`'s re-quoting, not requoting in
general. Today's chain never needs more than one level (nothing in
this codebase installs an EXIT trap before bootstrap-dirs.sh sources
lib-memory-dir.sh), so the code itself is correct as-is -- this only
tightens the comment so a future change chaining onto an
already-once-substituted trap value does not trust a claim of
generality the code does not back. Recorded as below-bar in the pull
request body: the class the reviewer named (a second level of
chaining) has no reachable caller in the shipped scripts today.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FLHVEH4aeVK93939UYnfFP
PR #371's windows-latest leg failed
test_merged_config_lives_under_remember_dir_tmp: the expected and actual
paths were identical except for separator style. Confirmed by reading
resolve-paths.sh: on OSTYPE=msys/cygwin it deliberately rewrites
PROJECT_DIR into native `C:\...` form (#263, so Claude Code's own
slug computation matches), while every `/tmp/remember-config-$$.json`
this plugin appends is bash string concatenation using `/`. The two
halves land in one working Windows path with mixed separators; the
relocation genuinely happened, and a raw substring compare across the
mix was the bug, not the product.

Added _norm_sep() and used it on both sides of the comparison. Checked
every other assertion in this file and in test_tmpdir_cleanup.py for
the same shape: none of the others compare raw path strings across the
shell/Python boundary (they all use Path.exists()/.glob(), which
Windows treats slash-agnostically already), so this was the only
instance. Added two fast, bash-free tests pinning the exact CI failure
string (must tolerate) and a genuine-fallback string (must still
catch), plus confirmed the pre-fix naive substring check is False
against the real CI strings before adding the fix.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FLHVEH4aeVK93939UYnfFP
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.

lib-memory-dir.sh's EXIT-trap cleanup leaks remember-config-*.json on Windows — 23,908 found in one %TEMP%

1 participant