-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
Closed as not planned
Labels
feature requestIssues that request new features to be added to Node.js.Issues that request new features to be added to Node.js.stale
Description
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 a close()
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.
'use strict'
const { register, unregister } = require('on-exit-leak-free')
const assert = require('assert')
function setup () {
// This object can be safely garbage collected,
// and the resulting shutdown function will not be called.
// There are no leaks.
const obj = { foo: 'bar' }
register(obj, shutdown)
// use registerBeforeExit(obj, shutdown) to execute the function only
// on beforeExit
// call unregister(obj) to remove
}
let shutdownCalled = false
// Please make sure that the function passed to register()
// does not create a closure around unnecessary objects.
function shutdown (obj, eventName) {
console.log(eventName) // beforeExit
shutdownCalled = true
assert.strictEqual(obj.foo, 'bar')
}
setup()
process.on('exit', function () {
assert.strictEqual(shutdownCalled, true)
})
Moreover in mcollina/on-exit-leak-free#31 (comment), @jimmywarting is proposing an even better syntax:
new FinalizationRegistry(fn, {
runOnExit: true
})
What alternatives have you considered?
No response
jimmywarting
Metadata
Metadata
Assignees
Labels
feature requestIssues that request new features to be added to Node.js.Issues that request new features to be added to Node.js.stale