Skip to content

Celery crashes with segfault on sqlite database #869

Closed

Description

@mitar

With processes pool implementation celery crashes with segfault on sqlite database when task access SQLite database. With for example solo pool it works OK.

Python 2.7.1 (r271:86832, Aug 5 2011, 03:30:24)

Django==1.4
PyYAML==3.10
South==0.7.5
amqplib==1.0.2
anyjson==0.3.3
billiard==2.7.3.10
brukva==0.0.1
celery==3.0.1
distribute==0.6.27
dj-database-url==0.2.1
django-celery==3.0.1
django-pushserver==0.1.9
kombu==2.2.6
psycopg2==2.4.5
py-hbpush==0.1.3
python-dateutil==1.5
tornado==2.3
wsgiref==0.1.2
OS Version:      Mac OS X 10.7.4 (11E2705)
Report Version:  9

Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000108

VM Regions Near 0x108:
--> 
    __TEXT                 0000000101795000-0000000101796000 [    4K] r-x/rwx SM=COW  /Users/USER/*

Application Specific Information:
*** multi-threaded process forked ***
objc[2862]: garbage collection is OFF

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libdispatch.dylib               0x00007fff8f084a91 _dispatch_barrier_async_f_slow + 62
1   libsqlite3.dylib                0x00007fff8de1df52 sqlite3_initialize + 2018
2   libsqlite3.dylib                0x00007fff8de1ca77 openDatabase + 71
3   _sqlite3.so                     0x00000001024a15f7 pysqlite_connection_init + 323
4   org.python.python               0x00000001017e6b7a 0x10179a000 + 314234
5   org.python.python               0x00000001017a1d32 PyObject_Call + 97
6   _sqlite3.so                     0x00000001024a5c63 0x1024a0000 + 23651
7   org.python.python               0x00000001018225d8 PyEval_EvalFrameEx + 16006
...

Activity

  1. ask commented on Jul 20, 2012

    @ask
    Contributor

    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.

  2. mitar commented on Jul 21, 2012

    @mitar
    ContributorAuthor

    Does not really work. I tried:

    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?

  3. ask commented on Jul 23, 2012

    @ask
    Contributor

    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.

  4. ask commented on Aug 31, 2012

    @ask
    Contributor

    Please try upgrading the latest version and if still doesn't work create a minimal project that is able to reproduce the issue

  5. mitar commented on Sep 1, 2012

    @mitar
    ContributorAuthor

    Sadly, with 3.0.9 it still does not work. Will try to prepare the project.

  6. albertz commented on Feb 6, 2013

    @albertz

    I'm curious, I have the same crash in an own project (albertz/music-player#22), was this resolved?

  7. ask commented on Mar 5, 2013

    @ask
    Contributor

    Are you using sqlite as a broker or as a result store?

  8. ask commented on Mar 25, 2013

    @ask
    Contributor

    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 -ldispatch support?

  9. albertz commented on Mar 26, 2013

    @albertz

    In MusicPlayer, we fixed that. See this comment.

    SQLite does not allow the use of database connections across forked processes.

    The backtrace of your crash looks exactly the same as our crash, so it seems likely that it is the same issue.

  10. ask commented on Mar 26, 2013

    @ask
    Contributor

    @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.

  11. ask commented on Mar 26, 2013

    @ask
    Contributor

    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.

  12. ask commented on Mar 26, 2013

    @ask
    Contributor

    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...

  13. ask commented on Mar 27, 2013

    @ask
    Contributor

    @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

  14. ask commented on Mar 28, 2013

    @ask
    Contributor

    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 :(

  15. ask commented on Mar 28, 2013

    @ask
    Contributor

    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>)

  16. ask commented on Mar 28, 2013

    @ask
    Contributor

    where <object file> is any Mach-O binary or shared library.

  17. ask commented on Apr 11, 2013

    @ask
    Contributor

    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

  18. Heroico commented on Oct 19, 2015

    @Heroico

    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.10
  19. evanj commented on May 25, 2016

    @evanj

    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()

  20. alexahn commented on Jun 11, 2016

    @alexahn

    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:

    celery --app=myproj.myproj.celery --pool=solo worker --loglevel=INFO
    
  21. noomz commented on Jun 22, 2016

    @noomz

    Thank you @alexahn. Your solution work!

  22. devos50 commented on Nov 5, 2016

    @devos50

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions