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

gh-132063: ProcessPoolExecutor swallows falsy Exceptions #132129

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

YvesDup
Copy link
Contributor

@YvesDup YvesDup commented Apr 5, 2025

ProcessPoolExecutor and ThreadPoolExecutor swallows falsy exception.

Fix tests about exception variable in 2 files of /Lib/concurrent/futures

  • _base.py
  • process.py

Changes 2 tests as below:

Replace if exception: with if exception is not None:

@@ -0,0 +1 @@
Fix tests of exception attribute in :method:`process_result_item`of :class:`_ExecutorManagerThread` and :method:`__get_result` of :class:`Future` in module :lib:`concurrent`.
Copy link
Member

@ZeroIntensity ZeroIntensity Apr 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the effort, but this is too many technical details for a blurb entry.

Suggested change
Fix tests of exception attribute in :method:`process_result_item`of :class:`_ExecutorManagerThread` and :method:`__get_result` of :class:`Future` in module :lib:`concurrent`.
Prevent exceptions that evaluate as falsey from being ignored
by :class:`concurrent.futures.ProcessPoolExecutor`.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not limited to __bool__, it can be anything that is falsey, including a class which implements __len__ as well (https://docs.python.org/3/reference/datamodel.html#object.__bool__)

Comment on lines +340 to +346
class MyException(Exception):
def __bool__(self):
return False

@classmethod
def raiser(cls):
raise cls.MyException("foo")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should isolate these to the test case, not the test class. It should be something like this:

def test_swallows_falsy_exceptions(self):
    class MyException(Exception):
        ...

    def raiser():
        raise MyException(...)

    ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a class which does not specify __bool__ but specifies __len__ and returns 0? TiA

raise cls.MyException("foo")

def test_swallows_falsy_exceptions(self):
# fix gh-132063 issue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you give a brief description of the problem in this comment?

def test_swallows_falsy_exceptions(self):
# fix gh-132063 issue
with self.assertRaisesRegex(self.MyException, "foo"):
with self.executor_type(max_workers=1) as executor:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this test case should go in the ExecutorTest base class.

@@ -112,6 +112,19 @@ def log_n_wait(ident):
# ident='third' is cancelled because it remained in the collection of futures
self.assertListEqual(log, ["ident='first' started", "ident='first' stopped"])

class MyException(Exception):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we don't need to repeat the test case. Let's move it to ExecutorTest.

@python-cla-bot
Copy link

python-cla-bot bot commented Apr 6, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never sent my review.

@@ -0,0 +1 @@
Fix tests of exception attribute in :method:`process_result_item`of :class:`_ExecutorManagerThread` and :method:`__get_result` of :class:`Future` in module :lib:`concurrent`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not limited to __bool__, it can be anything that is falsey, including a class which implements __len__ as well (https://docs.python.org/3/reference/datamodel.html#object.__bool__)

Comment on lines +340 to +346
class MyException(Exception):
def __bool__(self):
return False

@classmethod
def raiser(cls):
raise cls.MyException("foo")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a class which does not specify __bool__ but specifies __len__ and returns 0? TiA

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

Successfully merging this pull request may close these issues.

3 participants