Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiprocessing.Pool hangs on certain custom Exceptions #103061

Open
djm4686 opened this issue Mar 27, 2023 · 2 comments
Open

multiprocessing.Pool hangs on certain custom Exceptions #103061

djm4686 opened this issue Mar 27, 2023 · 2 comments
Labels
topic-multiprocessing type-bug An unexpected behavior, bug, or error

Comments

@djm4686
Copy link

djm4686 commented Mar 27, 2023

Bug report

Reproducing:

import multiprocessing


class MyExc(Exception):
    def __init__(self, stuff):
        super().__init__(stuff.two)


class Stuff:
    def __init__(self):
        self.one = 1
        self.two = 2


def run(_):
    raise MyExc(Stuff())


if __name__ == "__main__":
    with multiprocessing.Pool(3) as pool:
        pool.map(run, [1])

This will cause python to hang indefinitely. The cause is the custom exception initialization.

This will throw an exception in the subprocess and will not fail out
AttributeError: 'int' object has no attribute 'two'

Your environment

CPython 3.10.8
Mac OSX Ventura 13.2

@djm4686 djm4686 added the type-bug An unexpected behavior, bug, or error label Mar 27, 2023
@gaogaotiantian
Copy link
Member

The trigger of this problem is pickle. Exception classes assume that it can restore itself when pickling with the arguments given, but that's not the case in this example. One could argue that this is not the correct way to use Exception.

The exception did not happen in the subprocess, it happened in the main process, _handle_results thread. When the thread got a pickled result from the subprocess, it thought it could restore the Exception, but it failed because the custom Exception does not follow the initialization rules.

I guess that's a question for the multiprocessing lib owner - should we monitor the potential pickling error on _handle_results and bail out the process when it happens?

@peterallenwebb
Copy link

This is a very surprising behavior and caused recent versions of dbt-core to hang on thread exceptions. Would be great to see this fixed if it is possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-multiprocessing type-bug An unexpected behavior, bug, or error
Projects
Status: No status
Development

No branches or pull requests

4 participants