Skip to content

fs: deprecate file handle close #49291

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

rluvaton
Copy link
Member

@rluvaton rluvaton commented Aug 22, 2023

Warning

Work in Progress

Created after @ronag comment:

#49241 (comment)

.close should be deprecated.


same suggestion from @mcollina to remove the .close

@rluvaton rluvaton marked this pull request as ready for review August 22, 2023 19:37
@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. labels Aug 22, 2023
@rluvaton rluvaton marked this pull request as draft August 22, 2023 19:37
Copy link
Member

@ronag ronag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant on fs write stream. Also probably doc deprecate.

@rluvaton
Copy link
Member Author

You meant:

instead?

@rluvaton rluvaton marked this pull request as ready for review August 22, 2023 19:54
@rluvaton rluvaton marked this pull request as draft August 22, 2023 19:55
@mscdex
Copy link
Contributor

mscdex commented Aug 22, 2023

Why (doc or runtime) deprecate close() for node.js streams?

@bnoordhuis
Copy link
Member

I don't get it either. A comment in the linked issue says this:

fs streams should unref FileHandle not close it

If the suggestion is to let the GC drive closing files, then that's a bad suggestion for what I hope are obvious reasons.

@benjamingr
Copy link
Member

Why (doc or runtime) deprecate close() for node.js streams?

Why do we need to have three APIs to close a file stream .close .end and .destroy? Why should it be any different from any other stream?

If the suggestion is to let the GC drive closing files, then that's a bad suggestion for what I hope are obvious reasons.

Absolutely not. The idea from the linked issue is to enable a case where a user wants to open a steam for a FileHandle and keep the file open when the stream is closed (by explicitly opting into it!).

Copy link
Member

@benjamingr benjamingr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR should just be a doc-deprecation of https://nodejs.org/api/fs.html#writestreamclosecallback ?

@rluvaton
Copy link
Member Author

Why only doc deprecate instead of moving node itself out of using it?

@ronag
Copy link
Member

ronag commented Aug 27, 2023

Throwing will cause unwarranted breakage in the ecosystem.

@rluvaton
Copy link
Member Author

I did not mean to throw an error, I think that if deprecating an api we should move away from it as well

@rluvaton rluvaton marked this pull request as ready for review August 29, 2023 07:44
@rluvaton rluvaton marked this pull request as draft August 29, 2023 10:57
@rluvaton rluvaton marked this pull request as ready for review September 1, 2023 11:15
@rluvaton rluvaton marked this pull request as draft September 1, 2023 11:15
@rluvaton rluvaton marked this pull request as ready for review September 1, 2023 11:17
@rluvaton
Copy link
Member Author

rluvaton commented Sep 1, 2023

Absolutely not. The idea from the linked issue is to enable a case where a user wants to open a steam for a FileHandle and keep the file open when the stream is closed (by explicitly opting into it!).

when the user pass autoClose: false and the stream destroyed we should keep the file descriptor open?

In other words, should the following test pass?

it('should keep file handle open after stream closed when autoClose: false', async () => {
  const filePathForHandle = path.resolve(tmpDir, 'tmp-write.txt');

  const stream0Text = 'Hello world from stream0';

  const destFile = await open(filePathForHandle, 'w');
  await destFile.truncate(stream0Text.length  + 20);

  const stream0 = destFile.createWriteStream({ start: 0, autoClose: false });

  await pipeline(stream0Text, stream0);

  stream0.destroy(new Error('destroyed'));

  assert.notEqual(destFile.fd, -1);
  await destFile.close()
});

@benjamingr
Copy link
Member

when the user pass autoClose: false and the stream destroyed we should keep the file descriptor open?

That's entirely unrelated to this PR and s just context around the linked issue

@rluvaton
Copy link
Member Author

rluvaton commented Sep 2, 2023

What do you think of the PR so far? Is it in the right direction?

@benjamingr
Copy link
Member

I don't see an actual docs deprecation fo file handle close here?

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the title of this should be updated, as we are deprecating only close() for file streams, not file handles.

Copy link
Member

@ronag ronag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unref isn't right. Will leave a more constructive comment next week.


// Greater than 2 as when file handle is being created the refs is 1
// and when the stream is using that file handle the refs is 2
if (stream[kHandle]?.[kRefs] > 2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants