-
Notifications
You must be signed in to change notification settings - Fork 2
fix: query 超时保护 + 进程清理,防止 PM2 重启后残留孤儿进程 #24
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| module.exports = { | ||
| apps: [{ | ||
| name: 'feishu-claude', | ||
| script: 'dist/index.js', | ||
|
|
||
| // 给 shutdown handler 足够时间清理子进程(默认 1600ms 太短) | ||
| kill_timeout: 10000, | ||
|
|
||
| // 内存超限自动重启 | ||
| max_memory_restart: '1G', | ||
|
|
||
| env: { | ||
| NODE_ENV: 'production', | ||
| }, | ||
| }], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { execSync } from 'node:child_process'; | ||
| import { logger } from './logger.js'; | ||
|
|
||
| /** | ||
| * 启动时清理上一次残留的 Claude Code 子进程。 | ||
| * PM2 SIGKILL 或服务崩溃后,Agent SDK spawn 的子进程可能成为孤儿进程。 | ||
| */ | ||
| export function killOrphanedClaudeProcesses(): number { | ||
| const myPid = process.pid; | ||
| let killed = 0; | ||
|
|
||
| try { | ||
| // pgrep -fa claude: 列出命令行包含 "claude" 的进程 (PID + cmdline) | ||
| const output = execSync('pgrep -fa claude 2>/dev/null || true', { | ||
| encoding: 'utf-8', | ||
| timeout: 5000, | ||
| }).trim(); | ||
|
|
||
| if (!output) return 0; | ||
|
|
||
| for (const line of output.split('\n')) { | ||
| const match = line.match(/^(\d+)\s+(.*)$/); | ||
| if (!match) continue; | ||
|
|
||
| const pid = parseInt(match[1], 10); | ||
| const cmdline = match[2]; | ||
|
|
||
| // 跳过自身 | ||
| if (pid === myPid) continue; | ||
|
|
||
| // 只匹配 Claude Code CLI 进程(命令第一段以 claude 结尾) | ||
| // 例如: /usr/local/bin/claude --flags... 或 claude --flags... | ||
| const cmd = cmdline.split(/\s/)[0]; | ||
| const basename = cmd.split('/').pop(); | ||
| if (basename !== 'claude') continue; | ||
|
claude[bot] marked this conversation as resolved.
|
||
|
|
||
| // 仅清理真正的孤儿进程(PPID=1 表示父进程已退出,被 init 接管) | ||
| try { | ||
| const ppid = execSync(`ps -o ppid= -p ${pid} 2>/dev/null`, { encoding: 'utf-8' }).trim(); | ||
| if (ppid !== '1') continue; | ||
| } catch { | ||
| continue; // 无法获取 PPID,跳过 | ||
| } | ||
|
|
||
| try { | ||
| process.kill(pid, 'SIGTERM'); | ||
| killed++; | ||
| logger.info({ pid, cmdline: cmdline.slice(0, 120) }, 'Killed orphaned Claude process'); | ||
| } catch { | ||
| // 进程已退出 | ||
| } | ||
| } | ||
| } catch { | ||
| // pgrep 不可用或其他错误 — 非关键,跳过 | ||
| } | ||
|
|
||
| if (killed > 0) { | ||
| logger.info({ killed }, 'Cleaned up orphaned Claude processes on startup'); | ||
| } | ||
| return killed; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.