From 9afa6c5aa7c990f22136518e9f972c908b8f7bb9 Mon Sep 17 00:00:00 2001 From: Itamar Oren Date: Thu, 29 Feb 2024 12:12:05 -0800 Subject: [PATCH 1/3] gh-116143: Fix race condition in pydoc _start_server --- Lib/pydoc.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/pydoc.py b/Lib/pydoc.py index b0193b4a85164a..7ea52e2817281f 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -2495,6 +2495,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.""" @@ -2527,9 +2528,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 From e5ec6ac1e0aae16ef76015f7b865effee33ab635 Mon Sep 17 00:00:00 2001 From: Itamar Oren Date: Tue, 5 Mar 2024 20:53:48 -0800 Subject: [PATCH 2/3] Add NEWS entry --- .../Library/2024-03-05-20-53-34.gh-issue-116143.sww6Zl.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-03-05-20-53-34.gh-issue-116143.sww6Zl.rst 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..44288239a56243 --- /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. From 72e6037948de642ae13fb772c5ef8d1478526d3c Mon Sep 17 00:00:00 2001 From: Itamar Oren Date: Tue, 5 Mar 2024 21:03:23 -0800 Subject: [PATCH 3/3] one day I will get rst right in the first attempt. maybe. --- .../Library/2024-03-05-20-53-34.gh-issue-116143.sww6Zl.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 index 44288239a56243..07aa312ee25f3b 100644 --- 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 @@ -1,3 +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. +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.