Bird
0
0

This Node.js code aims to create two worker processes but has a bug:

medium📝 Debug Q14 of 15
Node.js - Cluster Module
This Node.js code aims to create two worker processes but has a bug:
const cluster = require('cluster');
if (cluster.isMaster) {
  cluster.fork();
} else {
  cluster.fork();
  console.log('Worker running');
}

What is the main problem?
ACalling cluster.fork() inside the worker causes infinite worker creation.
BMissing cluster.isMaster check before forking.
Cconsole.log is inside the master process, so no output.
Dcluster.fork() is not a valid method.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze fork calls in master and worker

    Master forks once, but worker also calls cluster.fork(), creating new workers repeatedly.
  2. Step 2: Identify infinite worker creation

    Workers keep forking new workers endlessly, causing a loop and resource exhaustion.
  3. Final Answer:

    Calling cluster.fork() inside the worker causes infinite worker creation. -> Option A
  4. Quick Check:

    Fork inside worker = infinite forks [OK]
Quick Trick: Only master should call cluster.fork() to avoid infinite loops [OK]
Common Mistakes:
  • Thinking workers can safely fork new workers
  • Ignoring cluster.isMaster condition
  • Assuming cluster.fork() is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes