Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix yieldThread memory leak #338

Merged
merged 1 commit into from
Dec 30, 2023
Merged

fix yieldThread memory leak #338

merged 1 commit into from
Dec 30, 2023

Conversation

syusui-s
Copy link
Contributor

I noticed that the current implementation of yieldThread will consume memory.
The message channel will never be used after the call but there is still a 'message' call-back function so GC cannot free it.

It is needed to call removeEventHandler.

Detail

If you run this script with node command like node script.js, the process will never finish.

async function yieldThread() {
  return new Promise(resolve => {
    const ch = new MessageChannel()
    ch.port1.addEventListener('message', resolve)
    ch.port2.postMessage(0)
    ch.port1.start()
  })
}

yieldThread()

If there is removeEventListener, it will finish successfully.

async function yieldThread() {
  return new Promise(resolve => {
    const ch = new MessageChannel()
    const handler = () => {
      ch.port1.removeEventListener('message', handler)
      resolve()
    }
    ch.port1.addEventListener('message', handler)
    ch.port2.postMessage(0)
    ch.port1.start()
  })
}

yieldThread();

@fiatjaf fiatjaf merged commit 4cfc67e into nbd-wtf:master Dec 30, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants