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

next: worker-thread messages not log when pino used in both main-thread and worker-thread #1095

Closed
climba03003 opened this issue Aug 23, 2021 · 6 comments · Fixed by #1097
Closed

Comments

@climba03003
Copy link
Contributor

climba03003 commented Aug 23, 2021

When I use pino in both main-thread and worker-thread. Only the main-thread can log messages.

Repro Link: https://replit.com/@climba03003/pino-issue-1095

// index.js
const { logger } = require('./logger.js')
const { Worker } = require('worker_threads')

const worker = new Worker('./pino.js')
setTimeout(function() {
  worker.terminate()
  process.exit(0)
}, 3000)

logger.info('main - show log')
logger.info('main - show log')
logger.info('main - show log')
logger.info('main - show log')
logger.info('main - show log')
logger.info('main - show log')
logger.info('main - show log')
logger.info('main - show log')

// logger.js
const pino = require('pino')

const logger = pino({ level: 'trace' }, pino.transport({
  targets: [
    { level: 'info', target: '#pino/file', options: {} }
  ]
}))

module.exports.logger = logger

// pino.js
const { logger } = require('./logger.js')

logger.info('worker - should show')
logger.info('worker - should show')
logger.info('worker - should show')
logger.info('worker - should show')
logger.info('worker - should show')
logger.info('worker - should show')
logger.info('worker - should show')

process.exit(0)
@climba03003 climba03003 changed the title next: messages not show when both main-thread and worker-thread using pino next: worker-thread messages not log when pino used in both main-thread and worker-thread Aug 23, 2021
@mcollina mcollina added the bug label Aug 23, 2021
@mcollina
Copy link
Member

Ah, this looks like a bug (!).

@mcollina
Copy link
Member

Removing process.exit(0) from pino.js fixes it. The hooks that we use beforeExit and exit do not work the same in worker_threads so we cannot hook things correctly.

Maybe you could send a PR?

@climba03003
Copy link
Contributor Author

climba03003 commented Aug 23, 2021

I do not know if I can comes up a solution. I have tested a lot of method, it seems that when ever I called process.exit inside the worker_threads, the worker is exit faster than pino can do anything (include register a process hook).

It would be better to use process.emit('beforeExit') or pino.final to terminate inside worker.

// let pino to handle the stream flush before exit
process.emit('beforeExit')
// terminate the worker process
process.once('beforeExit', function() {
  // create some time buffer for pino to clear logs
  setTimeout(() => process.exit(0), 1000)
})
pino.final(logger, function() {
  process.exit(0)
})

@JekRock
Copy link

JekRock commented Aug 24, 2021

From my understanding, the issue is that process.on doesn't deliver messages inside a worker thread.
From the documentation:

Notable differences inside a Worker environment are:

  • Signals will not be delivered through process.on('...').

I don't see a way to attach a listener to the exit event from inside a worker thread when process.exit(0) is called though.

@mcollina
Copy link
Member

mcollina commented Aug 24, 2021

const { logger } = require('./b.js')

logger.info('main - show log')
logger.info('main - show log')
logger.info('main - show log')
logger.info('main - show log')
logger.info('main - show log')
logger.info('main - show log')
logger.info('main - show log')
logger.info('main - show log')

process.exit(0)

The process.exit(0) will block the worker thread initialization. It's not possible to make it work :/.

@github-actions
Copy link

github-actions bot commented Feb 3, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants