Skip to content

gh-116143: Fix race condition in pydoc _start_server #116144

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
merged 3 commits into from
Mar 6, 2024

Conversation

itamaro
Copy link
Contributor

@itamaro itamaro commented Feb 29, 2024

Copy link
Contributor

@Jason-Y-Z Jason-Y-Z left a comment

Choose a reason for hiding this comment

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

This is great, thanks! Would it be possible to add a test for this as well?

@itamaro
Copy link
Contributor Author

itamaro commented Mar 3, 2024

This is great, thanks! Would it be possible to add a test for this as well?

Thank you!

I'm not sure about a specific test for this scenario. The existing test_pydoc.PydocServerTest.test_server test kinda covers it, but it triggers only in a very specific thread scheduling sequence. I think a test that would force that exact condition would be complex to write (potentially requiring a bunch of refactoring to allow mocking out the right things), and highly coupled to the internal implementation details.
I might be missing an obvious way to test this scenario, so please let me know if you have something in mind!

Copy link
Member

@carljm carljm left a comment

Choose a reason for hiding this comment

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

It looks like thread.serving will always be set before thread.docserver. It's set in the thread.ready() callback, which is called in DocServer.server_activate, which is called from TCPServer.__init__, so it will definitely happen -- barring an error occurring instead -- before the DocServer(...) initialization returns. So there's not a race condition in the attributes being set, the window is always there; the race is just a matter of whether we get a thread context switch such that _start_server catches that window and is able to return during it.

This change fixes the bug, and looks like the minimal effective fix. I agree that it looks impractical to write a test for this without a significant refactor.

@carljm
Copy link
Member

carljm commented Mar 4, 2024

Oh -- I don't agree with the use of the skip news label on this PR, though. Can you add a news entry? This is potentially a real user-facing issue, not just a tests issue; the fix should be recorded in the changelog.

@carljm carljm removed the skip news label Mar 4, 2024
@Jason-Y-Z
Copy link
Contributor

I see, yes I think in that case maybe we can skip adding tests for this change. Thanks again!

@itamaro
Copy link
Contributor Author

itamaro commented Mar 6, 2024

Oh -- I don't agree with the use of the skip news label on this PR, though. Can you add a news entry? This is potentially a real user-facing issue, not just a tests issue; the fix should be recorded in the changelog.

Added!

@carljm carljm added needs backport to 3.11 only security fixes needs backport to 3.12 only security fixes labels Mar 6, 2024
@carljm carljm merged commit 02ee475 into python:main Mar 6, 2024
@miss-islington-app
Copy link

Thanks @itamaro for the PR, and @carljm for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 6, 2024
@bedevere-app
Copy link

bedevere-app bot commented Mar 6, 2024

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 6, 2024
@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Mar 6, 2024
@bedevere-app
Copy link

bedevere-app bot commented Mar 6, 2024

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

@bedevere-app bedevere-app bot removed the needs backport to 3.11 only security fixes label Mar 6, 2024
carljm pushed a commit that referenced this pull request Mar 6, 2024
… (#116415)

gh-116143: Fix race condition in pydoc _start_server (GH-116144)
(cherry picked from commit 02ee475)

Co-authored-by: Itamar Oren <[email protected]>
@itamaro itamaro deleted the gh-116143-pydoc-race branch March 6, 2024 15:05
carljm pushed a commit that referenced this pull request Mar 6, 2024
… (#116416)

gh-116143: Fix race condition in pydoc _start_server (GH-116144)
(cherry picked from commit 02ee475)

Co-authored-by: Itamar Oren <[email protected]>
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 25, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
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