Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions code/core/src/common/utils/process-ancestry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,24 @@ describe('getProcessAncestry', () => {
expect(getProcessAncestry(9999)).toEqual([]);
});

it('breaks on cycles in the process tree', () => {
it('stops without warning on a self-referencing process on posix', () => {
platformMock.mockReturnValue('linux');
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
execSyncMock.mockReturnValue(' 5 5 looping-process \n');

getProcessAncestry(5);
expect(getProcessAncestry(5)).toEqual([{ pid: 5, ppid: 5, command: 'looping-process' }]);
});

expect(warn).toHaveBeenCalledWith('Detected cycle in process tree at PID 5');
warn.mockRestore();
it('stops without warning on cycles in the process tree', () => {
platformMock.mockReturnValue('win32');
execSyncMock.mockImplementation((command: string) =>
command.includes('ProcessId=10)')
? 'Node,CommandLine,ParentProcessId,ProcessId\r\nHOST,node child.js,20,10\r\n'
: 'Node,CommandLine,ParentProcessId,ProcessId\r\nHOST,cmd.exe,10,20\r\n'
);

expect(getProcessAncestry(10)).toEqual([
{ pid: 10, ppid: 20, command: 'node child.js' },
{ pid: 20, ppid: 10, command: 'cmd.exe' },
]);
});
});
2 changes: 1 addition & 1 deletion code/core/src/common/utils/process-ancestry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ function walkAncestry(
let maxDepth = MAX_DEPTH;

while (currentPid && maxDepth > 0) {
// Windows reuses PIDs without reparenting orphans, so a cycle here is expected.
if (visited.has(currentPid)) {
console.warn(`Detected cycle in process tree at PID ${currentPid}`);
break;
}
visited.add(currentPid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PackageManagerName,
resolveStorybookVersionSpecifier,
invalidateProjectRootCache,
getProcessAncestry,
} from 'storybook/internal/common';
import { logger, once } from 'storybook/internal/node-logger';
import { MinimumReleaseAgeHandledError } from 'storybook/internal/server-errors';
Expand Down Expand Up @@ -55,6 +56,7 @@ describe('PreflightCheckCommand', () => {
vi.clearAllMocks();
once.clear();
vi.mocked(resolveStorybookVersionSpecifier).mockReturnValue(undefined);
vi.mocked(getProcessAncestry).mockReturnValue([]);
});

afterAll(() => {
Expand Down
Loading