@@ -3196,13 +3196,17 @@ def cmdloop(self):
31963196 self .process_payload (payload )
31973197
31983198 def send_interrupt (self ):
3199- # Write to a socket that the PDB server listens on. This triggers
3200- # the remote to raise a SIGINT for itself. We do this because
3201- # Windows doesn't allow triggering SIGINT remotely.
3202- # See https://stackoverflow.com/a/35792192 for many more details.
3203- # We could directly send SIGINT to the remote process on Unix, but
3204- # doing the same thing on all platforms simplifies maintenance.
3205- self .interrupt_sock .sendall (signal .SIGINT .to_bytes ())
3199+ if self .interrupt_sock is not None :
3200+ # Write to a socket that the PDB server listens on. This triggers
3201+ # the remote to raise a SIGINT for itself. We do this because
3202+ # Windows doesn't allow triggering SIGINT remotely.
3203+ # See https://stackoverflow.com/a/35792192 for many more details.
3204+ self .interrupt_sock .sendall (signal .SIGINT .to_bytes ())
3205+ else :
3206+ # On Unix we can just send a SIGINT to the remote process.
3207+ # This is preferable to using the signal thread approach that we
3208+ # use on Windows because it can interrupt IO in the main thread.
3209+ os .kill (self .pid , signal .SIGINT )
32063210
32073211 def process_payload (self , payload ):
32083212 match payload :
@@ -3293,9 +3297,8 @@ def _connect(*, host, port, frame, commands, version, signal_raising_thread):
32933297 with closing (socket .create_connection ((host , port ))) as conn :
32943298 sockfile = conn .makefile ("rwb" )
32953299
3296- # Starting a signal raising thread is optional to allow us the flexibility
3297- # to switch to sending signals directly on Unix platforms in the future
3298- # without breaking backwards compatibility. This also makes tests simpler.
3300+ # The client requests this thread on Windows but not on Unix.
3301+ # Most tests don't request this thread, to keep them simpler.
32993302 if signal_raising_thread :
33003303 signal_server = (host , port )
33013304 else :
@@ -3332,6 +3335,8 @@ def attach(pid, commands=()):
33323335 tempfile .NamedTemporaryFile ("w" , delete_on_close = False )
33333336 )
33343337
3338+ use_signal_thread = sys .platform == "win32"
3339+
33353340 connect_script .write (
33363341 textwrap .dedent (
33373342 f"""
@@ -3342,7 +3347,7 @@ def attach(pid, commands=()):
33423347 frame=sys._getframe(1),
33433348 commands={ json .dumps ("\n " .join (commands ))} ,
33443349 version={ _PdbServer .protocol_version ()} ,
3345- signal_raising_thread=True ,
3350+ signal_raising_thread={ use_signal_thread !r } ,
33463351 )
33473352 """
33483353 )
@@ -3354,9 +3359,12 @@ def attach(pid, commands=()):
33543359 client_sock , _ = server .accept ()
33553360 stack .enter_context (closing (client_sock ))
33563361
3357- interrupt_sock , _ = server .accept ()
3358- stack .enter_context (closing (interrupt_sock ))
3359- interrupt_sock .setblocking (False )
3362+ if use_signal_thread :
3363+ interrupt_sock , _ = server .accept ()
3364+ stack .enter_context (closing (interrupt_sock ))
3365+ interrupt_sock .setblocking (False )
3366+ else :
3367+ interrupt_sock = None
33603368
33613369 _PdbClient (pid , client_sock , interrupt_sock ).cmdloop ()
33623370
0 commit comments