Skip to content

[WIP] bpo-36402: add random sleep to test_threads_join_2() #13889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,36 +894,34 @@ def f():
self.assertEqual(os.read(r, 1), b"x")

def test_threads_join_2(self):
# Same as above, but a delay gets introduced after the thread's
# Python code returned but before the thread state is deleted.
# To achieve this, we register a thread-local object which sleeps
# a bit when deallocated.
r, w = os.pipe()
self.addCleanup(os.close, r)
self.addCleanup(os.close, w)
code = r"""if 1:
import os
import threading
import time
import random

def random_sleep():
seconds = random.random() * 0.010
time.sleep(seconds)

class Sleeper:
def __del__(self):
time.sleep(0.05)
random_sleep()

tls = threading.local()

def f():
# Sleep a bit so that the thread is still running when
# Py_EndInterpreter is called.
time.sleep(0.05)
random_sleep()
tls.x = Sleeper()
os.write(%d, b"x")
random_sleep()

threading.Thread(target=f).start()
""" % (w,)
random_sleep()
"""
ret = test.support.run_in_subinterp(code)
self.assertEqual(ret, 0)
# The thread was joined properly.
self.assertEqual(os.read(r, 1), b"x")

@cpython_only
def test_daemon_threads_fatal_error(self):
Expand Down