Skip to content

Multiprocessing usage results in ZMQError: Address already in use #6109

Closed

Description

@khadiwala

Some custom python code that uses multiprocessing was occasionally throwing ZMQErrors for me when used in a notebook.

Here's a simple test case I made that always reproduces the issue for me. It creates a process that creates a process.

from multiprocessing import Process
def worker_worker():
    d = Process(target=worker)
    d.start()
    d.join()    

def worker():
    pass

p = Process(target=worker_worker)
p.start()
p.join()

Running this as a script with python, it finishes correctly. Running it in an ipython notebook causes a hang. When interrupted the exception shown is:

Exception in thread Thread-4:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.7_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/Users/rkhadiwala/Library/Python/2.7/lib/python/site-packages/zmq/utils/garbage.py", line 34, in run
    s.bind(self.gc.url)
  File "socket.pyx", line 444, in zmq.backend.cython.socket.Socket.bind (zmq/backend/cython/socket.c:4076)
  File "checkrc.pxd", line 21, in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:6032)
ZMQError: Address already in use

system information (I've also tried this on another box (linux) and saw the same issue):

python -c "import IPython; print(IPython.sys_info())"
{'commit_hash': '681fd77',
 'commit_source': 'installation',
 'default_encoding': 'UTF-8',
 'ipython_path': '/Users/rkhadiwala/Library/Python/2.7/lib/python/site-packages/IPython',
 'ipython_version': '2.1.0',
 'os_name': 'posix',
 'platform': 'Darwin-13.3.0-x86_64-i386-64bit',
 'sys_executable': '/usr/bin/python',
 'sys_platform': 'darwin',
 'sys_version': '2.7.5 (default, Mar  9 2014, 22:15:05) \n[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]'}

Activity

  1. ahaefner commented on Jul 18, 2014

    @ahaefner

    I'm seeing the same issue when running multiprocessing code from the ipython-qtconsole. I have a similar system to you, but I'm running Anaconda with python 2.7.8

  2. erccarls commented on Jul 28, 2014

    @erccarls

    Having the same problem on both EPD and Anaconda w/ python 2.7

  3. andaag commented on Jul 29, 2014

    @andaag

    Check that you're running the same version of the zmq library and the zmq python lib.

  4. ricklupton commented on Jul 29, 2014

    @ricklupton
    Contributor

    I think the problem here is that multiprocessing on Unix uses fork() by default to start the worker processes. Mixing forking (used by multiprocessing) and threading (used by IPython/ZMQ) is not necessarily supposed to work in the first place (see e.g. this bug). It's possible that the ZMQError is due to there being two copies of ZMQ trying to connect to the same port after the fork.

    If you're using Python 3.4, multiprocessing can now avoid forking by using the "forkserver" or "spawn" start methods. I've checked that your example fails for me (IPython master, Python 3.4, Ubuntu 14.04), but adding either

    multiprocessing.set_start_method('spawn')

    or

    multiprocessing.set_start_method('forkserver')

    before importing the example in the IPython notebook makes it work fine.

    It may be possible to make IPython "fork-safe" -- for example, the original discussion in this bug was about making the logging module work with fork -- but I don't know what this would involve.

  5. added this to the 3.0 milestone on Nov 10, 2014
  6. self-assigned this
    on Nov 10, 2014
  7. jamescasbon commented on Nov 12, 2014

    @jamescasbon

    It looks like billiard, which is a drop in replacement for multiprocessing with some of the patches from bugs.python.org/issue8713 applied, fixes the example above if you use billiard.forking_enable(False). I hope to do a more realistic test for my use case later, but this might be a good workaround for pythons < 3.4 without this feature.

  8. jamescasbon commented on Nov 12, 2014

    @jamescasbon

    Trying out billiard with billiard.Pool.map crashed the kernel :(

  9. modified the milestones: 4.0, 3.0 on Jan 11, 2015
  10. ellisonbg commented on Jan 11, 2015

    @ellisonbg
    Member

    Targeting this for 4.0 unless someone wants to tackle it soon.

  11. Shotgunosine commented on Feb 17, 2015

    @Shotgunosine

    I'm using joblib's Parallel and delayed, and they have been giving me this error intermittently.

    It seems like I don't get this error if I put the offending code inside a function and then call that function from a second cell. I hope this helps.

  12. khadiwala commented on Mar 2, 2015

    @khadiwala
    Author

    Does the resolution of this issue: zeromq/pyzmq#613 in pyzmq sufficiently solve the issues others have described here? If so, I think this can be closed - the test case in my original comment no longer hangs since upgrading to pyzmq==14.5.0.

  13. jamescasbon commented on Mar 2, 2015

    @jamescasbon

    Agreed, new pyzmq seems to fix this.

  14. modified the milestones: no action, 4.0 on Mar 2, 2015
  15. minrk commented on Mar 2, 2015

    @minrk
    Member

    Thanks for confirming this is fixed upstream.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions