What is the problem this feature will solve?
Problem
Node.js applications frequently suffer from resource leaks when developers forget to properly close files, sockets, timers, or other native resources. This is especially problematic when exceptions occur before cleanup code executes.
// Current problematic pattern
function processFile(filename) {
let fd = fs.openSync(filename, 'r')
let timer = setInterval(() => console.log('processing'), 1000)
// If exception occurs here, resources leak
if (someCondition) {
throw new Error('Processing failed')
}
// Cleanup code may never execute
fs.closeSync(fd)
clearInterval(timer)
}
What is the feature you are proposing to solve the problem?
Add a built-in ResourceScope API that automatically cleans up registered resources when the scope is disposed, even during exception.
{
using scope = new ResourceScope()
// Auto cleanup well-known nodejs objects.
let timer = setTimeout(() => console.log('tick'), 1000) // auto clearTimeout
let interval = setInterval(() => console.log('interval'), 500) // auto clearInterval
let fd = fs.openSync('file.txt', 'r') // auto fs.closeSync
let stream = fs.createReadStream('data.txt') // auto stream.close()
let server = http.createServer() // auto server.close()
let child = spawn('node', ['script.js']) // auto child.kill()
}
What alternatives have you considered?
import { ResourceScope } from 'node:resource'
function processFile(filename) {
using scope = new ResourceScope()
let fd = scope.register(fs.openSync(filename, 'r'), fs.closeSync)
let timer = scope.register(
setInterval(() => console.log('processing'), 1000),
clearInterval
)
// Process file...
// Resources automatically cleaned up on scope exit
}
What is the problem this feature will solve?
Problem
Node.js applications frequently suffer from resource leaks when developers forget to properly close files, sockets, timers, or other native resources. This is especially problematic when exceptions occur before cleanup code executes.
What is the feature you are proposing to solve the problem?
Add a built-in ResourceScope API that automatically cleans up registered resources when the scope is disposed, even during exception.
What alternatives have you considered?