Repository navigation
Description
[agent] Found by the architecture refactor routine while interrupting and killing runs at random moments. Run on main at cfc870f with the binary built from it: agent mode over a real npm install (npm 10.9) with a stand-in patch API, hosted mode over the npm project of the repository's benchmark harness.
Problem
Every file socket-patch writes is staged under a second name and renamed into place. A run that is interrupted (Ctrl-C, SIGTERM) or killed between the two leaves the stage behind. Two kinds of stage are cleaned up later, and two are never removed by any command:
| Stage | Where | Removed later? |
|---|---|---|
.socket-stage-<file>-<uuid>, .socket-orig-<file>-<uuid> |
beside a patched file of an installed package | yes: the next apply or rollback of the package, once ten minutes old |
.socket-stage-* |
directly under .socket/ |
yes: the next command that takes the lock, once an hour old |
.socket-dl-<hash>-<uuid> |
.socket/blobs/, a blob download that did not finish |
never |
.socket-stage-<file>-<uuid> |
beside a project file a hosted run rewrites (package-lock.json, .npmrc, gradle.lockfile, …) |
never |
Who it hurts:
- A project whose
.npmrcholds a registry token. A hosted scan appendsallow-remote=allto.npmrc, so its stage is a full copy of the file, token included. The project ignores.npmrcin git; no ignore rule names.socket-stage-.npmrc-<uuid>. After a Ctrl-C at the wrong moment,git statusshows the copy as untracked andgit add -Astages the token. - Anyone who interrupts a download. The partial blob sits in
.socket/blobs/, the directory the scan tells the user to commit ("Commit .socket/ to keep the patches").repairreports "all in use" beside it,scan --prunekeeps it, and after a fullrollbackit is what keeps.socket/blobs/in the project. Each interrupted run adds another.
Evidence
1. A token-bearing .npmrc, hosted mode. The project is the benchmark harness's npm project with .npmrc (mode 0600, one _authToken line) and a .gitignore naming .npmrc, committed. A hosted scan was sent SIGINT at a random moment until a stage was left; it took 63 interrupts of a run that takes about 70 ms.
$ git status --short # before any scan: clean
$ socket-patch scan --yes # Ctrl-C
$ git status --short
?? .socket-stage-.npmrc-b5e43b73-4a87-49ec-b1ff-9fdab649e005
$ ls -l .npmrc .socket-stage-.npmrc-*
-rw------- .npmrc
-rw------- .socket-stage-.npmrc-b5e43b73-4a87-49ec-b1ff-9fdab649e005
$ grep -c _authToken .socket-stage-.npmrc-*
1
$ git check-ignore .npmrc .socket-stage-.npmrc-*
.npmrc
$ socket-patch scan --yes; echo "exit $?"
Switched 1 package to hosted patches; rewrote 2 files.
exit 0
$ touch -d '2 hours ago' .socket-stage-.npmrc-*; socket-patch scan --yes; echo "exit $?"
exit 0
$ git status --short
M package-lock.json
?? .socket-stage-.npmrc-b5e43b73-4a87-49ec-b1ff-9fdab649e005
$ git add -A; git diff --cached --name-only
.socket-stage-.npmrc-b5e43b73-4a87-49ec-b1ff-9fdab649e005
package-lock.json
How often a signal lands in the window, over the same project without the token, 200 signals each at a random moment of the run:
| Signal | Left a stage of .npmrc or package-lock.json in the project root |
|---|---|
| SIGINT | 8 |
| SIGTERM | 8 |
| SIGKILL | 7 |
A rollback leaves them the same way: a kill inside a Gradle hosted rollback's writes left .socket-stage-gradle.lockfile-<uuid> in the project root, and a later scan and rollback kept it.
2. A partial download, agent mode. lodash, ms, kleur and left-pad installed by npm, one patch each (44 files). 150 signals each at a random moment of scan --mode agent --yes:
| Signal | Left .socket/blobs/.socket-dl-* |
Left stages beside patched files | Left .socket/.socket-stage-manifest.json-* |
|---|---|---|---|
| SIGINT | 8 | 24 | 0 |
| SIGTERM | 24 | 20 | 1 |
The API here is local, so the download is a small part of the run; against a real network it is most of it. Then, over one of those trees:
$ ls -A .socket/blobs | grep -v -E '^[0-9a-f]{64}$'
.socket-dl-54a481a52cca5cbe80e498ed5e41632a9b317f61cce5edaa29746948b06bba53-45d40953-1fb3-4413-b1d4-20312a8a66db
$ socket-patch scan --mode agent --yes | sed -n 2p
Applied 4 patches.
$ socket-patch repair --yes
All blobs are present locally.
Checked 88 blobs: all in use.
$ socket-patch scan --mode agent --yes --prune | sed -n 2p
Applied 0 of 4 patches: 4 already patched.
$ socket-patch rollback --yes | sed -n 2p
Rolled back 4 packages:
$ find .socket
.socket
.socket/manifest.json
.socket/blobs
.socket/blobs/.socket-dl-54a481a52cca5cbe80e498ed5e41632a9b317f61cce5edaa29746948b06bba53-45d40953-1fb3-4413-b1d4-20312a8a66db
The stage was still there after each of them, and after an apply. The same rollback of a project no run was interrupted in leaves .socket/manifest.json alone.
3. The two sweeps that exist, as controls. Both kinds of stage, two hours old, in one .socket/:
$ find .socket -name '.socket-*'
.socket/blobs/.socket-dl-c77d51fc…-1c259529-12a7-44e5-bf58-d7215a59afad
.socket/.socket-stage-manifest.json-121437bf-9fc7-4249-a068-ccf574b7b774
$ socket-patch scan --mode agent --yes >/dev/null; find .socket -name '.socket-*'
.socket/blobs/.socket-dl-c77d51fc…-1c259529-12a7-44e5-bf58-d7215a59afad
And the stages beside patched files, 16 minutes after the kill that left them: apply took 30 to 0, rollback 25 to 0, rollback --preserve-state 24 to 0.
Why
- A blob is staged as
.socket-dl-<hash>-<uuid>beside its final name (coreapi/blob_fetcher.rs, lines 369 and 433), and the stage is removed only when the write returns an error (lines 376 and 459). A signal ends the process before that. - The hour sweep reads the direct children of
.socket/and takes names that start with.socket-stage-(coreutils/socket_dir.rs, lines 129 to 164). It never opensblobs/. - The blob cleanup of
repair,scan --pruneandrollbackskips every name that starts with a dot, and says a hidden file keeps the directory (coremanifest/cleanup_blobs.rs, lines 126 and 158). - The ten-minute sweep reads only the directories of an installed package's patch files (core
patch/apply.rs, lines 778 to 815). - A project file is staged beside itself with the destination's mode (core
utils/fs.rs,stage_pathat line 776 andstage_open_optionsat line 793), which is why the copy of.npmrcis 0600. No sweep reads the project root or a rewritten file's directory. - On a catchable signal the handler unlinks the lock and nothing else (core
patch/apply_lock.rs,cleanup_held_lock_on_interruptat line 350).
Both sweeps came in with #2052 (git log -S"fn sweep_stale_stage_entries" and -S"fn sweep_stale_stages" both name it), for this same class: the comment on the first says that without it a half-written stage "would stay under .socket/ for good". The download stage and the project-file stage are the two places that fix did not reach. CLI_CONTRACT.md describes the two sweeps (line 37, and the sentence "A killed run also leaves …" in the apply section) and says nothing of the other two.
Planned fix
- One rule for "a stage this tool named, older than its floor", in place of the three that exist (
sweep_stale_stage_entries,is_stale_stage, and the gem build's), run over every directory a run stages into:.socket/and.socket/blobs/, when the lock is taken;- the directory of each project file a hosted or vendored run is about to rewrite, for stages of that file's name (the
<prefix><file name>-<uuid>shapeis_stale_stagealready checks).
- On a catchable signal, unlink the stages the run holds, beside the lock, so Ctrl-C and SIGTERM leave nothing to sweep. This is the part that closes the
.npmrccase without waiting an hour. - The blob cleanup takes a
.socket-dl-name as an orphan, sorepairandscan --prunereport and remove it and an emptiedblobs/goes with it. CLI_CONTRACT.mdline 37 names the two new places.
Tests: a unit test per directory that plants an old stage and runs the sweep; a CLI test that parks a hosted scan between stage and rename (the ~pause failpoint the interrupt tests use), signals it, and asserts the project holds no .socket-stage-*.
This is not a decision issue: the rule is the one the two existing sweeps state. The first slice is the project-file stage (1 and 2), since that is where the token is.
The routine has three PRs open and awaiting review, so no PR is opened for this yet.
Not checked: Windows; a vendored run's downloads into .socket/vendor/ (no vendor service here); get and repair, which download through the same writer; whether the hour sweep's age floor is right for a project file's directory.
Generated by Claude Code
Activity
refactor: #2337 filed; kills, signals and file modes ran
refactor: #2401 filed; L113 to L117 ran