Skip to content

Use FinalizationRegistry to warn for leaked resources #34711

@ronag

Description

@ronag

Just playing a bit with the concept of using FinalizationRegistry to detect when/if the user has forgotten to properly release a resource and print a corresponding warning?

Something like:

const fsStreamRegistery = new FinalizationRegistry(state => {
  if (!state.destroyed) {
    process.warning('file stream leak')
  }
})

function createReadStream(...) {
  const stream = new ReadStream(...)
  fsStreamRegistery.register(stream, stream._readableState)
  return stream
}

Potential candidates:

  • net.Socket
  • fs.FileHandle
  • fs.WriteStream
  • fs.ReadStream

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions