mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-05 06:03:51 +01:00
fix(server): stop reporting uploads the client abandoned mid-body (#23756)
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - The server accepts file uploads (issue attachments, images, company
imports) through multer
> - A client can close the connection before the upload body ends
(closed tab, lost network, cancelled upload)
> - multer then rejects with a plain `Error: Request aborted` or `Error:
Request closed`
> - The error handler only knew Node's own `aborted`/ECONNRESET
disconnect error, so it reported these as server crashes (500) to Sentry
> - This pull request treats multer's disconnect errors as a client
disconnect when the request stream really stopped early
> - The benefit is that cancelled uploads no longer create false crash
reports, and real server errors stay visible
## Linked Issues or Issue Description
**What happened?**
A hosted production server sent `Error: Request aborted` to Sentry from
`multer/lib/make-middleware.js` (`req.on('aborted')`). The error was
handled, had no code, and came from a client that dropped a multipart
upload.
**Steps to reproduce**
1. Start a multipart upload to a route that uses multer.
2. Send part of the file.
3. Close the connection before the body ends.
**Expected behavior**
The server ends the response with 499 and does not send a crash report.
**Actual behavior**
The error handler calls `reportCrash` and answers 500.
No public issue exists. I searched open and closed PRs for related fixes
and found none. Related earlier work: #19037 (AbortError after client
disconnect).
## What Changed
- `server/src/middleware/error-handler.ts`: add
`isMultipartClientDisconnect`. It matches a plain `Error` with the
message `Request aborted` or `Request closed`, no error code, and
`req.readableAborted === true`. The handler answers 499 and skips the
error sinks for this case.
- `server/src/__tests__/error-handler-client-abort.test.ts`: add a test
with a real multer route. The client sends part of a multipart body and
then drops the connection. Add a test that the same error text from a
connected request is still reported as 500.
## Verification
- `cd server && npx vitest run
src/__tests__/error-handler-client-abort.test.ts
src/__tests__/error-handler.test.ts`: 24 passed.
- Red before the fix: the new abandoned-upload test failed with
`captureException` called once with `Error: Request aborted`, the same
error as the production report.
- Green after the fix. I ran the abort suite 3 more times and it passed
each time.
- I did not run the full server typecheck locally. CI runs it.
## Risks
- Low risk. The match needs the exact multer messages, no error code,
and a request stream that stopped before its end. A route that throws
the same text while the client is still connected is still reported
(covered by a test).
> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.
## Model Used
- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used as a coding agent
with tool use (shell, code execution) in Claude Code.
## 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 <noreply@anthropic.com>
This commit is contained in:
2 files changed
+99
-1
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { createServer } from "node:http";
|
||||
import { createServer, request as httpRequest } from "node:http";
|
||||
import express from "express";
|
||||
import multer from "multer";
|
||||
import request from "supertest";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { errorHandler } from "../middleware/error-handler.js";
|
||||
@@ -47,6 +48,59 @@ function createApp(onReadStarted: () => void, onErrorHandled: (err: unknown) =>
|
||||
return app;
|
||||
}
|
||||
|
||||
/** An upload route whose multipart parsing failures reach the error handler. */
|
||||
function createUploadApp(onUploadStarted: () => void, onErrorHandled: (err: unknown) => void) {
|
||||
const app = express();
|
||||
const upload = multer({ storage: multer.memoryStorage() });
|
||||
app.post(
|
||||
"/upload",
|
||||
(_req, _res, next) => {
|
||||
onUploadStarted();
|
||||
next();
|
||||
},
|
||||
upload.single("file"),
|
||||
(_req, res) => {
|
||||
res.status(201).end();
|
||||
},
|
||||
);
|
||||
// A route that throws the same text while its client is still connected.
|
||||
app.post("/upload-failure", () => {
|
||||
throw new Error("Request aborted");
|
||||
});
|
||||
app.use((err: unknown, req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
try {
|
||||
errorHandler(err, req, res, next);
|
||||
} finally {
|
||||
onErrorHandled(err);
|
||||
}
|
||||
});
|
||||
return app;
|
||||
}
|
||||
|
||||
/** Starts a multipart upload, sends part of the file, then drops the connection. */
|
||||
async function abandonUpload(baseUrl: string, uploadStarted: Promise<void>) {
|
||||
const boundary = "paperclip-client-abort";
|
||||
const head = [
|
||||
`--${boundary}`,
|
||||
'Content-Disposition: form-data; name="file"; filename="large.bin"',
|
||||
"Content-Type: application/octet-stream",
|
||||
"",
|
||||
"",
|
||||
].join("\r\n");
|
||||
const clientRequest = httpRequest(`${baseUrl}/upload`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": `multipart/form-data; boundary=${boundary}`,
|
||||
"content-length": String(10 * 1024 * 1024),
|
||||
},
|
||||
});
|
||||
clientRequest.on("error", () => {});
|
||||
clientRequest.write(head);
|
||||
clientRequest.write(Buffer.alloc(64 * 1024, 1));
|
||||
await uploadStarted;
|
||||
clientRequest.destroy();
|
||||
}
|
||||
|
||||
async function withServer(app: express.Express, run: (baseUrl: string) => Promise<void>) {
|
||||
const server = createServer(app);
|
||||
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
@@ -97,4 +151,27 @@ describe("errorHandler client aborts", () => {
|
||||
message: "Request aborted",
|
||||
}));
|
||||
});
|
||||
|
||||
it("does not report an upload the client abandoned mid-body", async () => {
|
||||
let uploadStarted!: () => void;
|
||||
const started = new Promise<void>((resolve) => { uploadStarted = resolve; });
|
||||
let errorHandled!: (err: unknown) => void;
|
||||
const handled = new Promise<unknown>((resolve) => { errorHandled = resolve; });
|
||||
await withServer(createUploadApp(() => uploadStarted(), (err) => errorHandled(err)), async (baseUrl) => {
|
||||
await abandonUpload(baseUrl, started);
|
||||
// multer rejects with a plain Error once Node reports the aborted request.
|
||||
await expect(handled).resolves.toMatchObject({ message: expect.stringMatching(/^Request (aborted|closed)$/) });
|
||||
});
|
||||
|
||||
expect(captureExceptionMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("still reports a request-aborted error while the client is connected", async () => {
|
||||
await withServer(createUploadApp(() => {}, () => {}), async (baseUrl) => {
|
||||
const response = await request(baseUrl).post("/upload-failure");
|
||||
expect(response.status).toBe(500);
|
||||
});
|
||||
|
||||
expect(captureExceptionMock).toHaveBeenCalledWith(expect.objectContaining({ message: "Request aborted" }));
|
||||
});
|
||||
});
|
||||
@@ -315,6 +315,16 @@ export function errorHandler(
|
||||
return;
|
||||
}
|
||||
|
||||
// multer (multipart uploads) rejects with a plain `Error: Request aborted` or
|
||||
// `Error: Request closed` when the client drops the connection before the
|
||||
// body ends. Treat it as a disconnect only when the request stream really
|
||||
// stopped early, so the same text from a connected request is still reported.
|
||||
if (isMultipartClientDisconnect(rootError, req)) {
|
||||
if (!res.headersSent) res.status(499);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// A route that cancels its work when the response closes (for example a
|
||||
// storage read) rejects with AbortError after the client has gone. That
|
||||
// rejection is the cancellation, not a server fault. An AbortError while
|
||||
@@ -403,6 +413,17 @@ export function errorHandler(
|
||||
});
|
||||
}
|
||||
|
||||
const MULTIPART_CLIENT_DISCONNECT_MESSAGES = new Set(["Request aborted", "Request closed"]);
|
||||
|
||||
function isMultipartClientDisconnect(error: Error, req: Request) {
|
||||
return (
|
||||
error.name === "Error" &&
|
||||
MULTIPART_CLIENT_DISCONNECT_MESSAGES.has(error.message) &&
|
||||
(error as NodeJS.ErrnoException).code === undefined &&
|
||||
req.readableAborted === true
|
||||
);
|
||||
}
|
||||
|
||||
function shouldExposeTrustedCloudTenantImportError(req: Request) {
|
||||
return (
|
||||
req.actor?.source === "cloud_tenant" &&
|
||||
|
||||
Reference in new issue
Block a user