-
Notifications
You must be signed in to change notification settings - Fork 252
fix(pm2): 修复大注册表读取截断 #1026
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
zjdznl
wants to merge
3
commits into
deepcoldy:master
Choose a base branch
from
zjdznl:fix/pm2-jlist-stdout-flush
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix(pm2): 修复大注册表读取截断 #1026
Changes from all commits
Commits
Show all changes
3 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
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,16 @@ | ||
| /** A minimal writable surface whose completion callback means the chunk has | ||
| * reached the underlying stream. `write()` returning false is only a | ||
| * backpressure signal; callers that must not exit early must await callback. */ | ||
| export interface CompletionWritable { | ||
| write(chunk: string, callback: (error?: Error | null) => void): boolean; | ||
| } | ||
|
|
||
| /** Write one complete payload before allowing a child process to exit. */ | ||
| export function writeAndFlush(stream: CompletionWritable, chunk: string): Promise<void> { | ||
| return new Promise((resolve, reject) => { | ||
| stream.write(chunk, error => { | ||
| if (error) reject(error); | ||
| else resolve(); | ||
| }); | ||
| }); | ||
| } |
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,58 @@ | ||
| import { spawnSync } from 'node:child_process'; | ||
| import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { dirname, join } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
| import { captureReadonlyPm2Jlist } from '../src/cli/pm2-readonly.js'; | ||
|
|
||
| const PKG_ROOT = dirname(dirname(fileURLToPath(import.meta.url))); | ||
| const PM2_PATH = join(PKG_ROOT, 'node_modules', 'pm2', 'bin', 'pm2'); | ||
| const homes: string[] = []; | ||
|
|
||
| function tempHome(): string { | ||
| const home = mkdtempSync(join(tmpdir(), 'botmux-pm2-readonly-')); | ||
| homes.push(home); | ||
| return home; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| for (const home of homes.splice(0)) { | ||
| const pm2Home = join(home, '.botmux', 'pm2'); | ||
| spawnSync(process.execPath, [PM2_PATH, 'kill'], { | ||
| env: { ...process.env, PM2_HOME: pm2Home }, | ||
| stdio: 'ignore', | ||
| timeout: 10_000, | ||
| }); | ||
| rmSync(home, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| describe('PM2 read-only jlist stdout integrity', () => { | ||
| it('returns a large jlist without truncation', () => { | ||
| const home = tempHome(); | ||
| const pm2Home = join(home, '.botmux', 'pm2'); | ||
| mkdirSync(pm2Home, { recursive: true }); | ||
| expect(spawnSync(process.execPath, [PM2_PATH, 'status'], { | ||
| env: { ...process.env, PM2_HOME: pm2Home }, | ||
| stdio: 'ignore', | ||
| timeout: 10_000, | ||
| }).status).toBe(0); | ||
|
|
||
| const idleScript = join(home, 'idle.js'); | ||
| writeFileSync(idleScript, 'setInterval(() => {}, 1000);\n'); | ||
| for (let i = 0; i < 6; i++) { | ||
| const started = spawnSync(process.execPath, [PM2_PATH, 'start', idleScript, '--name', `flush-probe-${i}`], { | ||
| env: { ...process.env, PM2_HOME: pm2Home, BOTMUX_FLUSH_PROBE: 'x'.repeat(30_000) }, | ||
| stdio: 'ignore', | ||
| timeout: 20_000, | ||
| }); | ||
| expect(started.status).toBe(0); | ||
| } | ||
|
|
||
| const stdout = captureReadonlyPm2Jlist({ pkgRoot: PKG_ROOT, home: pm2Home }); | ||
| // Keep the fixture well past the partial-flush range observed with a pipe. | ||
| expect(Buffer.byteLength(stdout)).toBeGreaterThan(200_000); | ||
| expect(JSON.parse(stdout)).toHaveLength(6); | ||
| }, 90_000); | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new regression test invokes PM2 directly with
spawnSync(process.execPath, ...)here and again forstatusandstart, bypassing the repository’s mandatory runtime-aware subprocess abstraction. This leaves the test and its teardown outside the supported Node/Bun launch contract, so they can fail or exercise different behavior when the suite runs under Bun; usespawnSyncTsScriptfromtest/helpers/ts-runner.tsfor all three invocations.AGENTS.md reference: AGENTS.md:L28-L28
Useful? React with 👍 / 👎.