diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 56a47dc68a6663..a8cfeaf925fdce 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -2491,6 +2491,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.""" @@ -2523,9 +2524,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 diff --git a/Misc/NEWS.d/next/Library/2024-03-05-20-53-34.gh-issue-116143.sww6Zl.rst b/Misc/NEWS.d/next/Library/2024-03-05-20-53-34.gh-issue-116143.sww6Zl.rst new file mode 100644 index 00000000000000..07aa312ee25f3b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-03-05-20-53-34.gh-issue-116143.sww6Zl.rst @@ -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.