21
21
if sys .platform != "win32" :
22
22
from signal import SIGKILL
23
23
else :
24
- SIGKILL = None
24
+ SIGKILL = "windown-SIGKILL-sentinel"
25
25
26
26
27
27
try :
@@ -1136,6 +1136,31 @@ def _input_request(self, prompt, ident, parent, password=False):
1136
1136
raise EOFError
1137
1137
return value
1138
1138
1139
+
1140
+ async def _killpg (self , * , signal ):
1141
+ """
1142
+ similar to killpg but use psutil if it can on windows
1143
+ or if pgid is none
1144
+
1145
+ """
1146
+ pgid = os .getpgid (os .getpid ())
1147
+ if pgid and hasattr (os , "killpg" ):
1148
+ try :
1149
+ os .killpg (pgid , signal )
1150
+ except OSError :
1151
+ self .log .warning ("OSError running killpg, not killing children" )
1152
+ return
1153
+ elif psutil is not None :
1154
+ children = parent .children (recursive = True )
1155
+ for p in children :
1156
+ try :
1157
+ if signal == SIGTERM :
1158
+ p .terminate ()
1159
+ elif signal == SIGKILL :
1160
+ p .kill ()
1161
+ except psutil .NoSuchProcess :
1162
+ pass
1163
+
1139
1164
async def _progressively_terminate_all_children (self ):
1140
1165
if sys .platform == "win32" :
1141
1166
self .log .info (f"Terminating subprocesses not yet supported on windows." )
@@ -1152,10 +1177,10 @@ async def _progressively_terminate_all_children(self):
1152
1177
try :
1153
1178
await asyncio .sleep (0.05 )
1154
1179
self .log .debug ("Sending SIGTERM to {pgid}" )
1155
- os . killpg ( pgid , SIGTERM )
1180
+ self . _killpg ( SIGTERM )
1156
1181
await asyncio .sleep (0.05 )
1157
1182
self .log .debug ("Sending SIGKILL to {pgid}" )
1158
- os . killpg (pgid , SIGKILL )
1183
+ self . _killpg (pgid , SIGKILL )
1159
1184
except Exception :
1160
1185
self .log .exception ("Exception during subprocesses termination" )
1161
1186
return
@@ -1177,12 +1202,7 @@ async def _progressively_terminate_all_children(self):
1177
1202
if not children :
1178
1203
self .log .debug ("No more children, continuing shutdown routine." )
1179
1204
return
1180
- if pgid and hasattr (os , "killpg" ):
1181
- try :
1182
- os .killpg (pgid , signum )
1183
- except OSError :
1184
- self .log .warning ("OSError running killpg, not killing children" )
1185
- return
1205
+ self ._killpg (signum )
1186
1206
self .log .debug (
1187
1207
f"Will sleep { delay } s before checking for children and retrying."
1188
1208
)
0 commit comments