Repository navigation
Description
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.
- 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.
- 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.
- 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
jakirkham commentedon Jul 16, 2018
Yeah bare forks are a nightmare. Improvements in this area are generally welcome.
spawnworks quite nicely. So it is reasonable to prefer that.Another consideration would be to have proper fork+exec. IIUC
forkserverdoes this (our current default). Also Loky does this. Joblib version0.12uses 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.
itamarst commentedon Jul 17, 2018
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.
jakirkham commentedon Jul 17, 2018
How do you mean? Fork+exec overlays the existing process. There is nothing that sticks around.
itamarst commentedon Jul 17, 2018
Sorry, wasn't clear, I meant "the CPython issues/PRs specifically don't solve the underlying problem". fork+exec is the correct answer.
jakirkham commentedon Jul 17, 2018
Ah ok. Yep, agreed.
jakirkham commentedon Jul 17, 2018
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
forkseem to show up in C code on Linux and these issues are discussed generally.mrocklin commentedon Oct 22, 2018
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
ihnorton commentedon Mar 26, 2019
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.
itamarst commentedon Apr 24, 2020
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.
jakirkham commentedon Apr 24, 2020
I'm in favor of changing the default to
spawnin 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?itamarst commentedon Apr 24, 2020
I'll add it to my todo list. Can you reopen the issue and assign it to me?
itamarst commentedon Apr 24, 2020
Actually, there's #4003