diff options
author | Luca Casonato <hello@lcas.dev> | 2021-07-20 21:06:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-20 21:06:24 +0200 |
commit | a2512de95f2e03008e631a7b19405317d8c361b8 (patch) | |
tree | 6ec8b1bff9d5c48ade49894073cc4728a0d5d0d6 /extensions/fetch/26_fetch.js | |
parent | d744c0c6d9a557bbaa2a23571ffb3acabf19c35a (diff) |
fix: close fetch response body on GC (#11467)
This commit fixes fetch response bodies to be automatically closed if
the `Response.body` readable stream goes out of scope and is GC'ed.
Diffstat (limited to 'extensions/fetch/26_fetch.js')
-rw-r--r-- | extensions/fetch/26_fetch.js | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/extensions/fetch/26_fetch.js b/extensions/fetch/26_fetch.js index a0256b2bd..f7166001e 100644 --- a/extensions/fetch/26_fetch.js +++ b/extensions/fetch/26_fetch.js @@ -83,6 +83,15 @@ return core.opAsync("op_fetch_response_read", rid, body); } + // A finalization registry to clean up underlying fetch resources that are GC'ed. + const RESOURCE_REGISTRY = new FinalizationRegistry((rid) => { + try { + core.close(rid); + } catch { + // might have already been closed + } + }); + /** * @param {number} responseBodyRid * @param {AbortSignal} [terminator] @@ -119,6 +128,7 @@ // We read some data. Enqueue it onto the stream. controller.enqueue(TypedArrayPrototypeSubarray(chunk, 0, read)); } else { + RESOURCE_REGISTRY.unregister(readable); // We have reached the end of the body, so we close the stream. controller.close(); try { @@ -128,6 +138,7 @@ } } } catch (err) { + RESOURCE_REGISTRY.unregister(readable); if (terminator.aborted) { controller.error( new DOMException("Ongoing fetch was aborted.", "AbortError"), @@ -150,6 +161,7 @@ } }, }); + RESOURCE_REGISTRY.register(readable, responseBodyRid, readable); return readable; } |