镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

worker: parentPort.postMessage fails on undefined value #26122

Description

@legendecas
  • Version: 11.9.0
  • Platform: all
  • Subsystem: worker

parentPort.postMessage fails on assumption of not nil of first argument. Though it's documented as accepting any value.

internal/worker/io.js:65
  if (payload.type !== messageTypes.STDIO_WANTS_MORE_DATA)
              ^

TypeError: Cannot read property 'type' of undefined
    at MessagePort.onmessage (internal/worker/io.js:65:15)

full scripts:

const {
  Worker, isMainThread, parentPort, workerData
} = require('worker_threads')

if (isMainThread) {
    const worker = new Worker(__filename);
    worker.on('message', data => console.log('received worker data', data));
    worker.on('error', err => console.error('worker error:', error));
    worker.on('exit', (code) => {
      if (code !== 0)
        console.error(`Worker stopped with exit code ${code}`);
    })
    return
}

parentPort.postMessage(undefined)

Activity

  1. Abolfazl-MI commented on Nov 1, 2024

    @Abolfazl-MI

    I have this code in my worker file which i trigger but this method dose not sending the selected data to up

     _selectRandomQuestion(){
            // get random number base on the question length
            const randomIndex=Math.floor(Math.random()*this.#match_question.length)
            // storing selected question
            this.#_selectedQuestion=this.#match_question[randomIndex]
            console.log('selected question =>')
            console.log(this.#_selectedQuestion)
            // removing selected question from array
            this.#match_question.splice(randomIndex,1)
            // sends selected question out of worker
            return parentPort.postMessage({
                "E":quizGameEventNames.NEXT_QUESTION,
                "D":{
                    "data":{
                        "question":this.#_selectedQuestion
                    }
                }
            })
        }
        ```
        the worker.on("message") dosen't get anything why?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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