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

process.stdin still fetch bytes after clossing file descriptor 0 #12872

Closed
piranna opened this issue May 6, 2017 · 3 comments
Closed

process.stdin still fetch bytes after clossing file descriptor 0 #12872

piranna opened this issue May 6, 2017 · 3 comments
Labels
process Issues and PRs related to the process subsystem. question Issues that look for answers.

Comments

@piranna
Copy link
Contributor

piranna commented May 6, 2017

#!/usr/bin/env node

const close = require('fs').close

process.stdin.pipe(process.stdout)

close(0, function(err)
{
  console.log('stdin clossed', err)
})

When clossing the file descriptor zero (stdin), if something is still witten to it Node.js still fetchs that info. Since new file descriptors (both by opening files and/or using the unistd module) start at position 12, seems like Node.js itself is internally duplicating fd 0 (probably in the process.stdin stream, in fact process.stdin.fd is 0 but process.stdin._handle.TTY.fd is 9), making it difficult to manipulate the file descriptors at low level, for example when developing interactive shells like nsh where background tasks should be still running (so spawnSync() is not an option).

Probably related to #5574.

@Fishrock123
Copy link
Contributor

Please see https://nodejs.org/dist/latest-v7.x/docs/api/process.html#process_a_note_on_process_i_o -- I do not think you can close stdin.

e.g., in the repl:

> process.stdin.close()
TypeError: process.stdin.close is not a function
    at repl:1:15
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:96:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:349:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:548:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:188:7)

@addaleax
Copy link
Member

addaleax commented May 6, 2017

I do not think you can close stdin.

This is not about process.stdin as a stream, though.

Here is why libuv dup()s the file descriptor – ironically, Node doesn’t use non-blocking TTYs anymore.

I’m not sure there’s much to do here (unless you have concrete suggestions?). If you want the tip, you can use compare the fs.fstat(Sync) outputs to check whether two FDs refer to the same file, or just use process.stdin._handle.fd directly (even though it’s a private API).

@mscdex mscdex added process Issues and PRs related to the process subsystem. question Issues that look for answers. labels May 6, 2017
@Trott
Copy link
Member

Trott commented Aug 7, 2017

This issue has been inactive for sufficiently long that it seems like perhaps it should be closed. Feel free to re-open (or leave a comment requesting that it be re-opened) if you disagree. I'm just tidying up and not acting on a super-strong opinion or anything like that.

@Trott Trott closed this as completed Aug 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
process Issues and PRs related to the process subsystem. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

5 participants