Skip to content

Fix/chrome not closing #510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

rainerWJY
Copy link
Collaborator

Describe what this PR does / why we need it

这个 PR 主要增强了 Chrome 进程的管理机制,通过添加多重保护措施来确保 Chrome 进程能够在应用程序退出时被正确清理。主要修改包括:

  1. 添加 JVM shutdown hook 机制
  2. 修改进程清理逻辑,确保能清理历史遗留进程
  3. 增强系统信号处理(SIGTERM, SIGINT)

Does this pull request fix one issue?

Fixes #NONE

Describe how you did it

  1. ChromeDriverService 构造函数中添加 JVM shutdown hook:
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
    log.info("JVM shutting down - cleaning up all Chrome processes");
    cleanupAllChromeProcesses();
}));
  1. 增强 cleanupAllChromeProcesses 方法:
private void cleanupAllChromeProcesses() {
    // 1. 先清理系统级别的 Chrome 进程
    String os = System.getProperty("os.name").toLowerCase();
    if (os.contains("mac")) {
        Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", 
            "pgrep 'chrome|chromedriver' | xargs kill -9 2>/dev/null || true"});
    }
    // ...其他系统的处理逻辑
}
  1. 添加系统信号处理:
sun.misc.Signal.handle(new sun.misc.Signal("TERM"), signal -> {
    log.info("Received TERM signal - cleaning up Chrome processes");
    cleanupAllChromeProcesses();
});

Describe how to verify it

  1. 运行应用程序并执行以下测试:

    • 通过 VS Code debug 模式启动应用
    • 执行一些浏览器操作
    • 点击停止按钮
    • 确认所有 Chrome 进程已被清理
  2. 验证命令:

# 检查是否有残留的 Chrome 进程
ps aux | grep chrome
ps aux | grep chromedriver

Special notes for reviews

  1. 特别注意 cleanupAllChromeProcesses 方法中的系统命令执行部分
  2. 考虑是否需要添加超时机制
  3. 建议在不同操作系统下测试进程清理功能

@yuluo-yx
Copy link
Collaborator

yuluo-yx commented May 9, 2025

此 pr 似乎已经过时了?如果不能合并,我会关闭此 pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants