Skip to content

Commit 8518d6f

Browse files
committed
add psutils on windows and try to kill children
1 parent 0ccc591 commit 8518d6f

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

ipykernel/kernelbase.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
if sys.platform != "win32":
2222
from signal import SIGKILL
2323
else:
24-
SIGKILL = None
24+
SIGKILL = "windown-SIGKILL-sentinel"
2525

2626

2727
try:
@@ -1136,6 +1136,31 @@ def _input_request(self, prompt, ident, parent, password=False):
11361136
raise EOFError
11371137
return value
11381138

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+
11391164
async def _progressively_terminate_all_children(self):
11401165
if sys.platform == "win32":
11411166
self.log.info(f"Terminating subprocesses not yet supported on windows.")
@@ -1152,10 +1177,10 @@ async def _progressively_terminate_all_children(self):
11521177
try:
11531178
await asyncio.sleep(0.05)
11541179
self.log.debug("Sending SIGTERM to {pgid}")
1155-
os.killpg(pgid, SIGTERM)
1180+
self._killpg(SIGTERM)
11561181
await asyncio.sleep(0.05)
11571182
self.log.debug("Sending SIGKILL to {pgid}")
1158-
os.killpg(pgid, SIGKILL)
1183+
self._killpg(pgid, SIGKILL)
11591184
except Exception:
11601185
self.log.exception("Exception during subprocesses termination")
11611186
return
@@ -1177,12 +1202,7 @@ async def _progressively_terminate_all_children(self):
11771202
if not children:
11781203
self.log.debug("No more children, continuing shutdown routine.")
11791204
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)
11861206
self.log.debug(
11871207
f"Will sleep {delay}s before checking for children and retrying."
11881208
)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def run(self):
6868
'tornado>=4.2,<7.0',
6969
'matplotlib-inline>=0.1.0,<0.2.0',
7070
'appnope;platform_system=="Darwin"',
71+
'psutil;platform_system=="Windows"',
7172
'nest_asyncio',
7273
],
7374
extras_require={

0 commit comments

Comments
 (0)