Skip to content

Commit 686b4b5

Browse files
authored
bpo-34130: Fix test_signal.test_warn_on_full_buffer() (GH-8327)
On Windows, sometimes test_signal.test_warn_on_full_buffer() fails to fill the socketpair buffer. In that case, the C signal handler succeed to write into the socket, it doesn't log the expected send error, and so the test fail. On Windows, the test now uses a timeout of 50 ms to fill the socketpair buffer to fix this race condition. Other changes: * Begin with large chunk size to fill the buffer to speed up the test. * Add error messages to assertion errors to more easily identify which assertion failed. * Don't set the read end of the socketpair as non-blocking.
1 parent 99bb6df commit 686b4b5

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

Lib/test/test_signal.py

+41-15
Original file line numberDiff line numberDiff line change
@@ -479,26 +479,51 @@ def handler(signum, frame):
479479
signal.signal(signum, handler)
480480
481481
read, write = socket.socketpair()
482-
read.setblocking(False)
483-
write.setblocking(False)
484482
485-
# Fill the send buffer
483+
# Fill the socketpair buffer
484+
if sys.platform == 'win32':
485+
# bpo-34130: On Windows, sometimes non-blocking send fails to fill
486+
# the full socketpair buffer, so use a timeout of 50 ms instead.
487+
write.settimeout(0.050)
488+
else:
489+
write.setblocking(False)
490+
491+
# Start with large chunk size to reduce the
492+
# number of send needed to fill the buffer.
493+
written = 0
494+
for chunk_size in (2 ** 16, 2 ** 8, 1):
495+
chunk = b"x" * chunk_size
496+
try:
497+
while True:
498+
write.send(chunk)
499+
written += chunk_size
500+
except (BlockingIOError, socket.timeout):
501+
pass
502+
503+
print(f"%s bytes written into the socketpair" % written, flush=True)
504+
505+
write.setblocking(False)
486506
try:
487-
while True:
488-
write.send(b"x")
507+
write.send(b"x")
489508
except BlockingIOError:
509+
# The socketpair buffer seems full
490510
pass
511+
else:
512+
raise AssertionError("%s bytes failed to fill the socketpair "
513+
"buffer" % written)
491514
492515
# By default, we get a warning when a signal arrives
516+
msg = ('Exception ignored when trying to {action} '
517+
'to the signal wakeup fd')
493518
signal.set_wakeup_fd(write.fileno())
494519
495520
with captured_stderr() as err:
496521
_testcapi.raise_signal(signum)
497522
498523
err = err.getvalue()
499-
if ('Exception ignored when trying to {action} to the signal wakeup fd'
500-
not in err):
501-
raise AssertionError(err)
524+
if msg not in err:
525+
raise AssertionError("first set_wakeup_fd() test failed, "
526+
"stderr: %r" % err)
502527
503528
# And also if warn_on_full_buffer=True
504529
signal.set_wakeup_fd(write.fileno(), warn_on_full_buffer=True)
@@ -507,9 +532,9 @@ def handler(signum, frame):
507532
_testcapi.raise_signal(signum)
508533
509534
err = err.getvalue()
510-
if ('Exception ignored when trying to {action} to the signal wakeup fd'
511-
not in err):
512-
raise AssertionError(err)
535+
if msg not in err:
536+
raise AssertionError("set_wakeup_fd(warn_on_full_buffer=True) "
537+
"test failed, stderr: %r" % err)
513538
514539
# But not if warn_on_full_buffer=False
515540
signal.set_wakeup_fd(write.fileno(), warn_on_full_buffer=False)
@@ -519,7 +544,8 @@ def handler(signum, frame):
519544
520545
err = err.getvalue()
521546
if err != "":
522-
raise AssertionError("got unexpected output %r" % (err,))
547+
raise AssertionError("set_wakeup_fd(warn_on_full_buffer=False) "
548+
"test failed, stderr: %r" % err)
523549
524550
# And then check the default again, to make sure warn_on_full_buffer
525551
# settings don't leak across calls.
@@ -529,9 +555,9 @@ def handler(signum, frame):
529555
_testcapi.raise_signal(signum)
530556
531557
err = err.getvalue()
532-
if ('Exception ignored when trying to {action} to the signal wakeup fd'
533-
not in err):
534-
raise AssertionError(err)
558+
if msg not in err:
559+
raise AssertionError("second set_wakeup_fd() test failed, "
560+
"stderr: %r" % err)
535561
536562
""".format(action=action)
537563
assert_python_ok('-c', code)

0 commit comments

Comments
 (0)