Skip to content

Is fs.close() mandatory? #10

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

Closed
vsemozhetbyt opened this issue Oct 3, 2015 · 7 comments
Closed

Is fs.close() mandatory? #10

vsemozhetbyt opened this issue Oct 3, 2015 · 7 comments

Comments

@vsemozhetbyt
Copy link

vsemozhetbyt commented Oct 3, 2015

Must I close files after fs.open and fs.write in the end of a script?

What happens if I don't (in different OSes)?

Does Node closes opened files before normal exit? Before exception exit?

Are there different situations when not closing is safe and when it is not?

@bnoordhuis
Copy link
Member

The operating system will close file descriptors on exit. If you have a long-running process, you should close them yourself though, otherwise you'll eventually run out of file descriptors.

@vsemozhetbyt
Copy link
Author

Thank you.

@binki
Copy link

binki commented Oct 24, 2016

Could this be added to the documentation of fs.open()?

@CEbbinghaus
Copy link

CEbbinghaus commented Aug 25, 2024

Sorry to necro this thread so badly but I am really struggling with this.

As my process opens a file descriptor to read & write to a file I need to close it. However in the event failure during my programs lifecycle it may come to pass that I never end up writing to the file and have to call close without having written anything. Now if I do this the entire file content is cleared (since I opened it in w+ mode).

simple repro

const fs = require("fs");

const file = "/path/to/file/with/contents";
const fd = fs.openSync(file, 'w+');
fs.closeSync(fd);

This is very undesired behavior since the file contained information which is now lost. This behavior does not occur when using open(file, 'a+') as that positions the cursor at the end of the file but therefore it writes from the end of the file rather than the start thereby not overwriting the existing content.

I am unable to find a way to either arbitrarily seek the cursor nor close the file without it removing any content behind the cursor. So while theoretically I could forgo calling close that would not be the correct solution.

Edit: I am using node.js v20.17.0
Edit2: It seems to be that the behavior I am observing is a problem with the open rather than the close. the w+ truncates the content on open. Therefore this isn't a problem with close and unrelated to this thread. Pardon the necro :(

@binki
Copy link

binki commented Aug 25, 2024

@CEbbinghaus Yes, you should avoid opening the file if you don't want to truncate it. You probably actually want to use the write(), fsync(), and close() on a temporary file and then rename() to your target file pattern if you are trying to rewrite the file arbitrarily. Otherwise, if you are basically implementing a database or tail logging, then you'd want to open it in append mode as you described.

@CEbbinghaus
Copy link

For anyone that may stumble upon this thread in future with a similar problem to me I have found the best solution to be:

const fs = require("fs");

const file = "/path/to/file/with/contents";
const fd = fs.openSync(file, 'a+');

if (condition) {
    fs.truncateSync(fd, 0);
    fs.writeSync(fd, content);
}

fs.closeSync(fd);

This way if no data is written the file is closed with the same content it had and if we are writing to it we overwrite the data that was there.

@binki
Copy link

binki commented Aug 25, 2024

@CEbbinghaus that is unsafe. Use the pattern of writing to a temporary file and renaming.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants