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

node v12.2.0 worker_threads worker number > 10 EventEmitter warning #27687

Closed
AlfieriChou opened this issue May 14, 2019 · 1 comment · Fixed by #27691
Closed

node v12.2.0 worker_threads worker number > 10 EventEmitter warning #27687

AlfieriChou opened this issue May 14, 2019 · 1 comment · Fixed by #27691
Labels
console Issues and PRs related to the console subsystem. worker Issues and PRs related to Worker support.

Comments

@AlfieriChou
Copy link

  • Version: 12.2.0
  • Platform: macos 10.12.6
  • Subsystem:
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads')
const min = 2
let primes = []
function generatePrimes (start, range) {
  let isPrime = true
  let end = start + range
  for (let i = start; i < end; i++) {
    for (let j = min; j < Math.sqrt(end); j++) {
      if (i !== j && i % j === 0) {
        isPrime = false
        break
      }
    }
    if (isPrime) {
      primes.push(i)
    }
    isPrime = true
  }
}
if (isMainThread) {
  const max = 1e7
  const threadCount = +process.argv[2] || 2
  const threads = new Set()
  console.log(`Running with ${threadCount} threads...`)
  const range = Math.ceil((max - min) / threadCount)
  let start = min
  for (let i = 0; i < threadCount - 1; i++) {
    const myStart = start
    threads.add(new Worker(__filename, { workerData: { start: myStart, range } }))
    start += range
  }
  threads.add(new Worker(__filename, { workerData: { start, range: range + ((max - min + 1) % threadCount) } }))
  for (let worker of threads) {
    worker.on('error', (err) => { throw err })
    worker.on('exit', () => {
      threads.delete(worker)
      console.log(`Thread exiting, ${threads.size} running...`)
      if (threads.size === 0) {
        console.log(primes.join('\n'))
      }
    })
    worker.on('message', (msg) => {
      primes = primes.concat(msg)
    })
  }
} else {
  generatePrimes(workerData.start, workerData.range)
  parentPort.postMessage(primes)
}
node demo.js 10
// (node:10552) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added. Use emitter.setMaxListeners() to increase limit

maybe should add emitter alias. to avoid this warning.

@cjihrig
Copy link
Contributor

cjihrig commented May 14, 2019

Proposed fix in #27691.

@Fishrock123 Fishrock123 added worker Issues and PRs related to Worker support. console Issues and PRs related to the console subsystem. labels May 14, 2019
cjihrig added a commit to cjihrig/node that referenced this issue May 16, 2019
A noop error handler is attached to the console's stream on
write. The handler is then immediately removed after the write.
This commit skips adding the error handler if one already
exists.

PR-URL: nodejs#27691
Fixes: nodejs#27687
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
targos pushed a commit that referenced this issue May 17, 2019
A noop error handler is attached to the console's stream on
write. The handler is then immediately removed after the write.
This commit skips adding the error handler if one already
exists.

PR-URL: #27691
Fixes: #27687
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
console Issues and PRs related to the console subsystem. worker Issues and PRs related to Worker support.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants