@@ -3196,13 +3196,17 @@ def cmdloop(self):
3196
3196
self .process_payload (payload )
3197
3197
3198
3198
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 )
3206
3210
3207
3211
def process_payload (self , payload ):
3208
3212
match payload :
@@ -3293,9 +3297,8 @@ def _connect(*, host, port, frame, commands, version, signal_raising_thread):
3293
3297
with closing (socket .create_connection ((host , port ))) as conn :
3294
3298
sockfile = conn .makefile ("rwb" )
3295
3299
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.
3299
3302
if signal_raising_thread :
3300
3303
signal_server = (host , port )
3301
3304
else :
@@ -3332,6 +3335,8 @@ def attach(pid, commands=()):
3332
3335
tempfile .NamedTemporaryFile ("w" , delete_on_close = False )
3333
3336
)
3334
3337
3338
+ use_signal_thread = sys .platform == "win32"
3339
+
3335
3340
connect_script .write (
3336
3341
textwrap .dedent (
3337
3342
f"""
@@ -3342,7 +3347,7 @@ def attach(pid, commands=()):
3342
3347
frame=sys._getframe(1),
3343
3348
commands={ json .dumps ("\n " .join (commands ))} ,
3344
3349
version={ _PdbServer .protocol_version ()} ,
3345
- signal_raising_thread=True ,
3350
+ signal_raising_thread={ use_signal_thread !r } ,
3346
3351
)
3347
3352
"""
3348
3353
)
@@ -3354,9 +3359,12 @@ def attach(pid, commands=()):
3354
3359
client_sock , _ = server .accept ()
3355
3360
stack .enter_context (closing (client_sock ))
3356
3361
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
3360
3368
3361
3369
_PdbClient (pid , client_sock , interrupt_sock ).cmdloop ()
3362
3370
0 commit comments