### 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`](https://github.com/mcollina/on-exit-leak-free) to do just that. Using it is straightfoward. ```js '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 https://github.com/mcollina/on-exit-leak-free/issues/31#issuecomment-1294973971, @jimmywarting is proposing an even better syntax: ```js new FinalizationRegistry(fn, { runOnExit: true }) ``` ### What alternatives have you considered? _No response_