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

This commit is contained in:
Itamar Oren 2024-03-06 06:39:51 -08:00 committed by GitHub
parent e800265aa1
commit 02ee475ee3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 3 deletions

View File

@ -2504,6 +2504,7 @@ def _start_server(urlhandler, hostname, port):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.serving = False self.serving = False
self.error = None self.error = None
self.docserver = None
def run(self): def run(self):
"""Start the server.""" """Start the server."""
@ -2536,9 +2537,9 @@ def _start_server(urlhandler, hostname, port):
thread = ServerThread(urlhandler, hostname, port) thread = ServerThread(urlhandler, hostname, port)
thread.start() thread.start()
# Wait until thread.serving is True to make sure we are # Wait until thread.serving is True and thread.docserver is set
# really up before returning. # to make sure we are really up before returning.
while not thread.error and not thread.serving: while not thread.error and not (thread.serving and thread.docserver):
time.sleep(.01) time.sleep(.01)
return thread return thread

View File

@ -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.