question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Require fails when run in a process without stdin

See original GitHub issue

Overview

I have a Linux 5.4 server which I am running a NodeJS expressjs app on. This app uses prom-client.

If I run the app in an interactive terminal everything works fine. However if I run the app in the background it crashes. If I remove require('prom-client') (and only this) my app works fine in the background.

It looks like prom-client is trying to read stdin on import, and is failing, thus crashing my process.

Expected Behavior

When I run my NodeJS application without any stdin (in a non-interactive terminal) it should work.

Actual Behavior

When I run my NodeJS application without any stdin it crashes with the error:

+ exec node index.js
hello world
events.js:291
       throw er; // Unhandled 'error' event
       ^
 Error: read ENOTCONN
     at tryReadStart (net.js:574:20) 
     at ReadStream.Socket._read (net.js:585:5)
     at ReadStream.Readable.read (_stream_readable.js:475:10)
     at ReadStream.Socket.read (net.js:625:39)
     at ReadStream.Socket (net.js:377:12)
     at new ReadStream (tty.js:58:14)
     at process.getStdin [as stdin] (internal/bootstrap/switches/is_main_thread.js:151:15)
     at get (<anonymous>)
     at getOwn (internal/bootstrap/loaders.js:150:5)
     at NativeModule.syncExports (internal/bootstrap/loaders.js:258:31)
	 Emitted 'error' event on ReadStream instance at:
     at emitErrorNT (internal/streams/destroy.js:100:8)
     at emitErrorCloseNT (internal/streams/destroy.js:68:3)
     at processTicksAndRejections (internal/process/task_queues.js:80:21) {
	 errno: -107,
	 code: 'ENOTCONN',
	 syscall: 'read'
}

Details

I have isolated the cause of the crash to require('prom-client'). If I don’t have this require statement in my app everything works fine.

Here is a 2 line self contained example.

It seems like the program is getting the error ENOTCONN on a read system call for the stdin file descriptor.

The fix to this error is to pipe some sort of stdin to the process in the background:

: | node index.js

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
edwardofcltcommented, Dec 2, 2021

Came across this today and this solved my issue. Thanks for this.

0reactions
Noah-Huppertcommented, Jan 24, 2021

@zbjornson understandable I feel like something downstream from you is causing this issue. Hopefully the workarounds we’ve both posted will help anyone who comes across this from google 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js child process isn't receiving stdin unless I close the ...
In the Node.js docs for child process stdin, it says "Note that if a child process waits to read all of its input,...
Read more >
Process | Node.js v19.3.0 Documentation
The 'beforeExit' event is emitted when Node.js empties its event loop and has no additional work to schedule. Normally, the Node.js process will...
Read more >
Working with stdout and stdin of a child process in Node.js
Let's start with running a shell command in a child process: const {onExit} = require('@rauschma/stringio'); const {spawn} = require ...
Read more >
SSH inside SSH fails with "stdin: is not a tty"
By default, when you run a command on the remote machine using ssh, a TTY is not allocated for the remote session. This...
Read more >
os/exec - Go Packages
They may not run on Windows, and they do not run in the Go Playground used by golang.org ... If Stdin is nil,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found