Skip to content

Commit b9cd46a

Browse files
Use readline to synchronize between procs in remote pdb test
1 parent 670b6cc commit b9cd46a

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

Lib/test/test_remote_pdb.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,6 @@ def test_breakpoints(self):
465465

466466
def test_keyboard_interrupt(self):
467467
"""Test that sending keyboard interrupt breaks into pdb."""
468-
synchronizer_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
469-
synchronizer_sock.bind(('127.0.0.1', 0)) # Let OS assign port
470-
synchronizer_sock.settimeout(SHORT_TIMEOUT)
471-
synchronizer_sock.listen(1)
472-
self.addCleanup(synchronizer_sock.close)
473-
sync_port = synchronizer_sock.getsockname()[1]
474468

475469
script = textwrap.dedent(f"""
476470
import time
@@ -487,11 +481,10 @@ def bar():
487481
version=pdb._PdbServer.protocol_version(),
488482
)
489483
print("Connected to debugger")
490-
iterations = 10
491-
socket.create_connection(('127.0.0.1', {sync_port})).close()
484+
iterations = 50
492485
while iterations > 0:
493-
print("Iteration", iterations)
494-
time.sleep(1)
486+
print("Iteration", iterations, flush=True)
487+
time.sleep(0.2)
495488
iterations -= 1
496489
return 42
497490
@@ -508,22 +501,20 @@ def bar():
508501
# Continue execution
509502
self._send_command(client_file, "c")
510503

511-
# Wait until execution has continued
512-
synchronizer_sock.accept()[0].close()
513-
514-
# Wait a bit so the remote leaves create_connection(). This is not
515-
# required but makes the rest of the test faster as we will exit the main
516-
# loop immediately by setting iterations to 0.
517-
time.sleep(0.1)
504+
# Confirm that the remote is already in the while loop. We know
505+
# it's in bar() and we can exit the loop immediately by setting
506+
# iterations to 0.
507+
while line := process.stdout.readline():
508+
if line.startswith("Iteration"):
509+
break
518510

519511
# Inject a script to interrupt the running process
520512
self._send_interrupt(process.pid)
521513
messages = self._read_until_prompt(client_file)
522514

523-
# Verify we got the keyboard interrupt message. Is possible that we get interrupted somewhere
524-
# in bar() or when leving create_connection()
515+
# Verify we got the keyboard interrupt message.
525516
interrupt_msgs = [msg['message'] for msg in messages if 'message' in msg]
526-
expected_msg = [msg for msg in interrupt_msgs if "bar()" in msg or "create_connection()" in msg]
517+
expected_msg = [msg for msg in interrupt_msgs if "bar()" in msg]
527518
self.assertGreater(len(expected_msg), 0)
528519

529520
# Continue to end as fast as we can

0 commit comments

Comments
 (0)