Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test if it leaks when the given signal never aborts and connection succeeds #1947

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/seven-forks-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@whatwg-node/node-fetch': patch
---

Remove the event listener on the provided `AbortSignal` when `node-libcurl` is used, the connection finishes to prevent
potential memory leaks;

```ts
const res = await fetch(URL, { signal: new AbortController().signal });
// AbortController is never aborted, and HTTP request is done as expected successfully
```
22 changes: 13 additions & 9 deletions packages/node-fetch/src/fetchCurl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,27 @@ export function fetchCurl<TResponseJSON = any, TRequestJSON = any>(

const deferredPromise = createDeferredPromise<PonyfillResponse<TResponseJSON>>();
let streamResolved: Readable | undefined;
if (fetchRequest['_signal']) {
fetchRequest['_signal'].onabort = () => {
if (curlHandle.isOpen) {
try {
curlHandle.pause(CurlPause.Recv);
} catch (e) {
deferredPromise.reject(e);
}
function onAbort() {
if (curlHandle.isOpen) {
try {
curlHandle.pause(CurlPause.Recv);
} catch (e) {
deferredPromise.reject(e);
}
};
}
}
if (fetchRequest['_signal']) {
fetchRequest['_signal'].addEventListener('abort', onAbort, { once: true });
}
curlHandle.once('end', function endListener() {
try {
curlHandle.close();
} catch (e) {
deferredPromise.reject(e);
}
if (fetchRequest['_signal']) {
fetchRequest['_signal'].removeEventListener('abort', onAbort);
}
});
curlHandle.once('error', function errorListener(error: any) {
if (streamResolved && !streamResolved.closed && !streamResolved.destroyed) {
Expand Down
8 changes: 8 additions & 0 deletions packages/node-fetch/tests/fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ describe('Node Fetch Ponyfill', () => {
expect(response.status).toBe(200);
expect(response.url === 'https://github.com' || response.redirected).toBeTruthy();
});
it('does not leak when signal is not used', async () => {
const res = await fetchPonyfill(baseUrl, { signal: new AbortController().signal });
await res.text();
});
it('does not leak when timeout signal is not used', async () => {
const res = await fetchPonyfill(baseUrl, { signal: AbortSignal.timeout(10_000) });
await res.text();
});
},
);
});
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@
dependencies:
"@babel/helper-plugin-utils" "^7.25.9"

"@babel/plugin-syntax-import-meta@7.10.4", "@babel/plugin-syntax-import-meta@^7.10.4":
"@babel/plugin-syntax-import-meta@^7.10.4":
version "7.10.4"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-import-meta/-/plugin-syntax-import-meta-7.10.4.tgz#ee601348c370fa334d2207be158777496521fd51"
integrity sha512-Yqfm+XDx0+Prh3VSeEQCPU81yC+JWZ2pDPFSS4ZdpfZhp4MkFMaDC1UqseovEKwSUpnIL7+vK+Clp7bfh0iD7g==
Expand Down
Loading