Skip to content

gh-132063: ProcessPoolExecutor swallows falsy Exceptions #132129

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

Merged

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__)

Copy link
Contributor Author

@YvesDup YvesDup Apr 7, 2025

Choose a reason for hiding this comment

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

I suggest:

Prevent exceptions, including group exceptions, that evaluate as falsey (With bool and len dunder methods returns) from being ignored
by :class:concurrent.futures.ProcessPoolExecutor and :class:concurrent.futures.ThreadPoolExecutor.

Copy link
Member

Choose a reason for hiding this comment

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

I don't really like to mention bool and len again so how about linking falsey to object.__bool__?

:meth:`falsey <object.__bool__>`

Copy link
Member

@ZeroIntensity ZeroIntensity Apr 7, 2025

Choose a reason for hiding this comment

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

It might be worth adding a "falsey" term to the glossary, but that should be done elsewhere.

@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__)

@YvesDup
Copy link
Contributor Author

YvesDup commented Apr 7, 2025

After more tests, I found that __cause__, __context__ attributes in falsey exceptions are ignored.

if (e and e.__cause__ is not None

if (e and e.__context__ is not None

And falsey BaseExceptionGroup exception is ignored too.
if e and isinstance(e, BaseExceptionGroup):

Should we open a new issue with these bugs or integrate them to this PR ?

EDIT: please ignore this comment

YvesDup and others added 3 commits April 8, 2025 14:18
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
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.

Some nits and LGTM.


msg = 'boolbool'
with self.assertRaisesRegex(FalseyBoolException, msg):
self.executor.submit(raiser, FalseyBoolException, msg).result()
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 for a possible follow-up but this kind of call makes me wonder whether the base class shouldn't have some def submit_and_fetch(self) method that does this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does this method return directy the result ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a follow-up with the TracebackExceptionclass which swallows __cause__ and __context__ content of falsey exceptions.
I will post a new issue about this.

@picnixz picnixz enabled auto-merge (squash) April 8, 2025 14:55
@picnixz picnixz added the needs backport to 3.13 bugs and security fixes label Apr 8, 2025
@picnixz picnixz merged commit 933c665 into python:main Apr 8, 2025
41 checks passed
@miss-islington-app
Copy link

Thanks @YvesDup for the PR, and @picnixz for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 8, 2025
…alsey exceptions (pythonGH-132129)

(cherry picked from commit 933c665)

Co-authored-by: Duprat <yduprat@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Apr 8, 2025

GH-132275 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Apr 8, 2025
picnixz pushed a commit that referenced this pull request Apr 8, 2025
…falsey exceptions (GH-132129) (#132275)

gh-132063: ensure that `ProcessPoolExecutor` does not swallow falsey exceptions (GH-132129)
(cherry picked from commit 933c665)

Co-authored-by: Duprat <yduprat@gmail.com>
seehwan pushed a commit to seehwan/cpython that referenced this pull request Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants