@@ -1136,8 +1136,7 @@ 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 ):
1139
+ def _killpg (self , signal ):
1141
1140
"""
1142
1141
similar to killpg but use psutil if it can on windows
1143
1142
or if pgid is none
@@ -1147,8 +1146,8 @@ async def _killpg(self, *, signal):
1147
1146
if pgid and hasattr (os , "killpg" ):
1148
1147
try :
1149
1148
os .killpg (pgid , signal )
1150
- except OSError :
1151
- self .log .warning ( "OSError running killpg, not killing children" )
1149
+ except ( OSError ) as e :
1150
+ self .log .exception ( f "OSError running killpg, not killing children. " )
1152
1151
return
1153
1152
elif psutil is not None :
1154
1153
children = parent .children (recursive = True )
@@ -1162,30 +1161,20 @@ async def _killpg(self, *, signal):
1162
1161
pass
1163
1162
1164
1163
async def _progressively_terminate_all_children (self ):
1165
- if sys .platform == "win32" :
1166
- self .log .info (f"Terminating subprocesses not yet supported on windows." )
1167
- return
1168
1164
1169
1165
pgid = os .getpgid (os .getpid ())
1170
- if not pgid :
1171
- self .log .warning (f"No Pgid ({ pgid } ), not trying to stop subprocesses." )
1172
- return
1173
1166
if psutil is None :
1174
1167
# blindly send quickly sigterm/sigkill to processes if psutil not there.
1175
- self .log .debug ("Please install psutil for a cleaner subprocess shutdown." )
1168
+ self .log .info ("Please install psutil for a cleaner subprocess shutdown." )
1176
1169
self ._send_interupt_children ()
1177
- try :
1178
- await asyncio .sleep (0.05 )
1179
- self .log .debug ("Sending SIGTERM to {pgid}" )
1180
- self ._killpg (SIGTERM )
1181
- await asyncio .sleep (0.05 )
1182
- self .log .debug ("Sending SIGKILL to {pgid}" )
1183
- self ._killpg (pgid , SIGKILL )
1184
- except Exception :
1185
- self .log .exception ("Exception during subprocesses termination" )
1186
- return
1187
-
1188
- sleeps = (0.01 , 0.03 , 0.1 , 0.3 , 1 )
1170
+ await asyncio .sleep (0.05 )
1171
+ self .log .debug ("Sending SIGTERM to {pgid}" )
1172
+ self ._killpg (SIGTERM )
1173
+ await asyncio .sleep (0.05 )
1174
+ self .log .debug ("Sending SIGKILL to {pgid}" )
1175
+ self ._killpg (pgid , SIGKILL )
1176
+
1177
+ sleeps = (0.01 , 0.03 , 0.1 , 0.3 , 1 , 3 , 10 )
1189
1178
children = psutil .Process ().children (recursive = True )
1190
1179
if not children :
1191
1180
self .log .debug ("Kernel has no children." )
@@ -1195,24 +1184,38 @@ async def _progressively_terminate_all_children(self):
1195
1184
1196
1185
for signum in (SIGTERM , SIGKILL ):
1197
1186
self .log .debug (
1198
- f"Will try to send { signum } ({ Signals (signum )} ) to subprocesses :{ children } "
1187
+ f"Will try to send { signum } ({ Signals (signum )!r } ) to subprocesses :{ children } "
1199
1188
)
1200
1189
for delay in sleeps :
1201
1190
children = psutil .Process ().children (recursive = True )
1202
- if not children :
1203
- self .log .debug ("No more children, continuing shutdown routine." )
1204
- return
1205
- self ._killpg (signum )
1191
+ try :
1192
+ if not children :
1193
+ self .log .warning (
1194
+ "No more children, continuing shutdown routine."
1195
+ )
1196
+ return
1197
+ except psutil .NoSuchProcess :
1198
+ pass
1199
+ self ._killpg (15 )
1206
1200
self .log .debug (
1207
- f"Will sleep { delay } s before checking for children and retrying."
1201
+ f"Will sleep { delay } s before checking for children and retrying. { children } "
1208
1202
)
1209
- await ascynio .sleep (delay )
1203
+ await asyncio .sleep (delay )
1210
1204
1211
1205
async def _at_shutdown (self ):
1212
1206
"""Actions taken at shutdown by the kernel, called by python's atexit.
1213
1207
"""
1214
- await self ._progressively_terminate_all_children ()
1215
- if self ._shutdown_message is not None :
1216
- self .session .send (self .iopub_socket , self ._shutdown_message , ident = self ._topic ('shutdown' ))
1217
- self .log .debug ("%s" , self ._shutdown_message )
1218
- self .control_stream .flush (zmq .POLLOUT )
1208
+ try :
1209
+ await self ._progressively_terminate_all_children ()
1210
+ except Exception as e :
1211
+ self .log .exception ("Exception during subprocesses termination %s" , e )
1212
+
1213
+ finally :
1214
+ if self ._shutdown_message is not None :
1215
+ self .session .send (
1216
+ self .iopub_socket ,
1217
+ self ._shutdown_message ,
1218
+ ident = self ._topic ("shutdown" ),
1219
+ )
1220
+ self .log .debug ("%s" , self ._shutdown_message )
1221
+ self .control_stream .flush (zmq .POLLOUT )
0 commit comments