This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: multiprocessing fails to raise exception with parameters
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: multiprocessing.pool.AsyncResult.get() messes up exceptions
View: 9400
Assigned To: Nosy List: candlerb, r.david.murray, sbt
Priority: normal Keywords:

Created on 2012-11-26 14:50 by candlerb, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg176416 - (view) Author: Brian Candler (candlerb) Date: 2012-11-26 14:50
Probably best demonstrated by example.

~~~~~~~~~~~~~~~~
import multiprocessing

class Myerror(ValueError):
    def __init__(self,a):
        self.a = a
    def __str__(self):
        return repr(self.a)
        
def foo(arg):
    raise Myerror(arg)
    
#foo("1")   #<= this works fine, raises exception as expected

#But this breaks:
pool = multiprocessing.Pool(2)
pool.map(foo, ["1","2","3"])
~~~~~~~~~~~~~~~~

The result seen:

~~~~~~~~~~~~~~~~
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 353, in _handle_results
    task = get()
TypeError: ('__init__() takes exactly 2 arguments (1 given)', <class '__main__.Myerror'>, ())
~~~~~~~~~~~~~~~~

At this point the application hangs. Worse: pressing ctrl-C shows a traceback and KeyboardInterrupt, but the worker keeps getting restarted, so it's impossible to stop. You have to go to another shell and do somthing like

    killall python

to terminate the program.

A real-world example (which caused me to track this down) is a CalledProcessError raised by subprocess.check_call

~~~~~~~~~~~~~~~~
import multiprocessing
import subprocess

def foo(arg):
    subprocess.check_call("nonexistent", shell=True)
    #raise subprocess.CalledProcessError(127, "nonexistent")

pool = multiprocessing.Pool(2)
pool.map(foo, ["1","2","3"])
~~~~~~~~~~~~~~~~

which fails in the same way:

~~~~~~~~~~~~~~~~
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 353, in _handle_results
    task = get()
TypeError: ('__init__() takes at least 3 arguments (1 given)', <class 'subprocess.CalledProcessError'>, ())
~~~~~~~~~~~~~~~~

Behaviour tested on:
python 2.7.3 on Ubuntu 12.04
python 2.7.1 on OSX 10.7.5

Workaround: re-raise a parameter-less exception instead.

    try:
        ...
    except Exception as e:
        raise RuntimeError
msg176422 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-11-26 15:47
This is probably related to #1692335.  It looks like that fix was not backported.  Can you test if your example works now in 3.3?
msg176433 - (view) Author: Richard Oudkerk (sbt) * (Python committer) Date: 2012-11-26 17:37
The example works correctly on 3.3 because of #1692335.  I am not sure if it is appropriate to backport it though.

This is a duplicate of #9400 which I have assigned to myself.  (I had thought it was already fixed.)
History
Date User Action Args
2022-04-11 14:57:38adminsetgithub: 60762
2012-11-26 17:37:49sbtsetstatus: open -> closed
superseder: multiprocessing.pool.AsyncResult.get() messes up exceptions
messages: + msg176433

resolution: duplicate
stage: resolved
2012-11-26 15:47:47r.david.murraysetnosy: + r.david.murray, sbt
messages: + msg176422
2012-11-26 14:50:16candlerbcreate