Skip to content

Default multiprocessing context is broken and should never be used #3759

Closed

Description

@itamarst

The default multiprocessing context (on POSIX systems) is fork-without-exec. As a result, all process state is mirrored in the subprocesses, with often unexpected results.

  1. Threads are not propagated. If the parent process started a thread and code relies on it working this can break the subprocess. This is probably fairly rare, though I do maintain a library where this is a problem.
  2. More seriously, module-level globals are preserved. Common module-level state includes things like logging. Subprocesses inherit the parent process' logging, often with unexpected results. I have seen child processes trying to rotate the parent's log file, for example.
  3. Even if dropping threads doesn't directly impact the subprocess, it can indirectly cause deadlocks. In particular, if it acquires a lock and a fork happens immediately after that, the lock will be acquired in the subprocess and never released.

The following example (on Python 3.6) demonstrates problems 2 and 3. Notice how merely using the logging module in a thread can break subsequent processes created with multiprocessing:

import logging
import threading
import multiprocessing
import sys
import time

logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.info("HELLO FROM PARENT")

# Emulate a thread that is holding the logging lock when fork() happens. In real
# code this would be a race condition, I'm just making it fail consistently for
# demonstration purposes.
def logging_thread():
    logging._acquireLock()
    for i in range(1, 10):
        print("THREAD COUNTDOWN TO RELEASE LOCK", i)
        time.sleep(1)
    logging._releaseLock()
    print("THREAD IS GOING AWAY")

t = threading.Thread(target=logging_thread)
t.start()


def child_process():
    logging.info("CHILD PROCESS 1")
    # Do something that acquires lock:
    logging.Handler().set_name("hello")
    logging.info("CHILD PROCESS 2")

p = multiprocessing.Process(target=child_process)
p.start()

Here is the output:

$ python3.6 mp-fork.py 
INFO:root:HELLO FROM PARENT
THREAD COUNTDOWN TO RELEASE LOCK 1
INFO:root:CHILD PROCESS 1
THREAD COUNTDOWN TO RELEASE LOCK 2
THREAD COUNTDOWN TO RELEASE LOCK 3
THREAD COUNTDOWN TO RELEASE LOCK 4
THREAD COUNTDOWN TO RELEASE LOCK 5
THREAD COUNTDOWN TO RELEASE LOCK 6
THREAD COUNTDOWN TO RELEASE LOCK 7
THREAD COUNTDOWN TO RELEASE LOCK 8
THREAD COUNTDOWN TO RELEASE LOCK 9
THREAD IS GOING AWAY

"CHILD PROCESS 2" is never printed because the child process is deadlocked, and the process never exits.


I have seen processes mysteriously deadlock with default context, and start working just fine with spawn context. The deadlocks are impossible to debug, often, because they're highly complex race conditions.

Proposed solution: change default multiprocessing context to spawn on (Python 3 POSIX), which doesn't suffer from this problem. Tests can continue to run with default context for speed, given implementation of #3758. Python 2 will continue as is since it doesn't support the notion of contexts, Python 3 on Windows only supports spawn anyway.

Activity

  1. jakirkham commented on Jul 16, 2018

    @jakirkham
    Member

    Yeah bare forks are a nightmare. Improvements in this area are generally welcome. spawn works quite nicely. So it is reasonable to prefer that.

    Another consideration would be to have proper fork+exec. IIUC forkserver does this (our current default). Also Loky does this. Joblib version 0.12 uses Loky by default. There may be other relevant implementations out there.

    Side note: Given the problems you pointed out, this issue may be interesting to you. There also appears to be an open PR ( python/cpython#4071 ) to CPython to address the logging issues in particular.

  2. itamarst commented on Jul 17, 2018

    @itamarst
    ContributorAuthor

    Thanks for the pointers. Those would both improve the situation, but in the end it'll still be broken, it wouldn't fix locks in C libraries, or other module's global state.

  3. jakirkham commented on Jul 17, 2018

    @jakirkham
    Member

    How do you mean? Fork+exec overlays the existing process. There is nothing that sticks around.

  4. itamarst commented on Jul 17, 2018

    @itamarst
    ContributorAuthor

    Sorry, wasn't clear, I meant "the CPython issues/PRs specifically don't solve the underlying problem". fork+exec is the correct answer.

  5. jakirkham commented on Jul 17, 2018

    @jakirkham
    Member

    Ah ok. Yep, agreed.

  6. jakirkham commented on Jul 17, 2018

    @jakirkham
    Member

    It's worth noting that spawn is still probably better than fork+exec. This benchmark from the Ruby community is pretty informative of the cost of fork+exec for large processes. Though it may differ for Python, would be surprised if it differed significantly. Especially given this problems with fork seem to show up in C code on Linux and these issues are discussed generally.

  7. mrocklin commented on Oct 22, 2018

    @mrocklin
    Member

    This was discussed in #4003 . It was decided to stay with the system default. We should come back to this discussion if more users present with issues here. Closing for now. Thanks @itamarst

  8. ihnorton commented on Mar 26, 2019

    @ihnorton
    Contributor

    more users

    I ended up here via TBB, so +1. Might also have been the cause of #2602. My stacktrace looked just like the one in 2602, after interrupting the kernel -- which is too generic to understand what library specifically is causing the hang (it's only obvious after attaching lldb to the child process -- it's sitting on a spinlock).

    FWIW, this probably impacts anyone using TBB (e.g. OpenCV) or NumPy+MKL or BLIS.

  9. itamarst commented on Apr 24, 2020

    @itamarst
    ContributorAuthor

    Another attempt at reopening this:

    I just talked to a scientist who spent two weeks stuck because of multiprocessing's default fork()-without-exec() setting. And to be fair, she wasn't using Dask, she was using multiprocessing directly, but this has real impact on real people; Dask should really have a non-broken default.

    Also note the issues above referencing this suggest other people have this issue too.

  10. jakirkham commented on Apr 24, 2020

    @jakirkham
    Member

    I'm in favor of changing the default to spawn in Dask. We did it in Distributed not that long ago ( dask/distributed#3461 ). Seems more consistent if we do that in Dask as well. Would you be willing to send a PR?

  11. itamarst commented on Apr 24, 2020

    @itamarst
    ContributorAuthor

    I'll add it to my todo list. Can you reopen the issue and assign it to me?

  12. itamarst commented on Apr 24, 2020

    @itamarst
    ContributorAuthor

    Actually, there's #4003

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions