nodejs / nodejs/node

SEA + cluster.fork on Windows prepends executable path into worker process.argv (breaks argv parsing)

Ouverte
#62,776 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
JavaScript
Étoiles
122k
Forks
37.3k
Merge moyen
4 j 2 h
PR mergées (30 j)
283

Description

Version

v24.15.0

Platform
Microsoft Windows NT 10.0.26200.0 x64
Subsystem

single executable applications (SEA), cluster.fork

What steps will reproduce the bug?
Minimal reproduction idea
  1. Build a SEA executable whose entry:
    • Prints process.pid, role, process.execPath, and process.argv.slice(2)
    • If primary: cluster.fork() one worker
    • If worker: print argv then exit
  2. Run executable with user args, for example:
    app.exe command --flag value
  3. Compare primary vs worker argv slice(2).

Primary shows:
["foo","bar"]

Worker shows:
["X:\\...\\app.exe","foo","bar"]

Node.js versions:
  • v24.15.0 (reproducible)
  • v22.16.0 (reproducible)
  • Historically observed since Node 20 era in our project
How often does it reproduce? Is there a required condition?

100% reproducible under these conditions: (1) the application is built as a SEA executable, (2) the primary process uses cluster.fork() to spawn workers, (3) tested on Windows (Linux not verified). Running the same logic with plain node script.mjs does not reproduce the issue — the extra token only appears in SEA-built worker processes.

What is the expected behavior? Why is that the expected behavior?

Observed pattern:

  • Primary process process.argv.slice(2):
    ["command","--flag","value"]
  • Worker process process.argv.slice(2):
    ["X:\\path\\to\\app.exe","command","--flag","value"]
Expected behavior

Worker processes in SEA mode should see the same user command arguments as primary for process.argv.slice(2), without an injected executable path token.

What do you see instead?
Actual behavior

Worker process argv includes an extra executable path token before actual user arguments.

Additional information
Control test

Running equivalent script without SEA (node script.mjs foo bar) does not show this divergence.

Impact
  • Breaks multi-process startup paths in production for SEA apps using argv-based command routing.
  • Particularly problematic when a process model uses cluster workers that execute the same CLI entrypoint.
Current workaround

Application-side argv normalization — filter any token in process.argv.slice(2) that equals process.execPath before handing off to the command parser:

import path from 'node:path';

function normalizePathname(p) {
  return path.resolve(p).toLowerCase();
}

function isSelfExecutableArg(arg) {
  if (typeof arg !== 'string' || arg.trim() === '' || arg.startsWith('-')) {
    return false;
  }
  return normalizePathname(arg) === normalizePathname(process.execPath);
}

function normalizeArgv(rawArgv) {
  return rawArgv.filter(arg => !isSelfExecutableArg(arg));
}

// Usage
const argv = normalizeArgv(process.argv.slice(2));

Key points:

  • Case-insensitive path comparison is required on Windows (path.resolve + .toLowerCase()).
  • The filter must skip tokens starting with - to avoid accidentally dropping flags that happen to match.
  • The injected token can appear anywhere in slice(2), not just at index 0, so a .filter over the full array is safer than removing index 0 unconditionally.

This avoids the crash but seems like a workaround for runtime behavior that may be unintended.

Additional notes

If needed, I can provide a complete minimal repro repository.

Signed-off-by

GitHub Copilot

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par construire l’exécutable SEA décrit dans l’issue et comparez process.argv.slice(2) dans le primary et dans le worker de cluster.fork() sous Windows. Suivez le chemin de démarrage du worker SEA qui construit process.argv ; le travail est terminé lorsque le worker conserve les arguments utilisateur du primary sans chemin d’exécutable injecté, et que le cas de contrôle non-SEA continue de réussir.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript, nodejs
Domaine
backend, operating-systems
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
Calme
Clarté
Plutôt claire
Accessibilité débutants
48/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.