From bbb6b4fd5c6d5fa02e6a54bc5850f49e14c65046 Mon Sep 17 00:00:00 2001 From: paperclipbugbot Date: Fri, 20 Nov 2026 05:12:14 -0800 Subject: [PATCH] fix(workspaces): stop a cancelled single-pass capture from leaking a stream AbortError (#22397) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Paperclip saves remote agent workspaces as portable archives in a bounded worker thread > - When a save is cancelled during the single-pass archive write, the worker leaks an uncaught `AbortError` > - The parent already rejects with the cancellation reason, so the save behaves correctly, but the worker `error` event is reported to Sentry as an unhandled error > - This pull request checks the cancellation signal immediately before the single-pass read stream is created > - The benefit is that a normal cancellation stays quiet and the archive worker reports only real failures ## Linked Issues or Issue Description **What happened?** A staging server reported `AbortError: The operation was aborted` from the workspace archive worker thread (`workspaceArchive` → `readStableFile` → `FileHandle.createReadStream`). The error was unhandled in the worker. **Expected behavior** A cancelled workspace capture rejects with the cancellation reason. It does not emit an uncaught error in the archive worker. **Steps to reproduce** 1. Start `captureRemoteWorkspaceDirectory({ singlePass: true, signal })` on a workspace with many files. 2. Abort the signal while the archive body is written. 3. The worker emits an `error` event with an `AbortError`. **Root cause** The unverified path of `readStableFile` awaits `open`, `stat` and `realpath`, then calls `handle.createReadStream({ autoClose: false, signal })` without a new signal check. If the cancellation arrives in that gap, Node creates the stream with an already-aborted signal. The `for await` loop rejects as expected, but the stream also emits a second, uncaught `AbortError`. A standalone Node script shows the same behavior. The verified path is not affected because it calls `check()` synchronously before it creates the stream. ## What Changed - Call `check()` immediately before the single-pass `FileHandle` read stream is created in `readStableFile`. - Add a regression test that cancels a single-pass capture when the archive write opens its first source. The test asserts that the capture rejects with the cancellation reason, that no uncaught exception occurs, and that the partial archive is removed. ## Verification - `pnpm --filter @paperclipai/server exec vitest run src/services/remote-workspace-snapshot-archive.test.ts -t "leaking a stream abort"`: fails before the fix (rejects with the stream `AbortError` instead of the reason) and passes after the fix. - A local worker reproduction (single-pass capture of 400 files, abort after 1–500 ms, count worker `error` events) reported one worker `AbortError` before the fix and none after the fix. - The full `remote-workspace-snapshot-archive.test.ts` file has the same results with and without this change in the local sandbox, apart from the new passing test. The other local failures come from this sandbox (umask `077` and Git differences) and also fail on `master`. CI is the authority for that file. ## Risks - Low risk. The change adds one signal check on a path that already checks the signal on every chunk. A cancelled capture now rejects with the caller's reason instead of the stream's `AbortError`, which matches the verified path. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`) through Claude Code, with tool use and code execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: paperclipbugbot <356415228+paperclipbugbot@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- .../remote-workspace-snapshot-archive.test.ts | 18 ++++++++++++++++++ .../remote-workspace-snapshot-archive.ts | 1 + 2 files changed, 19 insertions(+) diff --git a/server/src/services/remote-workspace-snapshot-archive.test.ts b/server/src/services/remote-workspace-snapshot-archive.test.ts index ef290dbe637..8d501d19ac4 100644 --- a/server/src/services/remote-workspace-snapshot-archive.test.ts +++ b/server/src/services/remote-workspace-snapshot-archive.test.ts @@ -415,6 +415,24 @@ describe("portable remote workspace archives", () => { expect(await fs.readFile(outside, "utf8")).toBe("must stay outside\n"); for (const directory of archiveIo.captures) await expect(fs.access(directory)).rejects.toMatchObject({ code: "ENOENT" }); }); + it("cancels a single-pass archive write without leaking a stream abort", async () => { + const cwd = await fs.realpath(await source()), archivePath = path.join(root, "snapshot.pcws"), file = path.join(cwd, "saved.txt"); + await fs.writeFile(file, "saved\n"); + const controller = new AbortController(), reason = new Error("capture cancelled"); + let archiveOpened = false; + archiveIo.afterOpen = async target => { + if (target === archivePath) archiveOpened = true; + else if (archiveOpened && target === file) controller.abort(reason); + }; + const uncaught: unknown[] = [], record = (error: unknown) => { uncaught.push(error); }; + process.on("uncaughtException", record); + try { + await expect(captureRemoteWorkspaceDirectory({ cwd, archivePath, singlePass: true, signal: controller.signal })).rejects.toBe(reason); + await new Promise(resolve => setTimeout(resolve, 20)); + } finally { process.off("uncaughtException", record); } + expect(uncaught).toEqual([]); + await expect(fs.access(archivePath)).rejects.toMatchObject({ code: "ENOENT" }); + }); it("cleans a cancelled capture without deleting a pre-existing archive", async () => { const cwd = await source(), archivePath = path.join(root, "snapshot.pcws"); await fs.writeFile(path.join(cwd, "file"), "retained\n"); diff --git a/server/src/services/remote-workspace-snapshot-archive.ts b/server/src/services/remote-workspace-snapshot-archive.ts index 57339a6fbaa..40e231ca649 100644 --- a/server/src/services/remote-workspace-snapshot-archive.ts +++ b/server/src/services/remote-workspace-snapshot-archive.ts @@ -274,6 +274,7 @@ export async function workspaceArchive(input: { const stat = await handle.stat(); if (!stat.isFile() || await fs.realpath(source) !== source) fail("changed"); const digest = crypto.createHash("sha256"); let size = 0; + check(); for await (const bytes of handle.createReadStream({ autoClose: false, signal: input.signal, highWaterMark: 1024 * 1024 })) { check(); digest.update(bytes); size += bytes.length; await consume?.(bytes); }