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

src: ensure that fd 0-2 are valid on windows #11863

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4213,6 +4213,17 @@ inline void PlatformInit() {
} while (min + 1 < max);
}
#endif // __POSIX__
#ifdef _WIN32
for (int fd = 0; fd <= 2; ++fd) {
auto handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
if (handle == INVALID_HANDLE_VALUE ||
GetFileType(handle) == FILE_TYPE_UNKNOWN) {
_close(fd);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_close() can't fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fails on Windows 2008 and 2012 but succeeds on Windows 10. IMHO the cleanest way is to just assert that _open returned proper fd.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it known why it can fail on 2008/2012 and can that be worked around at all? I'm good with this as is but I'm curious :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Win2008 and 2012 fail with errno EBADF. Win 10 happily closes the handle. But regardless of that, _open works as expected on all Win versions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bzoz actually, having this explanation as a comment in the source seems like a good idea to me?

if (fd != _open("nul", _O_RDWR))
ABORT();
}
}
#endif // _WIN32
}


Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/spawn_closed_stdio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os
import sys
import subprocess
os.close(0)
os.close(1)
os.close(2)
exit_code = subprocess.call(sys.argv[1:], shell=False)
sys.exit(exit_code)
14 changes: 13 additions & 1 deletion test/parallel/test-stdio-closed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@ const common = require('../common');
const assert = require('assert');
const spawn = require('child_process').spawn;
const fs = require('fs');
const path = require('path');

if (common.isWindows) {
common.skip('platform not supported.');
if (process.argv[2] === 'child') {
process.stdin;
process.stdout;
process.stderr;
return;
}
const python = process.env.PYTHON || 'python';
const script = path.join(common.fixturesDir, 'spawn_closed_stdio.py');
const proc = spawn(python, [script, process.execPath, __filename, 'child']);
proc.on('exit', common.mustCall(function(exitCode) {
assert.strictEqual(exitCode, 0);
}));
return;
}

Expand Down