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

JavaScript file executed twice when using pino/file transport with --import option #1049

Closed
richterich opened this issue Aug 15, 2024 · 5 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@richterich
Copy link

💬 Question

When using the pino/file transport with the --import option to include a JavaScript file, the file is executed twice instead of just once. This behavior looks as unexpected and can lead to issues such as duplicated log entries or other side effects from the script running twice. Should I report an issue or is there a solution for this problem?

To reproduce this issue, please, create fastify instance with following options:

// index.js
const app = fastify({
  logger: {
    level: 'debug',
    transport: {
      target: 'pino/file',
      options: { destination: './tmp/app.log', mkdir: true },
    },
  },
});

app.listen({ port: 8080, host: '127.0.0.1' }, (err, address) => {
  if (err) {
    instance.log.error({ err, address }, 'Server Start Error');
    process.exit(1);
  }
});

console.log("once");

Create another module with the following code:

// twice.js
console.log("twice");

Run application with command:

node --import=./twice.js index.js

Actual output:

twice
once
twice

Expected output:

twice
once

Please, see the repo to reproduce the problem.

Environment

  • node version: 20.16.0
  • fastify version: 4.28.1
  • os: Linux 6.5.0-45-generic Ubuntu 22.04.1

Additional

As workaround it possible use true in logger options or don't use the transport option in logger options.

// use boolean value
const app = fastify({
  logger: true
});

// without transport option
const app = fastify({
  logger: {
    level: 'debug',
  },
});
@richterich richterich added the help wanted Extra attention is needed label Aug 15, 2024
@dosubot dosubot bot added the question Further information is requested label Aug 15, 2024
@climba03003
Copy link
Member

It is more like an issue from pino or node.js itself.

@jsumners
Copy link
Member

Pino does not modify console.log. Nothing in the report is due to Fastify nor Pino. --import loads the designated script in a separate thread. There must be something omitted from index.js that shows where it is also importing twice.js.

@climba03003

This comment was marked as outdated.

@climba03003
Copy link
Member

climba03003 commented Aug 16, 2024

Here is the minimal repro with file creation.
The --import script will be run in every Worker creation. If that is not the desire behavior, you should file an issue to node repository.

import { spawn } from 'node:child_process';
import { rm, writeFile } from 'node:fs/promises';

await writeFile('main.mjs', `
import { rm, writeFile } from 'node:fs/promises';
import { Worker } from 'node:worker_threads';

await writeFile('worker.mjs', \`console.log('worker')\`)

const worker = new Worker('./worker.mjs')

process.once('beforeExit', () => {
  rm('worker.mjs')
})`)

await writeFile('import.mjs', `console.log('import')`)

spawn('node', ['--import', './import.mjs', 'main.mjs'], {
  stdio: 'inherit'
})

process.once('beforeExit', () => {
  rm('main.mjs')
  rm('import.mjs')
})

@climba03003 climba03003 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 16, 2024
@richterich
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants