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

Ensure non-blocking process wait and correctly report process exit status #3419

Merged
merged 18 commits into from
May 8, 2020

Conversation

mfelsche
Copy link
Contributor

Waiting or a process to finish was potentially blocking both on windows and posix systems. It was only ever called when the child process was killed beforehand or has closed both stdout and stderr, so the actual risk to block was low, but still. Now it calls waitpid (and the windows equivalent) with WNOHANG (on windows with timeout of 0) to ensure it does not block. If the process is still running at that point, a timer is scheduled to poll the process using the non-blocking waitpid.

During testing I discovered that processes terminated by signals were usually reported as exited with exit-code 0, which is wrong. I changed the ProcessNotify.dispose signature to receive a Signaled or Exited class containing signal number or exit code.

This works pretty well, the only case this does not catch is, when a process exits and sets an exit-code in a signal handler. ProcessNotify.dispose will be called with Signaled, not with Exited and the exit code is basically lost. But I still prefer this.

As this is a breaking change, I thought to keep it as draft at first, in order to discuss it.
I would consider the non-blocking waitpid call as internal and the improved exit status reporting as actually fixing a bug, that is why I didn't create an RFC for this.

@mfelsche
Copy link
Contributor Author

This still needs to be tested on windows, CI doesnt run the process tests on windows.

@mfelsche mfelsche force-pushed the polling-process-wait branch from 33fe254 to 9348421 Compare December 19, 2019 08:06
@mfelsche mfelsche marked this pull request as ready for review December 19, 2019 09:00
@mfelsche
Copy link
Contributor Author

Ok, here we go! Tests run fine on windows! 🎉

@SeanTAllen
Copy link
Member

@cquinn can you take a look at this?

Copy link
Contributor

@cquinn cquinn left a comment

Choose a reason for hiding this comment

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

I think this looks good.

@@ -34,13 +34,95 @@ primitive _EAGAIN
primitive _EINVAL
fun apply(): I32 => 22

primitive WNOHANG
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a Comment here to describe what WNOHANG represents. Also, would it be possible to make it _WNOHANG?

Copy link
Member

Choose a reason for hiding this comment

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

The _ is a good catch. That should be changed.

Copy link
Member

@SeanTAllen SeanTAllen left a comment

Choose a reason for hiding this comment

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

WNOHANG should be _WNOHANG as cquinn noted.

I don't think it needs a comment on what it is, as none of the other primitives do. If we want that, it should be part of a separate PR.

@mfelsche mfelsche force-pushed the polling-process-wait branch from 9348421 to 6eaf1a3 Compare January 20, 2020 19:30
@mfelsche mfelsche force-pushed the polling-process-wait branch 3 times, most recently from d88817c to 989b563 Compare May 2, 2020 19:21
@mfelsche
Copy link
Contributor Author

mfelsche commented May 2, 2020

@SeanTAllen all good to go from my side. How about you?

@SeanTAllen
Copy link
Member

@mfelsche 0.34.1 has been released. you can merge this whenever. can you add the release notes to the 0.34.2 issue?

mfelsche and others added 14 commits May 7, 2020 14:07
by calling waitpid with WNOHANG on posix and doing the equivalent on windows
and if the process didnt exit yet, start a timer and poll the process.
But only create timers etc. if the process didnt finish in the first try to save resources in the normal case.
So users of ProcessNotify are properly notified of how their process exited. If it was killed by a signal, child_exit_status
will be a Signaled instance containing the signal number that terminated the process (only on POSIX).
mfelsche added 3 commits May 7, 2020 14:07
It was depending on being executed in the directory of its source, which was not true. THe test accidentally created an empty executable version of that file, while ensuring it to be executable. This was polluting the test directory. Now everything is put inside a temp dir, which is cleaned afterwards.
@mfelsche mfelsche force-pushed the polling-process-wait branch from 02ba6a1 to 10011c0 Compare May 7, 2020 12:08
@mfelsche mfelsche force-pushed the polling-process-wait branch from 10011c0 to a6f5a36 Compare May 7, 2020 13:24
@SeanTAllen SeanTAllen added the changelog - fixed Automatically add "Fixed" CHANGELOG entry on merge label May 8, 2020
@SeanTAllen
Copy link
Member

SeanTAllen commented May 8, 2020

@mfelsche don't forget to SQUASH when you merge this! 😉

I forget sometimes, thought I would remind you just in case.

@mfelsche
Copy link
Contributor Author

mfelsche commented May 8, 2020

Thanks, much appreciated!

@mfelsche mfelsche merged commit 7514e19 into master May 8, 2020
@mfelsche mfelsche deleted the polling-process-wait branch May 8, 2020 06:49
github-actions bot pushed a commit that referenced this pull request May 8, 2020
@SeanTAllen SeanTAllen mentioned this pull request May 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog - fixed Automatically add "Fixed" CHANGELOG entry on merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants