-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fetch: do not leak signal listeners (#3158)
* Do not leak signal listeners Signed-off-by: Matteo Collina <hello@matteocollina.com> * fixup Signed-off-by: Matteo Collina <hello@matteocollina.com> * Update lib/web/fetch/request.js Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com> --------- Signed-off-by: Matteo Collina <hello@matteocollina.com> Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com>
- Loading branch information
Showing
3 changed files
with
77 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict' | ||
|
||
const http = require('node:http') | ||
const { fetch } = require('../../') | ||
const { once } = require('events') | ||
const { test } = require('node:test') | ||
const { closeServerAsPromise } = require('../utils/node-http') | ||
const { strictEqual } = require('node:assert') | ||
|
||
const isNode18 = process.version.startsWith('v18') | ||
|
||
test('long-lived-abort-controller', { skip: isNode18 }, async (t) => { | ||
const server = http.createServer((req, res) => { | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
res.write('Hello World!') | ||
res.end() | ||
}).listen(0) | ||
|
||
await once(server, 'listening') | ||
|
||
t.after(closeServerAsPromise(server)) | ||
|
||
let warningEmitted = false | ||
function onWarning () { | ||
warningEmitted = true | ||
} | ||
process.on('warning', onWarning) | ||
t.after(() => { | ||
process.off('warning', onWarning) | ||
}) | ||
|
||
const controller = new AbortController() | ||
|
||
// The maxListener is set to 1500 in request.js. | ||
// we set it to 2000 to make sure that we are not leaking event listeners. | ||
// Unfortunately we are relying on GC and implementation details here. | ||
for (let i = 0; i < 2000; i++) { | ||
// make request | ||
const res = await fetch(`http://localhost:${server.address().port}`, { | ||
signal: controller.signal | ||
}) | ||
|
||
// drain body | ||
await res.text() | ||
} | ||
|
||
strictEqual(warningEmitted, false) | ||
}) |