Skip to content

fix: ensure that file descriptors 0/1/2 are opened at startup - #15555

Merged
jkleinsc merged 2 commits into
electron:masterfrom
anszom:stdio-fix
Nov 19, 2018
Merged

jkleinsc merged 2 commits into
electron:masterfrom
anszom:stdio-fix

Conversation

@anszom

@anszom anszom commented Nov 2, 2018 •

Copy link
Copy Markdown
Contributor

Description of Change

Libuv has a nasty mis-feature (libuv/libuv#2062), where it marks the standard input/output/error tty streams with the close-on-exec flag. In effect, when the chromium gpu process is spawned, it starts with the fd's 0,1,2 closed. These are reused for other purposes, and if any message is output on stdout/stderr, it ends up in an unrelated socket, pipe or file which generally messes things up. This is the root cause of at least two issues: #14246 and #12937, and possibly these (duplicates) as well: #13415, #14954, #14697

The proper place to fix this would be in libuv, but the upstream developers insist that the current behaviour is right. My change introduces a workaround which checks the standard fds at startup, and reopens them as /dev/null if missing. Feel free to point me to a better solution.

Checklist

  • PR description included and stakeholders cc'd
  • npm test passes
  • PR title follows semantic commit guidelines

In my environment, npm test on the 3-0-x branch fails on 5 tests.
My commit doesn't introduce any more failures.

Release Notes

Notes: Fixed a freeze when using the GPU in some configurations.

@anszom
anszom requested a review from a team November 2, 2018 19:20
@welcome

welcome Bot commented Nov 2, 2018

Copy link
Copy Markdown

💖 Thanks for opening this pull request! 💖

We use semantic commit messages to streamline the release process. Before your pull request can be merged, you should update your pull request title to start with a semantic prefix.

Examples of commit messages with semantic prefixes:

  • fix: don't overwrite prevent_default if default wasn't prevented
  • feat: add app.isPackaged() method
  • docs: app.isDefaultProtocolClient is now available on Linux

Things that will help get your PR across the finish line:

  • Follow the JavaScript, C++, and Python coding style.
  • Run npm run lint locally to catch formatting errors earlier.
  • Document any user-facing changes you've made following the documentation styleguide.
  • Include tests when adding/changing behavior.
  • Include screenshots and animated GIFs whenever possible.

We get a lot of pull requests on this repo, so please be patient and we will get back to you as soon as we can.

@Kagami

Kagami commented Nov 2, 2018

Copy link
Copy Markdown

Thank you for finding the root cause of that bug!
There are lot of duplicate bugs created, e.g. #12380 #12850 #13624

@anszom

anszom commented Nov 9, 2018

Copy link
Copy Markdown
Contributor Author

Is there anything more that I should do to proceed? Github says that some automatic checks have failed, but I'm not sure what I can do about this. Or should I just patiently wait for a review?

@nornagon

nornagon commented Nov 9, 2018

Copy link
Copy Markdown
Contributor

Maybe take a look at node's PlatformInit(), which does a similar thing but slightly more rigorous:

https://github.com/nodejs/node/blob/2ea70eae732682851ae6f9403daad7a966c11f5f/src/node.cc#L2366-L2377 (posix)

https://github.com/nodejs/node/blob/2ea70eae732682851ae6f9403daad7a966c11f5f/src/node.cc#L2423-L2435 (win32)

It looks like node ABORT()s in this case, whereas this patch would freopen(). @bnoordhuis can you shed some light on why node chooses to abort the process in this case? (& would that be appropriate for electron here?)

@anszom

anszom commented Nov 9, 2018

Copy link
Copy Markdown
Contributor Author

It looks like node ABORT()s in this case, whereas this patch would freopen(). @bnoordhuis can you shed some light on why node chooses to abort the process in this case? (& would that be appropriate for electron here?)

Not quite. In the quoted code ABORT is called only if the error is not EBADF, or reopening with /dev/null fails. Both cases represent a "pathological" scenario where proceeding is not a good idea.

@nornagon

nornagon commented Nov 9, 2018

Copy link
Copy Markdown
Contributor

Ohhhh, I see, it's just "cleverly" written code that calls open three times and checks that it returns fds 0, 1, 2 in order.

@nornagon nornagon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we also need to do this on windows, as node does?

Lastly, I'm not sure if you can re-target a PR, but generally we base changes on master and then backport them to release branches after they land there. It'd be great to get a version of this that targets master.

Comment thread atom/app/atom_main.cc Resolved
Comment thread atom/app/atom_main.cc Resolved
@bnoordhuis

Copy link
Copy Markdown

Ohhhh, I see, it's just "cleverly" written code that calls open three times and checks that it returns fds 0, 1, 2 in order.

POSIX guarantees that open() always returns the lowest available file descriptor. If fstat(0) fails with EBADF, you know that the next open() call is going to return 0.*

* Assuming no signal handlers or threads create file descriptors in the time window between the fstat() and the open() calls.

This code isn't susceptible to that. It runs at very early startup, when there are no threads or signal handlers yet.

@anszom
anszom requested review from a team November 11, 2018 22:16
@anszom
anszom changed the base branch from 3-0-x to master November 11, 2018 22:17
@anszom

anszom commented Nov 11, 2018

Copy link
Copy Markdown
Contributor Author

Do we also need to do this on windows, as node does?

I don't know much about windows, but it seems that libuv on windows doesn't have the same problem (example: https://github.com/libuv/libuv/blob/4c2dcca27b80945d6b7063f0ea031b8a75a46a52/src/win/tty.c#L187-L203).

@nornagon

Copy link
Copy Markdown
Contributor

Please rebase onto master so that the appveyor tests will run! 🙇

@anszom

anszom commented Nov 14, 2018 •

Copy link
Copy Markdown
Contributor Author

Please rebase onto master so that the appveyor tests will run! bowing_man

Ah, so that's what the errors were about.

Now lint is complaining about an #elif line that I didn't touch in my commit. Should I fix this as well? I assumed it's a bad idea to add unrelated whitespace-only changes to a commit.

@nornagon

Copy link
Copy Markdown
Contributor

Yes, please fix the lint errors. I'm not totally sure why that line isn't causing failures in master tbh :)

Looks like there are some legitimate failures on Windows, too, which I think you can fix by just hiding this code behind #if defined(OS_POSIX).

This fixes an issue where the gpu subprocess was writing messages to a
random pipe or socket. The standard file desciptors are closed in
chromium's subprocesses because of an unfortunate interaction with
libuv's tty handling code leaving them with the FD_CLOEXEC flag.
@anszom

anszom commented Nov 16, 2018

Copy link
Copy Markdown
Contributor Author

Is there anything I can do about the remaining test failures? These don't seem related to my changesin any way...

@nornagon

Copy link
Copy Markdown
Contributor

I agree that those failures seem unrelated. I'll rerun the CI jobs to see if they finish cleanly, if not then I think we're probably OK to merge.

@anszom

anszom commented Nov 17, 2018

Copy link
Copy Markdown
Contributor Author

It seems that there's another unrelated test failure. Do other PR's pass the test suite?

@nornagon

Copy link
Copy Markdown
Contributor

/trop run backport

@trop

trop Bot commented Nov 19, 2018

Copy link
Copy Markdown
Contributor

The backport process for this PR has been manually initiated, here we go! :D

@trop

trop Bot commented Nov 19, 2018

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "3-0-x" cleanly;
you will need to perform this backport manually.

@trop

trop Bot commented Nov 19, 2018

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "4-0-x", please check out #15763

@nornagon

Copy link
Copy Markdown
Contributor

/trop run backport

@trop

trop Bot commented Dec 20, 2018

Copy link
Copy Markdown
Contributor

The backport process for this PR has been manually initiated, here we go! :D

@trop

trop Bot commented Dec 20, 2018

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "3-0-x" cleanly;
you will need to perform this backport manually.

@trop

trop Bot commented Dec 20, 2018

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "3-1-x" cleanly;
you will need to perform this backport manually.

nornagon added a commit that referenced this pull request Jan 4, 2019
This reverts commit a5c3091.

Hoping that the underlying issue was fixed by #15555
nornagon added a commit that referenced this pull request Jan 15, 2019
This reverts commit a5c3091.

Hoping that the underlying issue was fixed by #15555
nornagon added a commit that referenced this pull request Jan 23, 2019
This reverts commit a5c3091.

Hoping that the underlying issue was fixed by #15555
@angvp

angvp commented Mar 12, 2019

Copy link
Copy Markdown

Which versions of electron (specifically 3.0.X) contains this pr?

bcpete pushed a commit to bcpete/electron that referenced this pull request Apr 18, 2019
…on#15555)

* fix: ensure that file descriptors 0/1/2 are opened at startup

This fixes an issue where the gpu subprocess was writing messages to a
random pipe or socket. The standard file desciptors are closed in
chromium's subprocesses because of an unfortunate interaction with
libuv's tty handling code leaving them with the FD_CLOEXEC flag.

* style: fix lint warnings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants