-
Notifications
You must be signed in to change notification settings - Fork 565
Fix - freeze when reading stdout from external process #268
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import asyncio | ||
import ctypes.util | ||
import logging | ||
from concurrent.futures import ThreadPoolExecutor | ||
from threading import Thread | ||
from unittest import TestCase | ||
|
||
import uvloop | ||
|
||
|
||
class ProcessSpawningTestCollection(TestCase): | ||
|
||
def test_spawning_external_process(self): | ||
"""Test spawning external process (using `popen` system call) that | ||
cause loop freeze.""" | ||
|
||
async def run(loop): | ||
event = asyncio.Event(loop=loop) | ||
|
||
dummy_workers = [simulate_loop_activity(loop, event) | ||
for _ in range(5)] | ||
spawn_worker = spawn_external_process(loop, event) | ||
done, pending = await asyncio.wait([spawn_worker] + dummy_workers, | ||
loop=loop) | ||
exceptions = [result.exception() | ||
for result in done if result.exception()] | ||
if exceptions: | ||
raise exceptions[0] | ||
|
||
return True | ||
|
||
async def simulate_loop_activity(loop, done_event): | ||
"""Simulate loop activity by busy waiting for event.""" | ||
while True: | ||
try: | ||
await asyncio.wait_for(done_event.wait(), | ||
timeout=0.1, loop=loop) | ||
except asyncio.TimeoutError: | ||
pass | ||
|
||
if done_event.is_set(): | ||
return None | ||
|
||
async def spawn_external_process(loop, event): | ||
executor = ThreadPoolExecutor() | ||
try: | ||
call = loop.run_in_executor(executor, spawn_process) | ||
await asyncio.wait_for(call, loop=loop, timeout=3600) | ||
finally: | ||
event.set() | ||
executor.shutdown(wait=False) | ||
return True | ||
|
||
BUFFER_LENGTH = 1025 | ||
BufferType = ctypes.c_char * (BUFFER_LENGTH - 1) | ||
|
||
def run_echo(popen, fread, pclose): | ||
fd = popen('echo test'.encode('ASCII'), 'r'.encode('ASCII')) | ||
try: | ||
while True: | ||
buffer = BufferType() | ||
data = ctypes.c_void_p(ctypes.addressof(buffer)) | ||
|
||
# -> this call will freeze whole loop in case of bug | ||
read = fread(data, 1, BUFFER_LENGTH, fd) | ||
if not read: | ||
break | ||
except Exception: | ||
logging.getLogger().exception('read error') | ||
raise | ||
finally: | ||
pclose(fd) | ||
|
||
def spawn_process(): | ||
"""Spawn external process via `popen` system call.""" | ||
|
||
stdio = ctypes.CDLL(ctypes.util.find_library('c')) | ||
|
||
# popen system call | ||
popen = stdio.popen | ||
popen.argtypes = (ctypes.c_char_p, ctypes.c_char_p) | ||
popen.restype = ctypes.c_void_p | ||
|
||
# pclose system call | ||
pclose = stdio.pclose | ||
pclose.argtypes = (ctypes.c_void_p,) | ||
pclose.restype = ctypes.c_int | ||
|
||
# fread system call | ||
fread = stdio.fread | ||
fread.argtypes = (ctypes.c_void_p, ctypes.c_size_t, | ||
ctypes.c_size_t, ctypes.c_void_p) | ||
fread.restype = ctypes.c_size_t | ||
|
||
for iteration in range(1000): | ||
t = Thread(target=run_echo, | ||
args=(popen, fread, pclose), | ||
daemon=True) | ||
t.start() | ||
t.join(timeout=10.0) | ||
if t.is_alive(): | ||
raise Exception('process freeze detected at {}' | ||
.format(iteration)) | ||
|
||
return True | ||
|
||
loop = uvloop.new_event_loop() | ||
proc = loop.run_until_complete(run(loop)) | ||
self.assertTrue(proc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
typedef void (*OnForkHandler)(); | ||
|
||
OnForkHandler __forkHandler = NULL; | ||
|
||
/* Auxiliary function to call global fork handler if defined. | ||
|
||
Note: Fork handler needs to be in C (not cython) otherwise it would require | ||
GIL to be present, but some forks can exec non-python processes. | ||
*/ | ||
void handleAtFork() { | ||
if (__forkHandler != NULL) { | ||
__forkHandler(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I follow... The old handler had a
nogil
decoration, i.e.cdef void __atfork_child() nogil:
. How's this indirection makes that different?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is small but significant difference in case you compile it with cython and nogil traces enabled (-DCYTHON_TRACE=1 -DCYTHON_TRACE_NOGIL=1).
In this mode cython will insert trace calls during compilation and resulting compiled __atfork_child function will contain
__Pyx_TraceCall
. It is not a problem when fork contains python runtime, but it will freeze at this trace call in case forking child does not contains python runtime.Traces are enabled in
make debug
, so I decide to write it in pure C where no such traces are inserted by cython.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just so that I understand the scope: this PR is only needed to unbreak
make debug
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, on the contrary, this PR should fix freezing uvloop (under some conditions) and freezing occurs even when compiled with pure make (without
debug
), but for a different reason.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I'll review the PR soon.
In the meantime, could you please squash all commits into 1 and write a commit message that explains the bug/problem in a detailed way? It would help me / future maintainers a lot.
Huge props for coming up with a functional test case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I squash it. Give me a note if there is something not clear enough.