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

Is it possible to delay the auto-closing & warning for the open FileHandle? #38769

Open
nujarum opened this issue May 22, 2021 · 1 comment
Open
Labels
fs Issues and PRs related to the fs subsystem / file system. promises Issues and PRs related to ECMAScript promises.

Comments

@nujarum
Copy link

nujarum commented May 22, 2021

  • Node.js Version: 14.17.0 & 16.2.0
  • Module (and version) (if relevant): fs/promises

I tried to cleanup the unclosed FileHandle using the FinalizationRegistry as shown in the following code example.

test.mjs

import { close } from 'fs';
import { open } from 'fs/promises';

const registry = new FinalizationRegistry(fd => {
    console.log('FinalizationRegistry callback');
    close(fd, err => {
        // console.log(err); // [Error: EBADF: bad file descriptor, close]
    });
});

let fileHandle = await open('/path/to/file', 'r');

// register the file descriptor
registry.register(fileHandle, fileHandle.fd);

// release the reference intentionally
setTimeout(() => fileHandle = null, 1000);

// run garbage collector
setTimeout(() => global?.gc?.(), 2000);

await new Promise(_ => setTimeout(_, 3000));

However, before the FinalizationRegistry callback is executed, the node.js "auto-closing" is run and warned.

$ node --expose-gc test.mjs 
(node:6796) Warning: Closing file descriptor 3 on garbage collection
(Use `node --trace-warnings ...` to show where the warning was created)
(node:6796) [DEP0137] DeprecationWarning: Closing a FileHandle object on garbage collection is deprecated. Please close FileHandle objects explicitly using FileHandle.prototype.close(). In the future, an error will be thrown if a file descriptor is closed during garbage collection.

This "auto-closing" is a great feature for safety reasons, but if the developer has implemented their own cleanup process, could you delay the timing of its execution a bit so that it doesn't get warned?

I would be happy if you will consider it.

@Ayase-252 Ayase-252 added fs Issues and PRs related to the fs subsystem / file system. promises Issues and PRs related to ECMAScript promises. labels May 22, 2021
@king-of-poppk
Copy link

king-of-poppk commented Jan 23, 2024

Have you tried passing the fileHandle instead of the file descriptor as the cleanup value? Also MDN says:

A conforming JavaScript implementation, even one that does garbage collection, is not required to call cleanup callbacks. When and whether it does so is entirely down to the implementation of the JavaScript engine. When a registered object is reclaimed, any cleanup callbacks for it may be called then, or some time later, or not at all.

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. promises Issues and PRs related to ECMAScript promises.
Projects
None yet
Development

No branches or pull requests

3 participants