Coder Social home page Coder Social logo

any-signal's People

Contributors

achingbrain avatar dependabot[bot] avatar gozala avatar jacobheun avatar mpetrunic avatar semantic-release-bot avatar ukstv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

any-signal's Issues

Potential memory leak by only adding handlers

Consider code in the README file:

const anySignal = require('any-signal')

const userController = new AbortController()
const timeoutController = new AbortController()

const combinedSignal = anySignal([userController.signal, timeoutController.signal])
combinedSignal.addEventListener('abort', () => console.log('Abort!'))

// Abort after 1 second
const timeoutId = setTimeout(() => timeoutController.abort(), 1000)

// The user or the timeout can now abort the action
await performSomeAction({ signal: combinedSignal })
clearTimeout(timeoutId)

After all the lines are executed, there are still event handlers attached to userController.signal and timeoutController.signal. There is no code that clears them. It all works fine if userController and timeoutController both are garbage collected after clearTimeout.

Let's consider the following code, where an AbortController is long-lived, along with its AbortSignal.

class Loader {
  shutdownController = new AbortController()

  async load(id: string, options: { signal: AbortSignal }) {
    const effectiveSignal = anySignal([this.shutdownController.signal, options.signal])
    await doSomething(effectiveSignal)
    return "Success!"
  }
}

On every call to load method, you'd add an event handler to this.shutdownController.signal. It never gets removed, which leads to some minor but very constant memory leak, as well as some annoying warnings, like libp2p/js-libp2p#900

Are you open to PRs fixing this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.