MCPcopy Index your code
hub / github.com/python/cpython / _wait_for_signal

Function _wait_for_signal

Lib/test/test_external_inspection.py:84–123  ·  view source on GitHub ↗

Wait for expected signal(s) from a socket with proper timeout and EOF handling. Args: sock: Connected socket to read from expected_signals: Single bytes object or list of bytes objects to wait for timeout: Socket timeout in seconds Returns: bytes: Compl

(sock, expected_signals, timeout=SHORT_TIMEOUT)

Source from the content-addressed store, hash-verified

82
83
84def _wait_for_signal(sock, expected_signals, timeout=SHORT_TIMEOUT):
85 """
86 Wait for expected signal(s) from a socket with proper timeout and EOF handling.
87
88 Args:
89 sock: Connected socket to read from
90 expected_signals: Single bytes object or list of bytes objects to wait for
91 timeout: Socket timeout in seconds
92
93 Returns:
94 bytes: Complete accumulated response buffer
95
96 Raises:
97 RuntimeError: If connection closed before signal received or timeout
98 """
99 if isinstance(expected_signals, bytes):
100 expected_signals = [expected_signals]
101
102 sock.settimeout(timeout)
103 buffer = b""
104
105 while True:
106 # Check if all expected signals are in buffer
107 if all(sig in buffer for sig in expected_signals):
108 return buffer
109
110 try:
111 chunk = sock.recv(4096)
112 if not chunk:
113 # EOF - connection closed
114 raise RuntimeError(
115 f"Connection closed before receiving expected signals. "
116 f"Expected: {expected_signals}, Got: {buffer[-200:]!r}"
117 )
118 buffer += chunk
119 except socket.timeout:
120 raise RuntimeError(
121 f"Timeout waiting for signals. "
122 f"Expected: {expected_signals}, Got: {buffer[-200:]!r}"
123 )
124
125
126def _wait_for_n_signals(sock, signal_pattern, count, timeout=SHORT_TIMEOUT):

Calls 2

settimeoutMethod · 0.45
recvMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…