-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Register process.on('exit') without leaking an handler #48058
Comments
The problem i had when using I did truly want to know when a reference was garbage collected durning a long living process that never shutdown so that i could go ahead and delete/unlink a temporary That was not something that But what would have been even better is if the things i registered in hence my proposal of: new FinalizationRegistry(fn, {
runOnExit: true
}) import { createTemporaryBlob, createTemporaryFile } from 'fetch-blob/from.js'
const response = await fetch('https://httpbin.org/image/png')
// pipes the data to OS- temp folder, with a unic/random name, and return a disk- based blob
let blob = await createTemporaryBlob(response.body, { type: 'image/png' })
// Sometime later
// loosing references to blob would then delete the file from disk
// with `runOnExit: true` It would also clean things up when process exit
blob = undefined the const registry = new FinalizationRegistry(unlink)
const tempDir = await mkdtemp(realpathSync(tmpdir()) + sep)
/**
* Creates a temporary blob backed by the filesystem.
* NOTE: requires node.js v14 or higher to use FinalizationRegistry
*
* @param {*} data Same as fs.writeFile data
* @param {BlobPropertyBag & {signal?: AbortSignal}} options
* @param {AbortSignal} [signal] in case you wish to cancel the write operation
* @returns {Promise<Blob>}
*/
async function createTemporaryBlob (data, {signal, type} = {}) {
const destination = join(tempDir, crypto.randomUUID())
await writeFile(destination, data, { signal })
const blob = await openAsBlob(destination, { type })
registry.register(blob, destination)
return blob
} |
A good use case for having this Even for |
Given that @jimmywarting Relying on |
Why should
By who? Why is it a "anti-pattern"? Different developers or teams may have different opinions on what constitutes good or bad practices in software development. What is considered an anti-pattern in one situation may be an appropriate solution in another, depending on factors such as project requirements, constraints, trade-offs, and the specific technologies or frameworks being used. There are many things that are considered as a "anti-pattern" like deffered promises, but ppl use it anyway. I could tell you that browser dose this magic all the time. const url = 'https://freetestdata.com/wp-content/uploads/2022/02/Free_Test_Data_10MB_MOV.mov'
fetch(url).then(res => res.blob()) Then browser will store this in a "blob-storage" on the disk. They are saved in They are magically resolved for developers. (if you would have tried to get it as
I haven't build any service that is long lived. I have built the And the fetch api dose not provide any mean of cleaning this file up after itself. And it would be bad if we tried to allocate really large constructed Developers that passes this blob around to other libraries in order to parse it and process it like a zip library wouldn't know when it would be safe to delete the blob from the disk and calling a And we don't want to add the burden to their user to call a i can understand your view point on not doing specialized NodeJS behavior on |
@jimmywarting If you insist on 👎'ing my comment and don't want to take my word for it, how about that of the folks who invented
That last sentence explicitly says that your idea is considered an anti-pattern by the very authors of |
+1 on not touching the We could expose a |
There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment. For more information on how the project manages feature requests, please consult the feature request management document. |
There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment. For more information on how the project manages feature requests, please consult the feature request management document. |
What is the problem this feature will solve?
In some cases, modules might want to set up some clean-up scripts for clearing some resources gracefully before everything is shut down. However, they do not want to install a global
'exit'
handler because then they would not know when to remove it and they do not want to add the burden to their user to call aclose()
function.What is the feature you are proposing to solve the problem?
A few years back I wrote
on-exit-leak-free
to do just that. Using it is straightfoward.Moreover in mcollina/on-exit-leak-free#31 (comment), @jimmywarting is proposing an even better syntax:
What alternatives have you considered?
No response
The text was updated successfully, but these errors were encountered: