Skip to content

Commit 2528e46

Browse files
[3.12] gh-116143: Fix race condition in pydoc _start_server (GH-116144) (#116415)
gh-116143: Fix race condition in pydoc _start_server (GH-116144) (cherry picked from commit 02ee475) Co-authored-by: Itamar Oren <[email protected]>
1 parent 5c69f60 commit 2528e46

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

Lib/pydoc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,6 +2493,7 @@ def __init__(self, urlhandler, host, port):
24932493
threading.Thread.__init__(self)
24942494
self.serving = False
24952495
self.error = None
2496+
self.docserver = None
24962497

24972498
def run(self):
24982499
"""Start the server."""
@@ -2525,9 +2526,9 @@ def stop(self):
25252526

25262527
thread = ServerThread(urlhandler, hostname, port)
25272528
thread.start()
2528-
# Wait until thread.serving is True to make sure we are
2529-
# really up before returning.
2530-
while not thread.error and not thread.serving:
2529+
# Wait until thread.serving is True and thread.docserver is set
2530+
# to make sure we are really up before returning.
2531+
while not thread.error and not (thread.serving and thread.docserver):
25312532
time.sleep(.01)
25322533
return thread
25332534

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a race in pydoc ``_start_server``, eliminating a window in which
2+
``_start_server`` can return a thread that is "serving" but without a
3+
``docserver`` set.

0 commit comments

Comments
 (0)