Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Different child termination behavior during Kernel shutdown on Posix and Windows systems

未关闭
#1,232 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
python

调研方向

从 ipykernel/kernelbase.py 的 1216-1227 行附近开始,跟踪 Kernel 关闭期间如何使用 _process_children()。比较 POSIX 进程组过滤与 Windows 子进程路径,并评估提议的实例级设置。当能够支持所需的自定义 Kernel 生命周期,同时保留现有默认行为时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

Hello,

I've developed a custom Kernel inheriting from the IPython Kernel. This Kernel initiates a child process, and I aim to manage this process' lifecycle independently of the Kernel's shutdown workflow. To achieve this independence, I start the child process as a new process group. This approach functions as expected in POSIX systems, as the IPython Kernel verifies if the kernel process group is distinct from the child process group. However, a divergence in behavior arises when attempting the same workflow on Windows. This discrepancy stems from the IPython Kernel's behavior of returning all children of the Kernel process and subsequently sending SIGTERM signals to them. Please refer:
https://github.com/ipython/ipykernel/blob/main/ipykernel/kernelbase.py#L1218:L1227 and https://github.com/ipython/ipykernel/blob/main/ipykernel/kernelbase.py#L1216:L1217

I understand that it is not straightforward to determine the process group equivalence in Windows OS without using lower-level win32 APIs but this difference in behavior is causing issues in my custom kernel workflows.

Can I achieve my requirements without having to make any changes to the IPython Kernel? My proposed solution involves introducing an instance-level variable in the base Kernel class, which, if set to False, ensures that the Kernel refrains from terminating child processes by default during Kernel shutdown.

Here's a potential implementation:

self.terminate_all_children: bool = True # Set to true by default to maintain backward-compatibility

This variable is then checked in the _process_children() method to determine whether to process the children or not.

kernel_process = psutil.Process()
all_children = kernel_process.children(recursive=True)
process_group_children = []
if not self.terminate_all_children:
     return process_group_children
else:
    if os.name == "nt":
        return all_children
    kernel_pgid = os.getpgrp()
    
    for child in all_children:
        try:
            child_pgid = os.getpgid(child.pid)
        except OSError:
            pass
        else:
            if child_pgid == kernel_pgid:
                process_group_children.append(child)
    return process_group_children

By setting terminate_all_children to False in the subclass, I ensure that the IPython Kernel doesn't terminate the child process by default when Kernel shutdown is initiated.

I'd appreciate your thoughts on this proposed solution. If it aligns with your expectations, I'm prepared to submit a PR. Thank you in advance!

主要语言
Python
星标
734
派生
411
平均合并
1 天 2 小时
30 天内合并 PR
9

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ipython/ipykernel 的其他 Issue

查看 ipython/ipykernel 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。