-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
fs: add autoClose option to FileHandle readableWebStream #58548
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
fs: add autoClose option to FileHandle readableWebStream #58548
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Being able to return new Response(filehandle.readableWebStream()); without leaking the handle is great, I hope we can make this enabled by default in next major releases.
LGTM with a few nits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change is LGTM. Should we test that the stream is indeed closed? I could not see that in the test where the option is now used.
I'm not entirely sure how to test that yet in this case but will look at it. The reason this is needed for this particular test is that I tried to make the "FileHandle closed on GC" warning an error and this test hit that. With the autoClose option that works and the corresponding test passes. Given that I think we will likely have sufficient test coverage. Either way, I think we can likely add a test separately if we are concerned that the streams close path may not be followed correctly. |
a90acc4 to
e49cce2
Compare
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as outdated.
This comment was marked as outdated.
I think something like this would be sufficient import { open } from 'node:fs/promises';
import { rejects } from 'node:assert';
{
const fh = await open(new URL(import.meta.url));
// TODO: remove autoClose option when it becomes default
const readableStream = fh.readableWebStream({ autoClose: true });
// Consume the stream
await new Response(readableStream).text();
await rejects(fh.read(), { code: 'EBADF' });
}
{
await using fh = await open(new URL(import.meta.url)); // or the usual let-try-finally-close if we want this backportable...
const readableStream = fh.readableWebStream({ autoClose: false });
// Consume the stream
await new Response(readableStream).text();
// Filehandle must be still open
await fh.read();
} |
By default, the `readableWebStream` method of `FileHandle` returns a ReadableStream that, when finished, does not close the underlying FileHandle. This can lead to issues if the stream is consumed without having a reference to the FileHandle to close after use. This commit adds an `autoClose` option to the `readableWebStream` method, which, when set to `true`, will automatically close the FileHandle when the stream is finished or canceled. The test modified in this commit demonstrates one of the cases where this is necessary in that the stream is consumed by separate code than the FileHandle which was being left to close the underlying fd when it is garbage collected, which is a deprecated behavior.
e49cce2 to
777d231
Compare
By default, the `readableWebStream` method of `FileHandle` returns a ReadableStream that, when finished, does not close the underlying FileHandle. This can lead to issues if the stream is consumed without having a reference to the FileHandle to close after use. This commit adds an `autoClose` option to the `readableWebStream` method, which, when set to `true`, will automatically close the FileHandle when the stream is finished or canceled. The test modified in this commit demonstrates one of the cases where this is necessary in that the stream is consumed by separate code than the FileHandle which was being left to close the underlying fd when it is garbage collected, which is a deprecated behavior. PR-URL: #58548 Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
|
Landed in 3f81645 |
By default, the `readableWebStream` method of `FileHandle` returns a ReadableStream that, when finished, does not close the underlying FileHandle. This can lead to issues if the stream is consumed without having a reference to the FileHandle to close after use. This commit adds an `autoClose` option to the `readableWebStream` method, which, when set to `true`, will automatically close the FileHandle when the stream is finished or canceled. The test modified in this commit demonstrates one of the cases where this is necessary in that the stream is consumed by separate code than the FileHandle which was being left to close the underlying fd when it is garbage collected, which is a deprecated behavior. PR-URL: #58548 Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Notable changes: doc: * deprecate utilisNativeError in favor of ErrorisError (Miguel Marcondes Filho) #58262 * deprecate passing an empty string to `options.shell` (Antoine du Hamel) #58564 * deprecate HTTP/2 priority signaling (Matteo Collina) #58313 * (SEMVER-MINOR) graduate Symbol.dispose/asyncDispose from experimental (James M Snell) #58467 esm: * (SEMVER-MINOR) implement import.meta.main (Joe) #57804 fs: * (SEMVER-MINOR) add autoClose option to FileHandle readableWebStream (James M Snell) #58548 http: * deprecate instantiating classes without new (Yagiz Nizipli) #58518 http2: * (SEMVER-MINOR) add diagnostics channel 'http2.server.stream.finish' (Darshan Sen) #58560 lib: * (SEMVER-MINOR) graduate error codes that have been around for years (James M Snell) #58541 perf_hooks: * (SEMVER-MINOR) make event loop delay histogram disposable (James M Snell) #58384 src: * (SEMVER-MINOR) support namespace options in configuration file (Pietro Marchini) #58073 test: * (SEMVER-MINOR) add disposable histogram test (James M Snell) #58384 * (SEMVER-MINOR) add test for async disposable worker thread (James M Snell) #58385 util: * (SEMVER-MINOR) add 'none' style to styleText (James M Snell) #58437 worker: * (SEMVER-MINOR) make Worker async disposable (James M Snell) #58385 PR-URL: #58635
Notable changes: doc: * deprecate `util.isNativeError` in favor of `Error.isError` (Miguel Marcondes Filho) #58262 * deprecate passing an empty string to `options.shell` (Antoine du Hamel) #58564 * deprecate HTTP/2 priority signaling (Matteo Collina) #58313 * (SEMVER-MINOR) graduate `Symbol.dispose`/`asyncDispose` from experimental (James M Snell) #58467 esm: * (SEMVER-MINOR) implement import.meta.main (Joe) #57804 fs: * (SEMVER-MINOR) add `autoClose` option to `FileHandle` `readableWebStream` (James M Snell) #58548 http: * deprecate instantiating classes without new (Yagiz Nizipli) #58518 http2: * (SEMVER-MINOR) add diagnostics channel 'http2.server.stream.finish' (Darshan Sen) #58560 * (SEMVER-MAJOR) remove support for priority signaling (Matteo Collina) #58293 lib: * (SEMVER-MINOR) graduate error codes that have been around for years (James M Snell) #58541 perf_hooks: * (SEMVER-MINOR) make event loop delay histogram disposable (James M Snell) #58384 src: * (SEMVER-MINOR) support namespace options in configuration file (Pietro Marchini) #58073 test: * (SEMVER-MINOR) add disposable histogram test (James M Snell) #58384 * (SEMVER-MINOR) add test for async disposable worker thread (James M Snell) #58385 util: * (SEMVER-MINOR) add 'none' style to styleText (James M Snell) #58437 worker: * (SEMVER-MINOR) make Worker async disposable (James M Snell) #58385 PR-URL: #58635
Notable changes: doc: * deprecate `util.isNativeError` in favor of `Error.isError` (Miguel Marcondes Filho) #58262 * deprecate passing an empty string to `options.shell` (Antoine du Hamel) #58564 * deprecate HTTP/2 priority signaling (Matteo Collina) #58313 * (SEMVER-MINOR) graduate `Symbol.dispose`/`asyncDispose` from experimental (James M Snell) #58467 esm: * (SEMVER-MINOR) implement import.meta.main (Joe) #57804 fs: * (SEMVER-MINOR) add `autoClose` option to `FileHandle` `readableWebStream` (James M Snell) #58548 http: * deprecate instantiating classes without new (Yagiz Nizipli) #58518 http2: * (SEMVER-MINOR) add diagnostics channel 'http2.server.stream.finish' (Darshan Sen) #58560 * (SEMVER-MAJOR) remove support for priority signaling (Matteo Collina) #58293 lib: * (SEMVER-MINOR) graduate error codes that have been around for years (James M Snell) #58541 perf_hooks: * (SEMVER-MINOR) make event loop delay histogram disposable (James M Snell) #58384 src: * (SEMVER-MINOR) support namespace options in configuration file (Pietro Marchini) #58073 test: * (SEMVER-MINOR) add disposable histogram test (James M Snell) #58384 * (SEMVER-MINOR) add test for async disposable worker thread (James M Snell) #58385 util: * (SEMVER-MINOR) add 'none' style to styleText (James M Snell) #58437 worker: * (SEMVER-MINOR) make Worker async disposable (James M Snell) #58385 PR-URL: #58635
Notable changes: doc: * add Filip Skokan to TSC (Rafael Gonzaga) #58499 * deprecate `util.isNativeError` in favor of `Error.isError` (Miguel Marcondes Filho) #58262 * deprecate passing an empty string to `options.shell` (Antoine du Hamel) #58564 * deprecate HTTP/2 priority signaling (Matteo Collina) #58313 * (SEMVER-MINOR) graduate `Symbol.dispose`/`asyncDispose` from experimental (James M Snell) #58467 esm: * (SEMVER-MINOR) implement import.meta.main (Joe) #57804 fs: * (SEMVER-MINOR) add `autoClose` option to `FileHandle` `readableWebStream` (James M Snell) #58548 http: * deprecate instantiating classes without new (Yagiz Nizipli) #58518 http2: * (SEMVER-MINOR) add diagnostics channel 'http2.server.stream.finish' (Darshan Sen) #58560 * (SEMVER-MAJOR) remove support for priority signaling (Matteo Collina) #58293 lib: * (SEMVER-MINOR) graduate error codes that have been around for years (James M Snell) #58541 perf_hooks: * (SEMVER-MINOR) make event loop delay histogram disposable (James M Snell) #58384 src: * (SEMVER-MINOR) support namespace options in configuration file (Pietro Marchini) #58073 permission: * implicit allow-fs-read to app entrypoint (Rafael Gonzaga) #58579 test: * (SEMVER-MINOR) add disposable histogram test (James M Snell) #58384 * (SEMVER-MINOR) add test for async disposable worker thread (James M Snell) #58385 util: * (SEMVER-MINOR) add 'none' style to styleText (James M Snell) #58437 worker: * (SEMVER-MINOR) make Worker async disposable (James M Snell) #58385 PR-URL: #58635
Notable changes: doc: * add Filip Skokan to TSC (Rafael Gonzaga) #58499 * deprecate `util.isNativeError` in favor of `Error.isError` (Miguel Marcondes Filho) #58262 * deprecate passing an empty string to `options.shell` (Antoine du Hamel) #58564 * deprecate HTTP/2 priority signaling (Matteo Collina) #58313 * (SEMVER-MINOR) graduate `Symbol.dispose`/`asyncDispose` from experimental (James M Snell) #58467 esm: * (SEMVER-MINOR) implement import.meta.main (Joe) #57804 fs: * (SEMVER-MINOR) add `autoClose` option to `FileHandle` `readableWebStream` (James M Snell) #58548 http: * deprecate instantiating classes without new (Yagiz Nizipli) #58518 http2: * (SEMVER-MINOR) add diagnostics channel 'http2.server.stream.finish' (Darshan Sen) #58560 * (SEMVER-MAJOR) remove support for priority signaling (Matteo Collina) #58293 lib: * (SEMVER-MINOR) graduate error codes that have been around for years (James M Snell) #58541 perf_hooks: * (SEMVER-MINOR) make event loop delay histogram disposable (James M Snell) #58384 src: * (SEMVER-MINOR) support namespace options in configuration file (Pietro Marchini) #58073 permission: * implicit allow-fs-read to app entrypoint (Rafael Gonzaga) #58579 test: * (SEMVER-MINOR) add disposable histogram test (James M Snell) #58384 * (SEMVER-MINOR) add test for async disposable worker thread (James M Snell) #58385 util: * (SEMVER-MINOR) add 'none' style to styleText (James M Snell) #58437 worker: * (SEMVER-MINOR) make Worker async disposable (James M Snell) #58385 PR-URL: #58635
Notable changes: doc: * add Filip Skokan to TSC (Rafael Gonzaga) #58499 * deprecate `util.isNativeError` in favor of `Error.isError` (Miguel Marcondes Filho) #58262 * deprecate passing an empty string to `options.shell` (Antoine du Hamel) #58564 * deprecate HTTP/2 priority signaling (Matteo Collina) #58313 * (SEMVER-MINOR) graduate `Symbol.dispose`/`asyncDispose` from experimental (James M Snell) #58467 esm: * (SEMVER-MINOR) implement import.meta.main (Joe) #57804 fs: * (SEMVER-MINOR) add `autoClose` option to `FileHandle` `readableWebStream` (James M Snell) #58548 http: * deprecate instantiating classes without new (Yagiz Nizipli) #58518 http2: * (SEMVER-MINOR) add diagnostics channel 'http2.server.stream.finish' (Darshan Sen) #58560 * (SEMVER-MAJOR) remove support for priority signaling (Matteo Collina) #58293 lib: * (SEMVER-MINOR) graduate error codes that have been around for years (James M Snell) #58541 perf_hooks: * (SEMVER-MINOR) make event loop delay histogram disposable (James M Snell) #58384 src: * (SEMVER-MINOR) support namespace options in configuration file (Pietro Marchini) #58073 permission: * implicit allow-fs-read to app entrypoint (Rafael Gonzaga) #58579 test: * (SEMVER-MINOR) add disposable histogram test (James M Snell) #58384 * (SEMVER-MINOR) add test for async disposable worker thread (James M Snell) #58385 util: * (SEMVER-MINOR) add 'none' style to styleText (James M Snell) #58437 worker: * (SEMVER-MINOR) make Worker async disposable (James M Snell) #58385 PR-URL: #58635
Notable changes: doc: * add Filip Skokan to TSC (Rafael Gonzaga) #58499 * deprecate `util.isNativeError` in favor of `Error.isError` (Miguel Marcondes Filho) #58262 * deprecate passing an empty string to `options.shell` (Antoine du Hamel) #58564 * deprecate HTTP/2 priority signaling (Matteo Collina) #58313 * (SEMVER-MINOR) graduate `Symbol.dispose`/`asyncDispose` from experimental (James M Snell) #58467 esm: * (SEMVER-MINOR) implement import.meta.main (Joe) #57804 fs: * (SEMVER-MINOR) add `autoClose` option to `FileHandle` `readableWebStream` (James M Snell) #58548 http: * deprecate instantiating classes without new (Yagiz Nizipli) #58518 http2: * (SEMVER-MINOR) add diagnostics channel 'http2.server.stream.finish' (Darshan Sen) #58560 * (SEMVER-MAJOR) remove support for priority signaling (Matteo Collina) #58293 lib: * (SEMVER-MINOR) graduate error codes that have been around for years (James M Snell) #58541 perf_hooks: * (SEMVER-MINOR) make event loop delay histogram disposable (James M Snell) #58384 src: * (SEMVER-MINOR) support namespace options in configuration file (Pietro Marchini) #58073 permission: * implicit allow-fs-read to app entrypoint (Rafael Gonzaga) #58579 test: * (SEMVER-MINOR) add disposable histogram test (James M Snell) #58384 * (SEMVER-MINOR) add test for async disposable worker thread (James M Snell) #58385 util: * (SEMVER-MINOR) add 'none' style to styleText (James M Snell) #58437 worker: * (SEMVER-MINOR) make Worker async disposable (James M Snell) #58385 PR-URL: #58635
Notable changes: doc: * add Filip Skokan to TSC (Rafael Gonzaga) #58499 * deprecate `util.isNativeError` in favor of `Error.isError` (Miguel Marcondes Filho) #58262 * deprecate passing an empty string to `options.shell` (Antoine du Hamel) #58564 * deprecate HTTP/2 priority signaling (Matteo Collina) #58313 * (SEMVER-MINOR) graduate `Symbol.dispose`/`asyncDispose` from experimental (James M Snell) #58467 esm: * (SEMVER-MINOR) implement import.meta.main (Joe) #57804 fs: * (SEMVER-MINOR) add `autoClose` option to `FileHandle` `readableWebStream` (James M Snell) #58548 http: * deprecate instantiating classes without new (Yagiz Nizipli) #58518 http2: * (SEMVER-MINOR) add diagnostics channel 'http2.server.stream.finish' (Darshan Sen) #58560 * (SEMVER-MAJOR) remove support for priority signaling (Matteo Collina) #58293 lib: * (SEMVER-MINOR) graduate error codes that have been around for years (James M Snell) #58541 perf_hooks: * (SEMVER-MINOR) make event loop delay histogram disposable (James M Snell) #58384 src: * (SEMVER-MINOR) support namespace options in configuration file (Pietro Marchini) #58073 permission: * implicit allow-fs-read to app entrypoint (Rafael Gonzaga) #58579 test: * (SEMVER-MINOR) add disposable histogram test (James M Snell) #58384 * (SEMVER-MINOR) add test for async disposable worker thread (James M Snell) #58385 util: * (SEMVER-MINOR) add 'none' style to styleText (James M Snell) #58437 worker: * (SEMVER-MINOR) make Worker async disposable (James M Snell) #58385 PR-URL: #58635
Notable changes: doc: * add Filip Skokan to TSC (Rafael Gonzaga) #58499 * deprecate `util.isNativeError` in favor of `Error.isError` (Miguel Marcondes Filho) #58262 * deprecate passing an empty string to `options.shell` (Antoine du Hamel) #58564 * deprecate HTTP/2 priority signaling (Matteo Collina) #58313 * (SEMVER-MINOR) graduate `Symbol.dispose`/`asyncDispose` from experimental (James M Snell) #58467 esm: * (SEMVER-MINOR) implement import.meta.main (Joe) #57804 fs: * (SEMVER-MINOR) add `autoClose` option to `FileHandle` `readableWebStream` (James M Snell) #58548 http: * deprecate instantiating classes without new (Yagiz Nizipli) #58518 http2: * (SEMVER-MINOR) add diagnostics channel 'http2.server.stream.finish' (Darshan Sen) #58560 * (SEMVER-MAJOR) remove support for priority signaling (Matteo Collina) #58293 lib: * (SEMVER-MINOR) graduate error codes that have been around for years (James M Snell) #58541 perf_hooks: * (SEMVER-MINOR) make event loop delay histogram disposable (James M Snell) #58384 src: * (SEMVER-MINOR) support namespace options in configuration file (Pietro Marchini) #58073 permission: * implicit allow-fs-read to app entrypoint (Rafael Gonzaga) #58579 test: * (SEMVER-MINOR) add disposable histogram test (James M Snell) #58384 * (SEMVER-MINOR) add test for async disposable worker thread (James M Snell) #58385 util: * (SEMVER-MINOR) add 'none' style to styleText (James M Snell) #58437 worker: * (SEMVER-MINOR) make Worker async disposable (James M Snell) #58385 PR-URL: #58635

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

Separated out from #58536
By default, the
readableWebStreammethod ofFileHandlereturns a ReadableStream that, when finished, does not close the underlying FileHandle. This can lead to issues if the stream is consumed without having a reference to the FileHandle to close after use. This commit adds anautoCloseoption to thereadableWebStreammethod, which, when set totrue, will automatically close the FileHandle when the stream is finished or canceled.The test modified in this commit demonstrates one of the cases where this is necessary in that the stream is consumed by separate code than the FileHandle which was being left to close the underlying fd when it is garbage collected, which is a deprecated behavior.