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

Conversation

bzoz
Copy link
Contributor

@bzoz bzoz commented Mar 15, 2017

Check that stdin, stdout and stderr are valid file descriptors on Windows. If not, reopen them with nul file.

This is a port of #875 for Windows.

Fixes: #11656

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

src

Check that stdin, stdout and stderr are valid file descriptors on
Windows. If not, reopen them with 'nul' file.

Refs: nodejs#875
Fixes: nodejs#11656
@bzoz bzoz added c++ Issues and PRs that require attention from people who are familiar with C++. windows Issues and PRs related to the Windows platform. labels Mar 15, 2017
@nodejs-github-bot nodejs-github-bot added the c++ Issues and PRs that require attention from people who are familiar with C++. label Mar 15, 2017
@mscdex mscdex added the process Issues and PRs related to the process subsystem. label Mar 15, 2017
@bzoz
Copy link
Contributor Author

bzoz commented Mar 15, 2017

cc @nodejs/platform-windows

@bzoz
Copy link
Contributor Author

bzoz commented Mar 15, 2017

CI failed on win2008 and 2012, I'll investigate.

@bzoz bzoz added the wip Issues and PRs that are still a work in progress. label Mar 15, 2017
@jasnell
Copy link
Member

jasnell commented Mar 15, 2017

ping @bnoordhuis

Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

Looks good from a distance ;) It’s been too long since I worked with Windows APIs to give this a proper LGTM, even if it’s only a few lines of code.

os.close(0)
os.close(1)
os.close(2)
cmd = sys.argv[1] + ' -e "process.stdin; process.stdout; process.stderr;"'
Copy link
Member

Choose a reason for hiding this comment

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

I think I would prefer it if the arguments came from the test file, i.e. you just spawn sys.argv[1] here and move the -e … bits to test-stdio-closed.js

Copy link
Member

Choose a reason for hiding this comment

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

If you do use -e, write this as cmd = sys.argv[1:2] + ['-e', 'process.stdin; ...'] so it gets escaped properly.

src/node.cc Outdated
@@ -4213,6 +4213,16 @@ inline void PlatformInit() {
} while (min + 1 < max);
}
#endif // __POSIX__
#ifdef _WIN32
for (int fd = 0; fd <= 2; ++fd) {
auto handle = (HANDLE) _get_osfhandle(fd);
Copy link
Member

Choose a reason for hiding this comment

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

Can you use the appropriate C++-style cast here, whichever one that is?

os.close(0)
os.close(1)
os.close(2)
cmd = sys.argv[1] + ' -e "process.stdin; process.stdout; process.stderr;"'
Copy link
Member

Choose a reason for hiding this comment

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

If you do use -e, write this as cmd = sys.argv[1:2] + ['-e', 'process.stdin; ...'] so it gets escaped properly.


if (common.isWindows) {
common.skip('platform not supported.');
const python = process.env.PYTHON || 'python';
const script = path.join(__dirname, '..', 'fixtures',
Copy link
Member

Choose a reason for hiding this comment

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

path.join(common.fixturesDir, 'spawn_closed_stdio.py')?

@bzoz
Copy link
Contributor Author

bzoz commented Mar 16, 2017

Updated, CI is green, PTAL

@bzoz bzoz removed the wip Issues and PRs that are still a work in progress. label Mar 16, 2017
Copy link
Member

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

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

LGTM with a question.

src/node.cc Outdated
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?

Copy link
Contributor

@Fishrock123 Fishrock123 left a comment

Choose a reason for hiding this comment

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

Uh not really sure about my review on this, CI passes and stuff I guess? ¯\_(ツ)_/¯

@addaleax addaleax added lts-watch-v4.x lib / src Issues and PRs related to general changes in the lib or src directory. labels Mar 19, 2017
@bzoz
Copy link
Contributor Author

bzoz commented Mar 20, 2017

@addaleax I've added a comment about _close

@addaleax
Copy link
Member

Landed in bd496e0

@addaleax addaleax closed this Mar 20, 2017
@italoacasas italoacasas mentioned this pull request Apr 10, 2017
2 tasks
MylesBorins pushed a commit that referenced this pull request Apr 18, 2017
Check that stdin, stdout and stderr are valid file descriptors on
Windows. If not, reopen them with 'nul' file.

Refs: #875
Fixes: #11656
PR-URL: #11863
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
MylesBorins pushed a commit that referenced this pull request Apr 18, 2017
Check that stdin, stdout and stderr are valid file descriptors on
Windows. If not, reopen them with 'nul' file.

Refs: #875
Fixes: #11656
PR-URL: #11863
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@MylesBorins
Copy link
Contributor

landed in v4.x and v6x staging
lmk if this shouldn't have landed

MylesBorins pushed a commit that referenced this pull request Apr 19, 2017
Check that stdin, stdout and stderr are valid file descriptors on
Windows. If not, reopen them with 'nul' file.

Refs: #875
Fixes: #11656
PR-URL: #11863
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
MylesBorins pushed a commit that referenced this pull request Apr 19, 2017
Check that stdin, stdout and stderr are valid file descriptors on
Windows. If not, reopen them with 'nul' file.

Refs: #875
Fixes: #11656
PR-URL: #11863
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This was referenced Apr 19, 2017
bzoz added a commit to JaneaSystems/node that referenced this pull request Apr 20, 2017
nodejs#11863 adds _O_RDWR to node.cc
which is defined in fcntl.h. This adds this include directly to
node.cc.
@bzoz bzoz mentioned this pull request Apr 20, 2017
2 tasks
MylesBorins pushed a commit that referenced this pull request Apr 24, 2017
#11863 adds _O_RDWR to node.cc
which is defined in fcntl.h. This adds this include directly to
node.cc.

PR-URL: #12540
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
MylesBorins pushed a commit that referenced this pull request Apr 24, 2017
#11863 adds _O_RDWR to node.cc
which is defined in fcntl.h. This adds this include directly to
node.cc.

PR-URL: #12540
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
MylesBorins pushed a commit that referenced this pull request Apr 24, 2017
#11863 adds _O_RDWR to node.cc
which is defined in fcntl.h. This adds this include directly to
node.cc.

PR-URL: #12540
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
MylesBorins pushed a commit that referenced this pull request Apr 24, 2017
#11863 adds _O_RDWR to node.cc
which is defined in fcntl.h. This adds this include directly to
node.cc.

PR-URL: #12540
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
MylesBorins pushed a commit that referenced this pull request Apr 24, 2017
#11863 adds _O_RDWR to node.cc
which is defined in fcntl.h. This adds this include directly to
node.cc.

PR-URL: #12540
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
MylesBorins pushed a commit that referenced this pull request Apr 24, 2017
#11863 adds _O_RDWR to node.cc
which is defined in fcntl.h. This adds this include directly to
node.cc.

PR-URL: #12540
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
evanlucas pushed a commit that referenced this pull request Apr 25, 2017
#11863 adds _O_RDWR to node.cc
which is defined in fcntl.h. This adds this include directly to
node.cc.

PR-URL: #12540
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
evanlucas pushed a commit that referenced this pull request May 1, 2017
#11863 adds _O_RDWR to node.cc
which is defined in fcntl.h. This adds this include directly to
node.cc.

PR-URL: #12540
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
evanlucas pushed a commit that referenced this pull request May 2, 2017
#11863 adds _O_RDWR to node.cc
which is defined in fcntl.h. This adds this include directly to
node.cc.

PR-URL: #12540
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
andrew749 pushed a commit to michielbaird/node that referenced this pull request Jul 19, 2017
Check that stdin, stdout and stderr are valid file descriptors on
Windows. If not, reopen them with 'nul' file.

Refs: nodejs/node#875
Fixes: nodejs/node#11656
PR-URL: nodejs/node#11863
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
andrew749 pushed a commit to michielbaird/node that referenced this pull request Jul 19, 2017
nodejs/node#11863 adds _O_RDWR to node.cc
which is defined in fcntl.h. This adds this include directly to
node.cc.

PR-URL: nodejs/node#12540
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. process Issues and PRs related to the process subsystem. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants