-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
asyncio segfault when using threadpool and "_asyncio" native module #75244
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
Comments
I have a project in a prod environment which heavily uses asyncio and a threadpool. It uses the threadpool to run CPU heavy tasks (in this case populating a defaultdict) to avoid blocking the main thread (no async code in thread). For some time now my service has been randomly crashing at the same place in the thread which does the dict updating. I've finally got both the python and native stack traces, and based on the information presented it looked very similar to the issue found by the devs at home-assistant (home-assistant/core#7752 (comment), which points to home-assistant/core#7848). So I tried their fix of disabling the "_asyncio" module, and lo and behold python no longer segfaults. Per the stacktrace it's crashing in PyObject_GC_Del, and the only place this is used in the asyncio module seems to be here: https://github.com/python/cpython/blob/master/Modules/_asynciomodule.c#L996 does anyone have any idea why it's crashing on this line? Are there thread protections missing in this file? I'm trying to reproduce this in a testcase but it's proving very difficult as I'm guessing it's timing related. |
btw I've seen this issue in 3.5.2 + 3.6.2 on debian jessie + stretch |
Interesting. A code to reproduce this crash would help tremendously. So far this is the first time I hear about this crash. |
so looks like disabling the _asyncio module just caused the crash to happen less often, closing and will continue investigating after a get a core file |
Interesting. So it crashes even with pure Python Future/Task implementations? Another idea to debug this is to try to see what happens when you run your code with CFuture and PyTask, and with PyFuture/CTask. |
the problem with this crash is that it only happens periodically in our prod environment :( If I try running the exact same docker container with the same inputs locally it doesn't reproduce, so frustrating. I've created a whole workflow now for deploying with a debug python to get a core file with symbols. Hopefully have some more info w/in a day. Thanks for the tips! |
Thank you for trying to get to the bottom of this! |
ok got a full debug core file, let me know what other information I can provide. |
Thank you. Does the crash happen with python versions of Tasks and Future? |
this is the comment on the assert: /* Python's cyclic gc should never see an incoming refcount
I've also attached a file that's similar to the code we run in production, however couldn't get it to reproduce the crash. In the datafile it uses it has some tuples like the following: |
hmm, how would I do that? btw I'm not 100% sure this is due to asyncio. |
btw got slightly difference stacktrace on second core file |
oh, so this is looking like an asyncio issue, the "gc" that is causing the crash is: (gdb) print *FROM_GC(gc)->ob_type note: it's a _GatheringFuture. |
another core had a different gc object: $1 = {ob_base = {ob_base = {ob_next = 0x7f801eac3158, _ob_prev = 0x7f801eab95a0, ob_refcnt = 41, ob_type = 0x7f80238e76e0 <PyType_Type>}, ob_size = 0}, tp_name = 0x7f801e8967af "_asyncio.Task", tp_basicsize = 128, tp_itemsize = 0, |
I'm hoping this is the fix: --- Modules/_asynciomodule.c.orig 2017-07-31 12:16:16.000000000 -0700
+++ Modules/_asynciomodule.c 2017-07-31 13:08:52.000000000 -0700
@@ -953,15 +953,18 @@
FutureObj_dealloc(PyObject *self)
{
FutureObj *fut = (FutureObj *)self;
+ PyObject_GC_UnTrack(self);
if (Future_CheckExact(fut)) {
/* When fut is subclass of Future, finalizer is called from
* subtype_dealloc.
*/
+ _PyObject_GC_TRACK(self);
if (PyObject_CallFinalizerFromDealloc(self) < 0) {
// resurrected.
return;
}
+ _PyObject_GC_UNTRACK(self);
}
if (fut->fut_weakreflist != NULL) {
@@ -1828,14 +1831,18 @@
{
TaskObj *task = (TaskObj *)self;
+ PyObject_GC_UnTrack(self);
+
if (Task_CheckExact(self)) {
/* When fut is subclass of Task, finalizer is called from
* subtype_dealloc.
*/
+ _PyObject_GC_TRACK(self);
if (PyObject_CallFinalizerFromDealloc(self) < 0) {
// resurrected.
return;
}
+ _PyObject_GC_UNTRACK(self);
}
if (task->task_weakreflist != NULL) { |
Alexander, I think this is it, you found it! Can you open a PR against master with a fix? I'll review & merge it. |
ok, created: #2966 there are some other deallocs in there, mind verifying the rest? |
I've verified that this along with the changes in 31095 resolve the crashes I've been seeing in our production environment |
Thank you, Alexander. Bugs like this are notoriously hard to fix, you saved us from hours, if not days, of debugging. |
bad news, I just got a crash in the same place (updating defaultdict) after running for a week with the fixes from this and inada naoki's patches. I think the threadpool may be leaking threads too as I had > 40 threads after running for a week when I use no more than ~10. I'm going to switch to debug build and will update when I get more details. |
hmm, may be my fault due to docker image tagging issue. Will redeploy and update if the issue persists. If I don't reply again sorry for the noise. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: