You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Celery crashes with segfault on sqlite database #869
With processes pool implementation celery crashes with segfault on sqlite database when task access SQLite database. With for example solo pool it works OK.
When celery uses the multiprocessing pool the following happens:
celery worker starts
imports a bunch of modules: both celery core and user provided
spawns child worker processes for the multiprocessing.Pool using fork()
(followed by an execv() if CELERYD_FORCE_EXECV is enabled.
When a fork() happens without a following execv() the new child processes
share the same file descriptors (which also include sockets) as the parent process
So if an SQLIte database is opened before the fork occurs
the parent and all the child processes will share the same file descriptor.
This could very likely lead to crash, so probably you should make
sure that the database is closed when the child processes starts.
You can accomplish that by connection to the worker_process_init signal,
which is only sent by each child process:
from celery.signals import worker_process_init
@worker_process_init.connect
def close_databases(**kwargs):
some_closeable_object.close()
This can be added to your app module, or a config module if you have one.
from django import db
from celery import signals
@signals.worker_process_init.connect
def close_database(**kwargs):
print "closing"
db.connection.close()
and although signal handler is called, it still crashes.
Anyway, I believe this should be handled by Celery? Is it possible that the problem is the conflict between Django database access and Celery alchemy?
This could very well be a bug in Apple's dispatch library, and that it does not play well with fork(). Maybe it's possible to build sqlite without -ldispatch support?
Yeah, I saw the MusicPlayer issue while searching google.
But connections across processes doesn't explain everything, for one django-celery (used by reporters here) will automatically close the connection object after fork, and this leads to no problems on platforms other than OS X.
It could be that celery is not closing/cleaning up all inherited sqlite-related resources, but then that doesn't explain
why it doesn't crash on other platforms, also I have seen problems with inherited connections before and usually
they take some time to manifest (often it's only a real problem if the connection is attempted used simultaneously by both parent and child)
I'm inclined to think this is more about some other state in libdispatch that is not being reset after fork,
libdispatch is relatively new too, and I don't know if it promises fork-safety.
Btw, note that Apple ships with a modified version of sqlite, otool -L shows that this is linked against libdispatch (though via libSystem).
It may be interesting to try if brew install sqlite also uses libdispatch, and if not whether the bug is present there.
That would be different from configure script to configure script I guess. It could be something like --with-sqlite or something else, probably it's eventually using $LDFLAGS or $CFLAGS. You can read the source of pysqlite's setup.py maybe, but sqlite3 is now part of Python stdlib so you may be looking at recompiling Python :(
Btw, technically it's possible to change the path of the dynamic loader image without recompilation, but this won't work if the API's are not compatible. The $DYLD_LIBRARY_PATH environment variable lets you specify the path for dynamically linked libraries, but in this case the library may be specified using an absolute path (you can see the list of dependencies using otool -L <object file>)
Btw, I'm unable to reproduce this here using sqlite with django-celery. So probably this is some concurrency thing, you really shouldn't be using sqlite with multiple processes/threads
I know this has been closed a long deal ago, but I ran into a similar issue in my project, and this discussion helped me solve it. I got a similar scenario and pretty much the same crash stack trace. So, I would like to add a little noise in case someone experiences the same thing.
In my case, it was Apple's sqlite3 indeed. I used a custom python (via pyenv) linked against homebrew's sqlite, as suggested here, and the problem went away. Thanks @ask and @albertz.
PS:
In my project, I had a desktop GUI application that called long calculation process in the background. This background process crashed when accessing sqlite3... And it was only one process accessing the database. No actual concurrency.
I debugged a very similar problem today. My scenario was closer to @Heroico's, in that I was not experiencing a concurrency issue and the task was failing even when celery was set to start with a concurrency of one.
OS Version: Mac OS X 10.11.4 (15E65)
Report Version: 11
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000110
VM Regions Near 0x110:
-->
__TEXT 000000010f949000-000000010fa77000 [ 1208K] r-x/rwx SM=COW /Users/USER/*
Application Specific Information:
crashed on child side of fork pre-exec
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libdispatch.dylib 0x00007fff8b371162 _dispatch_barrier_async_f_slow + 356
1 libsqlite3.dylib 0x00007fff82b65ff4 sqlite3_initialize + 3060
2 libsqlite3.dylib 0x00007fff82b64381 openDatabase + 65
3 _sqlite3.so 0x0000000110be701d pysqlite_connection_init + 509
4 python 0x000000010f9ae81b type_call + 347
5 python 0x000000010f9556a3 PyObject_Call + 99
6 _sqlite3.so 0x0000000110bebbab module_connect + 171
7 python 0x000000010f9fb029 PyEval_EvalFrameEx + 33257
8 python 0x000000010f9f2ab4 PyEval_EvalCodeEx + 2100
9 python 0x000000010f9ff0c6 fast_function + 118
10 python 0x000000010f9fa05c PyEval_EvalFrameEx + 29212
In my case, I'm only using sqlite for dev reasons, so a quick solution was to start the celery worker(s) with the pool implementation set to solo. Here is an example worker command:
I experienced the same issue in another application and this discussion helped me to better understand what's going on. The workaround by @evanj solved the problem for me.
Activity
ask commentedon Jul 20, 2012
When celery uses the multiprocessing pool the following happens:
celery workerstartsfork()(followed by an
execv()ifCELERYD_FORCE_EXECVis enabled.When a
fork()happens without a followingexecv()the new child processesshare the same file descriptors (which also include sockets) as the parent process
So if an SQLIte database is opened before the fork occurs
the parent and all the child processes will share the same file descriptor.
This could very likely lead to crash, so probably you should make
sure that the database is closed when the child processes starts.
You can accomplish that by connection to the
worker_process_initsignal,which is only sent by each child process:
This can be added to your app module, or a config module if you have one.
mitar commentedon Jul 21, 2012
Does not really work. I tried:
and although signal handler is called, it still crashes.
Anyway, I believe this should be handled by Celery? Is it possible that the problem is the conflict between Django database access and Celery alchemy?
ask commentedon Jul 23, 2012
django-celery already closes the django database connection at worker process init, yes.
I guess then you should try to search for the error and see if someone has had a similar problem,
or ask the python sqlite driver maintainers.
ask commentedon Aug 31, 2012
Please try upgrading the latest version and if still doesn't work create a minimal project that is able to reproduce the issue
mitar commentedon Sep 1, 2012
Sadly, with 3.0.9 it still does not work. Will try to prepare the project.
albertz commentedon Feb 6, 2013
I'm curious, I have the same crash in an own project (albertz/music-player#22), was this resolved?
ask commentedon Mar 5, 2013
Are you using sqlite as a broker or as a result store?
ask commentedon Mar 25, 2013
The crash happens in a libdispatch function (_dispatch_barrier_async_f_slow):
http://www.opensource.apple.com/source/libdispatch/libdispatch-84.5/src/queue.c
This could very well be a bug in Apple's dispatch library, and that it does not play well with
fork(). Maybe it's possible to build sqlite without-ldispatchsupport?albertz commentedon Mar 26, 2013
In MusicPlayer, we fixed that. See this comment.
The backtrace of your crash looks exactly the same as our crash, so it seems likely that it is the same issue.
ask commentedon Mar 26, 2013
@albertz,
Yeah, I saw the MusicPlayer issue while searching google.
But connections across processes doesn't explain everything, for one django-celery (used by reporters here) will automatically close the connection object after fork, and this leads to no problems on platforms other than OS X.
It could be that celery is not closing/cleaning up all inherited sqlite-related resources, but then that doesn't explain
why it doesn't crash on other platforms, also I have seen problems with inherited connections before and usually
they take some time to manifest (often it's only a real problem if the connection is attempted used simultaneously by both parent and child)
I'm inclined to think this is more about some other state in libdispatch that is not being reset after fork,
libdispatch is relatively new too, and I don't know if it promises fork-safety.
ask commentedon Mar 26, 2013
Btw, note that Apple ships with a modified version of sqlite, otool -L shows that this is linked against libdispatch (though via libSystem).
It may be interesting to try if
brew install sqlitealso uses libdispatch, and if not whether the bug is present there.ask commentedon Mar 26, 2013
In fact, grepping Apple's sqlite source code (http://www.opensource.apple.com/release/mac-os-x-1082/) gives many references to
<dispatch/dispatch.h>which are not present in the canonical sqlite sources. Pretty sure Apple is the culprit here...ask commentedon Mar 27, 2013
@dangerfarms and you are sure that Python is loading the new sqlite lib? Doesn't really make sense, unless brew also takes in the Apple patches
ask commentedon Mar 28, 2013
That would be different from configure script to configure script I guess. It could be something like --with-sqlite or something else, probably it's eventually using
$LDFLAGSor$CFLAGS. You can read the source of pysqlite's setup.py maybe, but sqlite3 is now part of Python stdlib so you may be looking at recompiling Python :(ask commentedon Mar 28, 2013
Btw, technically it's possible to change the path of the dynamic loader image without recompilation, but this won't work if the API's are not compatible. The
$DYLD_LIBRARY_PATHenvironment variable lets you specify the path for dynamically linked libraries, but in this case the library may be specified using an absolute path (you can see the list of dependencies usingotool -L <object file>)ask commentedon Mar 28, 2013
where
<object file>is any Mach-O binary or shared library.ask commentedon Apr 11, 2013
Btw, I'm unable to reproduce this here using sqlite with django-celery. So probably this is some concurrency thing, you really shouldn't be using sqlite with multiple processes/threads
Heroico commentedon Oct 19, 2015
I know this has been closed a long deal ago, but I ran into a similar issue in my project, and this discussion helped me solve it. I got a similar scenario and pretty much the same crash stack trace. So, I would like to add a little noise in case someone experiences the same thing.
In my case, it was Apple's sqlite3 indeed. I used a custom python (via pyenv) linked against homebrew's sqlite, as suggested here, and the problem went away. Thanks @ask and @albertz.
PS:
In my project, I had a desktop GUI application that called long calculation process in the background. This background process crashed when accessing sqlite3... And it was only one process accessing the database. No actual concurrency.
Since I used pyenv, this was my build command:
$ PYTHON_CONFIGURE_OPTS="LD_RUN_PATH=/usr/local/opt/sqlite/lib LDFLAGS=-L/usr/local/opt/sqlite/lib CPPFLAGS=-I/usr/local/opt/sqlite/include" pyenv install 2.7.10evanj commentedon May 25, 2016
I think the root cause is this Python/Mac OS X bug: http://bugs.python.org/issue27126
A workaround: Before you start forking any subprocesses, you can run
sqlite3.connect(':memory:').close()alexahn commentedon Jun 11, 2016
I debugged a very similar problem today. My scenario was closer to @Heroico's, in that I was not experiencing a concurrency issue and the task was failing even when celery was set to start with a concurrency of one.
In my case, I'm only using sqlite for dev reasons, so a quick solution was to start the celery worker(s) with the pool implementation set to solo. Here is an example worker command:
noomz commentedon Jun 22, 2016
Thank you @alexahn. Your solution work!
devos50 commentedon Nov 5, 2016
I experienced the same issue in another application and this discussion helped me to better understand what's going on. The workaround by @evanj solved the problem for me.