Skip to content

Commit aaad575

Browse files
committed
handle windows
1 parent 8518d6f commit aaad575

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

ipykernel/kernelbase.py

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,7 @@ def _input_request(self, prompt, ident, parent, password=False):
11361136
raise EOFError
11371137
return value
11381138

1139-
1140-
async def _killpg(self, *, signal):
1139+
def _killpg(self, signal):
11411140
"""
11421141
similar to killpg but use psutil if it can on windows
11431142
or if pgid is none
@@ -1147,8 +1146,8 @@ async def _killpg(self, *, signal):
11471146
if pgid and hasattr(os, "killpg"):
11481147
try:
11491148
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.")
11521151
return
11531152
elif psutil is not None:
11541153
children = parent.children(recursive=True)
@@ -1162,30 +1161,20 @@ async def _killpg(self, *, signal):
11621161
pass
11631162

11641163
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
11681164

11691165
pgid = os.getpgid(os.getpid())
1170-
if not pgid:
1171-
self.log.warning(f"No Pgid ({pgid}), not trying to stop subprocesses.")
1172-
return
11731166
if psutil is None:
11741167
# 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.")
11761169
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)
11891178
children = psutil.Process().children(recursive=True)
11901179
if not children:
11911180
self.log.debug("Kernel has no children.")
@@ -1195,24 +1184,38 @@ async def _progressively_terminate_all_children(self):
11951184

11961185
for signum in (SIGTERM, SIGKILL):
11971186
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}"
11991188
)
12001189
for delay in sleeps:
12011190
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)
12061200
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}"
12081202
)
1209-
await ascynio.sleep(delay)
1203+
await asyncio.sleep(delay)
12101204

12111205
async def _at_shutdown(self):
12121206
"""Actions taken at shutdown by the kernel, called by python's atexit.
12131207
"""
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

Comments
 (0)