Skip to content

[python] <defunct> #3135

@lbyczzz

Description

@lbyczzz

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I used the zero-downtime restart solution. The new process was launched, while the old process became a defunct process. Tell me how to solve it? thank you

sanic 23.12
Python 3.11.10

Code snippet

import os
import signal
import sys
from sanic.worker.constants import RestartOrder

async def setup_restart_listener(app, loop):
# 只有主进程且非 Windows 系统才注册 SIGHUP
# hasattr(app, "m") 确保是在主管理进程中运行

if sys.platform == "win32":
    return print("Windows 模式启动:信号监听已跳过。")

def handle_restart(sig, frame):
    try:
        # 1. 尝试使用 app.m
        if hasattr(app, "m") and app.m:
            print(f"主进程 {os.getpid()} 收到信号,通过 Multiplexer 触发...")
            app.m.restart(zero_downtime=True)
        # 2. 检查是否存在 manager 实例 (这是最稳健的访问方式)
        elif hasattr(app, "manager") and app.manager:
            print(f"主进程 {os.getpid()} 收到信号,通过 Manager 触发零停机重启...")
            app.manager.restart(zero_downtime=True, restart_order=RestartOrder.STARTUP_FIRST)
        else:
            print("无法触发重启:Sanic Manager 尚未初始化完成")
    except Exception as e:
        print(f"触发重启失败: {str(e)}")

try:
    signal.signal(signal.SIGHUP, handle_restart)
    print(f"主进程 {os.getpid()} 已监听 SIGHUP (Zero-Downtime Mode)")
except Exception as e:
    print(f"信号注册失败: {e}")

Expected Behavior

No response

How do you run Sanic?

As a module

Operating System

Linux

Sanic Version

23.12

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions