GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-109370: Fix unexpected traceback output in test_concurrent_futures by serhiy-storchaka · Pull Request #109780 · python/cpython

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Sep 23, 2023 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Follow-up of gh-107219.

  • Only close the connection writer on Windows.
  • Also use existing constant _winapi.ERROR_OPERATION_ABORTED instead of WSA_OPERATION_ABORTED.
  • Issue: Unexpected traceback output in test_concurrent_futures and crash #109370

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka
pythongh-109370: Fix unexpected traceback output in test_concurrent_f…
3030774
…utures

Follow-up of pythongh-107219.

* Only close the connection writer on Windows.
* Also use existing constant _winapi.ERROR_OPERATION_ABORTED instead of
  WSA_OPERATION_ABORTED.
@serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error skip news needs backport to 3.11 only security fixes needs backport to 3.12 only security fixes labels Sep 23, 2023
@serhiy-storchaka
serhiy-storchaka requested a review from vstinner September 23, 2023 08:26
@bedevere-app bedevere-app Bot added the awaiting review label Sep 23, 2023
@bedevere-app bedevere-app Bot mentioned this pull request Sep 23, 2023
Unexpected traceback output in test_concurrent_futures and crash #109370
Closed
@serhiy-storchaka serhiy-storchaka mentioned this pull request Sep 23, 2023
gh-109370: Support closing Connection and PipeConnection from other thread #109397
Closed
vstinner
vstinner reviewed Sep 25, 2023
View reviewed changes
Comment thread Lib/concurrent/futures/process.py
# gh-107219: Close the connection writer which can unblock
# Queue._feed() if it was stuck in send_bytes().
self.call_queue._writer.close()
if sys.platform == 'win32':

@vstinner vstinner Sep 25, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Why is it an issue on other platforms?

See also PR gh-109810 which goes further in terminate_broken(), to try harder to close everything, and terminate the child processes.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Sep 25, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Because of scenarios described in #109397 (comment). It is not possible to safely close a file descriptor in other thread without using some kind of locking, and it is difficult if possible without rewriting the way the GIL or all IO works even to use locking.

It may be lesser issue on Windows (if handles are never reused, I do not know if it is so), but I am not sure that it is bug-free there. It may be simply that bugs are rarely manifested on Windows because most time the queue thread spends in WaitForMultipleObjects().

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Sep 25, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

It is not possible to safely close a file descriptor in other thread without using some kind of locking

It's maybe time to add "some kind of locking".

There are two cases:

  • Thread B sets the file descriptor / handle to None, and then close FD / handle. No other thread was using it, no problem. Next, when thread A tries to use it, "closed FD/handle" exception is raised.

  • Thread B closes the FD/handle while thread A is using it. That's an important use case to be able to unblock multiprocessing when it goes into troubles with a sick worker progress, sick manager process, well, when things "go wrong".

For me, the case that we should care about is terminate_broken() of concurrent.futures ProcessPoolExecutor and the associated BrokenProcessPool exception. When everything goes wrong, we should just cleanup resources: terminate processes, close queue, close file descriptors/handles, etc. I don't think that we can still expect worker processes to handle "please stop" commands from the main process.

For example, PR #109810 is when the executor cannot even create new threads. In that case, multiprocessing and concurrent.futures cannot work properly, and it's time to exit as soon as possible.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Sep 26, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

It's maybe time to add "some kind of locking".

Actually, I do not think how is it possible, even with cooperation with GIL. It needs a support in the kernel to allow switching to other thread only when a system call waits. We can reduce the window for race condition, but it will always be possible with some small probability to close and replace file descriptor just before system call.

There are two cases:

There are many other cases, depending on definition of "use it". If the file descriptor is closed or the attribute is set to None between the place where you can realistically check this and the place where it is used, you have an unsolvable problem.

For now, I am worried about regression introduce by #107219 on non-Windows platforms. Since the original issue was Windows-only, I prefer to apply the solution (even if it may be non-perfect) on Windows where it fixes serious issue and not apply it other platforms where it seems unnecessary but causes visible regression.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
vstinner
vstinner approved these changes Sep 26, 2023
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM.

Honestly, I don't understand well this code. Apparently, there are multiple known issues. There is no simple fix. This change restores the code to the previous "status quo" and keep my fix for the other issue. It sounds like an acceptable tradeoff.

Thanks for reusing ERROR_OPERATION_ABORTED macro, I dislike hardcoded constants :-)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting core review and removed awaiting review labels Sep 26, 2023
@serhiy-storchaka
serhiy-storchaka merged commit 0b4e090 into python:main Sep 26, 2023
@miss-islington

miss-islington commented Sep 26, 2023

Copy link
Copy Markdown
Contributor

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting core review label Sep 26, 2023
@serhiy-storchaka
serhiy-storchaka deleted the gh-109370 branch September 26, 2023 07:06
@miss-islington

miss-islington commented Sep 26, 2023

Copy link
Copy Markdown
Contributor

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 0b4e090422db5f959184353d53552d1675f74212 3.12

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington miss-islington assigned serhiy-storchaka Sep 26, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 26, 2023
@serhiy-storchaka @miss-islington
pythongh-109370: Fix unexpected traceback output in test_concurrent_f…
4e08e35
…utures (pythonGH-109780)

Follow-up of pythongh-107219.

* Only close the connection writer on Windows.
* Also use existing constant _winapi.ERROR_OPERATION_ABORTED instead of
  WSA_OPERATION_ABORTED.
(cherry picked from commit 0b4e090)

Co-authored-by: Serhiy Storchaka <[email protected]>
@bedevere-app

bedevere-app Bot commented Sep 26, 2023

Copy link
Copy Markdown

GH-109882 is a backport of this pull request to the 3.11 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.11 only security fixes label Sep 26, 2023
serhiy-storchaka added a commit that referenced this pull request Sep 26, 2023
@miss-islington @serhiy-storchaka
[3.11] gh-109370: Fix unexpected traceback output in test_concurrent_…
8fef834
…futures (GH-109780) (GH-109882)

Follow-up of gh-107219.

* Only close the connection writer on Windows.
* Also use existing constant _winapi.ERROR_OPERATION_ABORTED instead of
  WSA_OPERATION_ABORTED.
(cherry picked from commit 0b4e090)

Co-authored-by: Serhiy Storchaka <[email protected]>
csm10495 pushed a commit to csm10495/cpython that referenced this pull request Sep 28, 2023
@serhiy-storchaka @csm10495
pythongh-109370: Fix unexpected traceback output in test_concurrent_f…
4ddd6fa
…utures (pythonGH-109780)

Follow-up of pythongh-107219.

* Only close the connection writer on Windows.
* Also use existing constant _winapi.ERROR_OPERATION_ABORTED instead of
  WSA_OPERATION_ABORTED.
@hugovk

hugovk commented Nov 9, 2023

Copy link
Copy Markdown
Member

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.12 due to a conflict. Please backport using cherry_picker on command line. cherry_picker 0b4e090422db5f959184353d53552d1675f74212 3.12

@serhiy-storchaka This fix has been merged to main and 3.11, please could you also do 3.12?

serhiy-storchaka reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Nov 10, 2023
@serhiy-storchaka
[3.12] pythongh-109370: Fix unexpected traceback output in test_concu…
7d01d51
…rrent_futures (pythonGH-109780)

Follow-up of pythongh-107219.

* Only close the connection writer on Windows.
* Also use existing constant _winapi.ERROR_OPERATION_ABORTED instead of
  WSA_OPERATION_ABORTED.
(cherry picked from commit 0b4e090)

Co-authored-by: Serhiy Storchaka <[email protected]>
@bedevere-app

bedevere-app Bot commented Nov 10, 2023

Copy link
Copy Markdown

GH-111934 is a backport of this pull request to the 3.12 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.12 only security fixes label Nov 10, 2023
serhiy-storchaka added a commit that referenced this pull request Nov 10, 2023
@serhiy-storchaka
[3.12] gh-109370: Fix unexpected traceback output in test_concurrent_…
4018209
…futures (GH-109780) (GH-111934)

Follow-up of gh-107219.

* Only close the connection writer on Windows.
* Also use existing constant _winapi.ERROR_OPERATION_ABORTED instead of
  WSA_OPERATION_ABORTED.
(cherry picked from commit 0b4e090)
@serhiy-storchaka

serhiy-storchaka commented Nov 10, 2023

Copy link
Copy Markdown
Member Author

Thank you for your reminder @hugovk.

hugovk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
@serhiy-storchaka @Glyphack
pythongh-109370: Fix unexpected traceback output in test_concurrent_f…
25b6222
…utures (pythonGH-109780)

Follow-up of pythongh-107219.

* Only close the connection writer on Windows.
* Also use existing constant _winapi.ERROR_OPERATION_ABORTED instead of
  WSA_OPERATION_ABORTED.
@serhiy-storchaka serhiy-storchaka removed their assignment Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news type-bug An unexpected behavior, bug, or error

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@serhiy-storchaka @miss-islington @hugovk @vstinner
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.