|
10 | 10 | import itertools |
11 | 11 | import logging |
12 | 12 | import inspect |
| 13 | +import os |
13 | 14 | from signal import signal, default_int_handler, SIGINT |
14 | 15 | import sys |
15 | 16 | import time |
@@ -198,7 +199,7 @@ def _parent_header(self): |
198 | 199 | 'inspect_request', 'history_request', |
199 | 200 | 'comm_info_request', 'kernel_info_request', |
200 | 201 | 'connect_request', 'shutdown_request', |
201 | | - 'is_complete_request', |
| 202 | + 'is_complete_request', 'interrupt_request', |
202 | 203 | # deprecated: |
203 | 204 | 'apply_request', |
204 | 205 | ] |
@@ -780,6 +781,30 @@ async def comm_info_request(self, stream, ident, parent): |
780 | 781 | reply_content, parent, ident) |
781 | 782 | self.log.debug("%s", msg) |
782 | 783 |
|
| 784 | + async def interrupt_request(self, stream, ident, parent): |
| 785 | + pid = os.getpid() |
| 786 | + pgid = os.getpgid(pid) |
| 787 | + |
| 788 | + if os.name == "nt": |
| 789 | + self.log.error("Interrupt message not supported on Windows") |
| 790 | + |
| 791 | + else: |
| 792 | + # Prefer process-group over process |
| 793 | + if pgid and hasattr(os, "killpg"): |
| 794 | + try: |
| 795 | + os.killpg(pgid, SIGINT) |
| 796 | + return |
| 797 | + except OSError: |
| 798 | + pass |
| 799 | + try: |
| 800 | + os.kill(pid, SIGINT) |
| 801 | + except OSError: |
| 802 | + pass |
| 803 | + |
| 804 | + content = parent['content'] |
| 805 | + self.session.send(stream, 'interrupt_reply', content, parent, ident=ident) |
| 806 | + return |
| 807 | + |
783 | 808 | async def shutdown_request(self, stream, ident, parent): |
784 | 809 | content = self.do_shutdown(parent['content']['restart']) |
785 | 810 | if inspect.isawaitable(content): |
|
0 commit comments