Skip to content

Commit

Permalink
gh-116143: Fix race condition in pydoc _start_server (#116144)
Browse files Browse the repository at this point in the history
  • Loading branch information
itamaro committed Mar 6, 2024
1 parent e800265 commit 02ee475
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Lib/pydoc.py
Expand Up @@ -2504,6 +2504,7 @@ def __init__(self, urlhandler, host, port):
threading.Thread.__init__(self)
self.serving = False
self.error = None
self.docserver = None

def run(self):
"""Start the server."""
Expand Down Expand Up @@ -2536,9 +2537,9 @@ def stop(self):

thread = ServerThread(urlhandler, hostname, port)
thread.start()
# Wait until thread.serving is True to make sure we are
# really up before returning.
while not thread.error and not thread.serving:
# Wait until thread.serving is True and thread.docserver is set
# to make sure we are really up before returning.
while not thread.error and not (thread.serving and thread.docserver):
time.sleep(.01)
return thread

Expand Down
@@ -0,0 +1,3 @@
Fix a race in pydoc ``_start_server``, eliminating a window in which
``_start_server`` can return a thread that is "serving" but without a
``docserver`` set.

0 comments on commit 02ee475

Please sign in to comment.