Repository navigation
fix(core): remediate subagent memory leaks using AbortSignal in MessageBus #25048
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ import { resolveConfirmation } from './confirmation.js'; | |
| import { checkPolicy, updatePolicy } from './policy.js'; | ||
| import { ToolExecutor } from './tool-executor.js'; | ||
| import { ToolModificationHandler } from './tool-modifier.js'; | ||
| import { MessageBusType, type Message } from '../confirmation-bus/types.js'; | ||
|
|
||
| vi.mock('./state-manager.js'); | ||
| vi.mock('./confirmation.js'); | ||
|
|
@@ -1299,6 +1300,64 @@ describe('Scheduler (Orchestrator)', () => { | |
| }); | ||
| }); | ||
|
|
||
| describe('Fallback Handlers', () => { | ||
| it('should respond to TOOL_CONFIRMATION_REQUEST with requiresUserConfirmation: true', async () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: what does this test have to do with the new functionality?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was added b/c we didn't have any coverage to make sure that the listeners were actually working. I can remove if needed
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fine to leave. just checking |
||
| const listeners: Record< | ||
| string, | ||
| Array<(message: Message) => void | Promise<void>> | ||
| > = {}; | ||
|
|
||
| const mockBus = { | ||
| subscribe: vi.fn( | ||
| ( | ||
| type: string, | ||
| handler: (message: Message) => void | Promise<void>, | ||
| ) => { | ||
| listeners[type] = listeners[type] || []; | ||
| listeners[type].push(handler); | ||
| }, | ||
| ), | ||
| publish: vi.fn(async (message: Message) => { | ||
| const type = message.type as string; | ||
| if (listeners[type]) { | ||
| for (const handler of listeners[type]) { | ||
| await handler(message); | ||
| } | ||
| } | ||
| }), | ||
| } as unknown as MessageBus; | ||
|
|
||
| const scheduler = new Scheduler({ | ||
| context: mockConfig, | ||
| messageBus: mockBus, | ||
| getPreferredEditor, | ||
| schedulerId: 'fallback-test', | ||
| }); | ||
|
|
||
| const handler = vi.fn(); | ||
| mockBus.subscribe(MessageBusType.TOOL_CONFIRMATION_RESPONSE, handler); | ||
|
|
||
| await mockBus.publish({ | ||
| type: MessageBusType.TOOL_CONFIRMATION_REQUEST, | ||
| correlationId: 'test-correlation-id', | ||
| toolCall: { name: 'test-tool' }, | ||
| }); | ||
|
|
||
| // Wait for async handler to fire | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| expect(handler).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| correlationId: 'test-correlation-id', | ||
| confirmed: false, | ||
| requiresUserConfirmation: true, | ||
| }), | ||
| ); | ||
|
|
||
| scheduler.dispose(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Cleanup', () => { | ||
| it('should unregister McpProgress listener on dispose()', () => { | ||
| const onSpy = vi.spyOn(coreEvents, 'on'); | ||
|
|
@@ -1323,6 +1382,40 @@ describe('Scheduler (Orchestrator)', () => { | |
| expect.any(Function), | ||
| ); | ||
| }); | ||
|
|
||
| it('should abort disposeController signal on dispose()', () => { | ||
| const mockSubscribe = | ||
| vi.fn< | ||
| ( | ||
| type: unknown, | ||
| listener: unknown, | ||
| options?: { signal?: AbortSignal }, | ||
| ) => void | ||
| >(); | ||
| const mockBus = { | ||
| subscribe: mockSubscribe, | ||
| publish: vi.fn(), | ||
| } as unknown as MessageBus; | ||
|
|
||
| let capturedSignal: AbortSignal | undefined; | ||
| mockSubscribe.mockImplementation((type, listener, options) => { | ||
| capturedSignal = options?.signal; | ||
| }); | ||
|
|
||
| const s = new Scheduler({ | ||
| context: mockConfig, | ||
| messageBus: mockBus, | ||
| getPreferredEditor, | ||
| schedulerId: 'cleanup-test-2', | ||
| }); | ||
|
|
||
| expect(capturedSignal).toBeDefined(); | ||
| expect(capturedSignal?.aborted).toBe(false); | ||
|
|
||
| s.dispose(); | ||
|
|
||
| expect(capturedSignal?.aborted).toBe(true); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.