Skip to content

Commit d5b1217

Browse files
Skn0ttCopilot
andauthored
fix(fetch): do not crash on late EPIPE after refused body (#42116)
Co-authored-by: Copilot App <[email protected]> Copilot-Session: 5cd1c251-bdcb-4f5f-b9a6-17ed643d095e
1 parent 93ba7b6 commit d5b1217

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎packages/playwright-core/src/server/fetch.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,20 @@ export abstract class APIRequestContext extends SdkObject {
356356
let serverPort: number | undefined;
357357

358358
let securityDetails: har.SecurityDetails | undefined;
359+
let responseReceived = false;
359360

360361
const listeners: RegisteredListener[] = [];
361362

363+
const handleRequestError = (error: Error) => {
364+
// Write errors after we received a response are swallowed, following undici behaviour:
365+
// https://github.com/nodejs/undici/blob/01a912e49a50c48009ed2639d2a457a6ec26752a/lib/dispatcher/client-h1.js#L735
366+
if (responseReceived && isNetworkConnectionError(error))
367+
return;
368+
reject(error);
369+
};
370+
362371
const request = requestConstructor(url, requestOptions as any, async response => {
372+
responseReceived = true;
363373
const responseAt = monotonicTime();
364374

365375
const notifyRequestFinished = (body?: Buffer) => {
@@ -559,7 +569,7 @@ export abstract class APIRequestContext extends SdkObject {
559569
body.on('data', chunk => chunks.push(chunk));
560570
body.on('end', notifyBodyFinished);
561571
});
562-
request.on('error', reject);
572+
request.on('error', handleRequestError);
563573
destroyRequest = () => request.destroy();
564574

565575
listeners.push(
@@ -589,6 +599,14 @@ export abstract class APIRequestContext extends SdkObject {
589599
serverIPAddress = socket.remoteAddress;
590600
serverPort = socket.remotePort;
591601

602+
socket.on('error', handleRequestError);
603+
// Drop on keep-alive reuse so listeners do not accumulate. Keep if destroyed:
604+
// a late write EPIPE may still fire after a refused-body reset.
605+
request.once('close', () => {
606+
if (!socket.destroyed)
607+
socket.off('error', handleRequestError);
608+
});
609+
592610
if (request.reusedSocket) {
593611
reusedSocketAt = monotonicTime();
594612
captureSecurityDetails(socket);

‎tests/library/global-fetch.spec.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,38 @@ it('should retry ECONNRESET', {
707707
await request.dispose();
708708
});
709709

710+
it('should not crash when server refuses body before reading it', {
711+
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/42074' }
712+
}, async ({ playwright, server }) => {
713+
// Respond without reading the body, then reset. Node emits a late write
714+
// EPIPE/ECONNRESET on the request socket; without a listener that becomes an
715+
// unhandled 'error' and kills the process.
716+
server.setRoute('/refuse', (req, res) => {
717+
req.pause();
718+
setTimeout(() => {
719+
res.writeHead(413, { 'content-type': 'application/json' });
720+
res.end(JSON.stringify({ error: 'too large' }));
721+
req.socket.destroy();
722+
}, 50);
723+
});
724+
725+
const request = await playwright.request.newContext();
726+
// Large body so the client is still writing when the reset lands.
727+
// Prefer CROSS_PROCESS_PREFIX (127.0.0.1) over PREFIX (localhost/::1) for a stable race.
728+
const result = await request.post(server.CROSS_PROCESS_PREFIX + '/refuse', {
729+
data: Buffer.alloc(20 * 1024 * 1024, 0x78),
730+
headers: { 'content-type': 'text/plain' },
731+
maxRetries: 0,
732+
}).catch(e => e);
733+
if (result instanceof Error) {
734+
expect(result.message).toMatch(/apiRequestContext\.post|ECONNRESET|EPIPE|ECONNABORTED|socket/i);
735+
} else {
736+
expect(result.status()).toBe(413);
737+
expect(await result.text()).toBe(JSON.stringify({ error: 'too large' }));
738+
}
739+
await request.dispose();
740+
});
741+
710742
it('should throw when failOnStatusCode is set to true inside APIRequest context options', async ({ playwright, server }) => {
711743
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/34204' });
712744
const request = await playwright.request.newContext({ failOnStatusCode: true });

0 commit comments

Comments
 (0)