This repository was archived by the owner on Nov 23, 2017. It is now read-only.
make fork + exec non blocking on unix#428
Open
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Dear all,
Following the bug #414, here is a patch which makes the fork + exec operation non blocking on Unix.
The patch is probably not ready to land, but I believe it's in pretty good shape.
This patch require a few changes in cpython (
subprocess.Popen), which are included in this patch for discussion (intmp_subprocess, as a child class ofsubprocess.Popen). Those changes have been tested with cpython unittests on my laptop. The whole patch has been tested with Python 3.5.2 on Linux, so the CI may report undetected errors for other configurations.The problem:
subprocess.Popen()will fork and synchronously read on a pipe until it is closed as a way to wait and check the result of the exec() call in the new child process. All of this is done in the constructor ofPopen.This is an issue when the
preexec_fn(user function called before exec) orexec()is slow.This PR does the following:
Popen._execute_child()method into several methods so we can isolate the blocking reads (on the fderrpipe_read) in a (temporary) child classtmp_subprocess._Popen._NonBlockingPopen, a version of_Popenwhich uses the loop and a future to signal the caller when the exec() is done (or failed). It means that an error will usually be reported by the future instead of raised when the_NonBlockingPopenobject is created. It also means that to follow the (implicit) contract ofPopen,_NonBlockingPopenis responsible of cleaning resources it opened if an error occurs before exec(). Note that_NonBlockingPopenalso uses sockets instead of pipes (following what was done in_UnixSubprocessTransport._start())._BaseSubprocessTransport._start()can be a coroutine. The implementation of the transport has been modified to support this change: the fork is now done in a task scheduled by the constructor. This doesn't change much for the caller (_make_subprocess_transport()) which awaited a future reporting errors during the transport initialization or is done once the subprocess transport is in a stable condition (_connect_pipes()finished). Special care has been taken to possible races conditions due to cancellations (usually whenloop.subprocess_exec/shell()are canceled).loop._make_subprocess_transport()doesn't change much. The biggest change is that the child_watcher is handled by the transport itself. This is required because we don't want this method to handle the process termination if something failed before the subprocess exec-ed, while now_NonBlockingPopenstill have to handle this asynchronously._NonBlockingPopenwill eventually raise an exception if something wrong happens inpreexec_fn(this was previously the responsibility ofPopen).test_proc_exited): it assumed that the transport was ready before the waiter was done. I believe this assumption is incorrect, and doesn't hold true now that the _BaseSubprocessTransport._proc attribute is set asynchronously (instead of in the constructor).This is my biggest PR for asyncio so far, and my first attempt at patching cpython, I'm eager to receive feedback and comments about the patch and about how it can successfully land in the codebase (for instance, should I get in touch with the owner of cpython's subprocess?).
Thanks for your time,
Martin