You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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';constregistry=newFinalizationRegistry(fd=>{console.log('FinalizationRegistry callback');close(fd,err=>{// console.log(err); // [Error: EBADF: bad file descriptor, close]});});letfileHandle=awaitopen('/path/to/file','r');// register the file descriptorregistry.register(fileHandle,fileHandle.fd);// release the reference intentionallysetTimeout(()=>fileHandle=null,1000);// run garbage collectorsetTimeout(()=>global?.gc?.(),2000);awaitnewPromise(_=>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.
The text was updated successfully, but these errors were encountered:
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
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.
fs/promises
I tried to cleanup the unclosed
FileHandle
using theFinalizationRegistry
as shown in the following code example.test.mjs
However, before the
FinalizationRegistry
callback is executed, the node.js "auto-closing" is run and warned.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.
The text was updated successfully, but these errors were encountered: