nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
107.15k stars 29.37k forks source link

node:repl docs code not working (under "Starting multiple REPL instances against a single running instance") #43118

Open martian17 opened 2 years ago

martian17 commented 2 years ago

Affected URL(s)

https://nodejs.org/docs/latest/api/repl.html

Description of the problem

Details

Under Starting multiple REPL instances against a single running instance there is a code that demonstrates the sharing of a single global object among multiple repl instances. However, the code in question does not work. That is, when I run this code and connect to it with telnet localhost 5001, the variable that I define in the telnet instance does not get reflected in the normal stdin/out instance. I found that this is most likely because useGlobal option is set to false by default in repl.start, as explained in the same document. To enable this, I needed to explicitly set this to true in repl.start argument like this. Now the code works as expected.

const net = require('node:net');
const repl = require('node:repl');
let connections = 0;

repl.start({
  prompt: 'Node.js via stdin> ',
  input: process.stdin,
  output: process.stdout,
  useGlobal: true   //modified
});

net.createServer((socket) => {
  connections += 1;
  repl.start({
    prompt: 'Node.js via Unix socket> ',
    input: socket,
    output: socket,
    useGlobal: true //modified
  }).on('exit', () => {
    socket.end();
  });
}).listen('/tmp/node-repl-sock');

net.createServer((socket) => {
  connections += 1;
  repl.start({
    prompt: 'Node.js via TCP socket> ',
    input: socket,
    output: socket,
    useGlobal: true //modified
  }).on('exit', () => {
    socket.end();
  });
}).listen(5001);

Also, there is a second but minor problem in this code. After I exit and try to execute the script again, the following error is thrown.

Node.js via stdin> node:events:371
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use /tmp/node-repl-sock
    at Server.setupListenHandle [as _listen2] (node:net:1302:21)
    at listenInCluster (node:net:1367:12)
    at Server.listen (node:net:1465:5)
    at Object.<anonymous> (/Users/yoshi/prog/repl/telnet2.js:22:4)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1346:8)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'EADDRINUSE',
  errno: -48,
  syscall: 'listen',
  address: '/tmp/node-repl-sock',
  port: -1
}

To get this to work again, I had to remove /tmp/node-repl-sock file from my laptop. I don't have a good idea on how Unix socket works, but I think there should be an additional cleanup process in the code.

Here are my system info just for the reference.

Node.js version

v16.8.0

Operating system

macOS Mojave 10.14.6

I originally filed this issue on nodejs/help, but found out later that the main repo also accepts the docs issue, so I'm re-posting it here.

martian17 commented 2 years ago

Confirmed the problem on Ubuntu 20.04 node v16.15.0. Also confirmed that the same counter measure was effective (useGlobal: true in repl.start() for the first problem and removing /tmp/node-repl-sock for the second problem)

mscdex commented 2 years ago

In general, named UNIX sockets are not automatically removed from the file system.