-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix: prevent infinite PTY recreation loop on terminal natural exit #2015
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
luiggibcn
wants to merge
8
commits into
AndyMik90:develop
Choose a base branch
from
luiggibcn:fix/terminal-exit-pty-recreation-loop
base: develop
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f2e43ed
fix: prevent infinite PTY recreation loop on terminal natural exit
luiggibcn 73dbba9
fix: also skip PTY creation when terminal is not in store
luiggibcn 7e37824
test: tighten TerminalStatus type and assert status reset on recreation
luiggibcn 13f9c49
Asserting destroyTerminal is not invoked during the loop
luiggibcn 1654b4e
test: attach electronAPI to jsdom window instead of replacing it
luiggibcn fd228d5
test: remove unused getTerminal mock from usePtyProcess tests
luiggibcn f2c48fa
test: address CR comments on usePtyProcess tests
luiggibcn 362cc62
test(terminal): lock status reset call order with toHaveBeenNthCalled…
luiggibcn 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
167 changes: 167 additions & 0 deletions
167
apps/desktop/src/renderer/components/terminal/__tests__/usePtyProcess.test.ts
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,167 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
|
|
||
| /** | ||
| * Unit tests for usePtyProcess | ||
| * | ||
| * Covers the guard that prevents PTY creation when a terminal has exited | ||
| * naturally (status === 'exited'), which was causing an infinite | ||
| * mount → create PTY → unmount → mount loop via the TerminalGrid | ||
| * pendingCleanup grace-period mechanism. | ||
| */ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import { renderHook, act } from '@testing-library/react'; | ||
| import { useRef } from 'react'; | ||
| import { usePtyProcess } from '../usePtyProcess'; | ||
| import type { TerminalStatus } from '../../../stores/terminal-store'; | ||
|
|
||
| const mockCreateTerminal = vi.fn(); | ||
| const mockDestroyTerminal = vi.fn(); | ||
| const mockRestoreTerminalSession = vi.fn(); | ||
|
|
||
| Object.defineProperty(window, 'electronAPI', { | ||
| configurable: true, | ||
| writable: true, | ||
| value: { | ||
| createTerminal: mockCreateTerminal, | ||
| destroyTerminal: mockDestroyTerminal, | ||
| restoreTerminalSession: mockRestoreTerminalSession, | ||
| }, | ||
| }); | ||
|
|
||
| /** Mutable terminal state controlled per test */ | ||
| let mockTerminalStatus: TerminalStatus = 'idle'; | ||
| let mockIsRestored = false; | ||
|
|
||
| const mockSetTerminalStatus = vi.fn(); | ||
| const mockUpdateTerminal = vi.fn(); | ||
|
|
||
| vi.mock('../../../stores/terminal-store', () => ({ | ||
| useTerminalStore: Object.assign(vi.fn(), { | ||
| getState: () => ({ | ||
| terminals: [ | ||
| { | ||
| id: 'term-1', | ||
| status: mockTerminalStatus, | ||
| isRestored: mockIsRestored, | ||
| isCLIMode: false, | ||
| cwd: '/test', | ||
| title: 'Terminal 1', | ||
| claudeSessionId: undefined, | ||
| worktreeConfig: undefined, | ||
| createdAt: new Date('2024-01-01T00:00:00Z'), | ||
| }, | ||
| ], | ||
| setTerminalStatus: mockSetTerminalStatus, | ||
| updateTerminal: mockUpdateTerminal, | ||
| }), | ||
| }), | ||
| })); | ||
|
|
||
| const DEFAULT_OPTIONS = { | ||
| terminalId: 'term-1', | ||
| cwd: '/test', | ||
| projectPath: '/test', | ||
| cols: 80, | ||
| rows: 24, | ||
| skipCreation: false, | ||
| } as const; | ||
|
|
||
| describe('usePtyProcess — exited-terminal guard (#fix/terminal-exit-pty-recreation-loop)', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockTerminalStatus = 'idle'; | ||
| mockIsRestored = false; | ||
| mockCreateTerminal.mockResolvedValue({ success: true }); | ||
| mockDestroyTerminal.mockResolvedValue({ success: true }); | ||
| mockRestoreTerminalSession.mockResolvedValue({ success: true, data: { success: true } }); | ||
| }); | ||
|
|
||
| it('does not call createTerminal when terminal status is exited (natural exit)', async () => { | ||
| mockTerminalStatus = 'exited'; | ||
|
|
||
| await act(async () => { | ||
| renderHook(() => usePtyProcess(DEFAULT_OPTIONS)); | ||
| }); | ||
|
|
||
| expect(mockCreateTerminal).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not call createTerminal on repeated mounts when status remains exited', async () => { | ||
| mockTerminalStatus = 'exited'; | ||
|
|
||
| for (let i = 0; i < 3; i++) { | ||
| await act(async () => { | ||
| const { unmount } = renderHook(() => usePtyProcess(DEFAULT_OPTIONS)); | ||
| unmount(); | ||
| }); | ||
| } | ||
|
|
||
| expect(mockCreateTerminal).not.toHaveBeenCalled(); | ||
| expect(mockDestroyTerminal).not.toHaveBeenCalled(); | ||
| }); | ||
|
luiggibcn marked this conversation as resolved.
|
||
|
|
||
| it('allows PTY creation when status is exited but isRecreatingRef is true (worktree switch)', async () => { | ||
| mockTerminalStatus = 'exited'; | ||
|
|
||
| await act(async () => { | ||
| renderHook(() => { | ||
| const isRecreatingRef = useRef(true); | ||
| return usePtyProcess({ ...DEFAULT_OPTIONS, isRecreatingRef }); | ||
| }); | ||
| }); | ||
|
|
||
| expect(mockCreateTerminal).toHaveBeenCalledTimes(1); | ||
| // The recreation path must reset status from 'exited' → 'idle' BEFORE creating the PTY, | ||
| // otherwise the exited-guard above would short-circuit creation. | ||
| expect(mockSetTerminalStatus).toHaveBeenNthCalledWith(1, 'term-1', 'idle'); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| it('calls createTerminal when terminal status is idle', async () => { | ||
| mockTerminalStatus = 'idle'; | ||
|
|
||
| await act(async () => { | ||
| renderHook(() => usePtyProcess(DEFAULT_OPTIONS)); | ||
| }); | ||
|
|
||
| expect(mockCreateTerminal).toHaveBeenCalledTimes(1); | ||
| expect(mockCreateTerminal).toHaveBeenCalledWith( | ||
| expect.objectContaining({ id: 'term-1', cwd: '/test', cols: 80, rows: 24 }), | ||
| ); | ||
| }); | ||
|
|
||
| it('calls createTerminal (new-terminal branch) when status is running and isRestored is false', async () => { | ||
| mockTerminalStatus = 'running'; | ||
|
|
||
| await act(async () => { | ||
| renderHook(() => usePtyProcess(DEFAULT_OPTIONS)); | ||
| }); | ||
|
|
||
| expect(mockCreateTerminal).toHaveBeenCalledTimes(1); | ||
| expect(mockRestoreTerminalSession).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('calls restoreTerminalSession (restore branch) when status is running and isRestored is true', async () => { | ||
| mockTerminalStatus = 'running'; | ||
| mockIsRestored = true; | ||
|
|
||
| await act(async () => { | ||
| renderHook(() => usePtyProcess(DEFAULT_OPTIONS)); | ||
| }); | ||
|
|
||
| expect(mockRestoreTerminalSession).toHaveBeenCalledTimes(1); | ||
| expect(mockCreateTerminal).not.toHaveBeenCalled(); | ||
| expect(mockUpdateTerminal).toHaveBeenCalledWith('term-1', { isRestored: false }); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| it('does not call createTerminal when skipCreation is true', async () => { | ||
| mockTerminalStatus = 'idle'; | ||
|
|
||
| await act(async () => { | ||
| renderHook(() => usePtyProcess({ ...DEFAULT_OPTIONS, skipCreation: true })); | ||
| }); | ||
|
|
||
| expect(mockCreateTerminal).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
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
Oops, something went wrong.
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.