mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-02-03 21:03:08 +00:00
fix: improve abort handling in spawnJSONLProcess
- Added immediate invocation of abort handler if the abort signal is already triggered, ensuring proper cleanup. - Updated test to use setImmediate for aborting, allowing the generator to start processing before the abort is called, enhancing reliability.
This commit is contained in:
@@ -97,7 +97,12 @@ export async function* spawnJSONLProcess(options: SubprocessOptions): AsyncGener
|
|||||||
}
|
}
|
||||||
childProcess.kill('SIGTERM');
|
childProcess.kill('SIGTERM');
|
||||||
};
|
};
|
||||||
abortController.signal.addEventListener('abort', abortHandler);
|
// Check if already aborted, if so call handler immediately
|
||||||
|
if (abortController.signal.aborted) {
|
||||||
|
abortHandler();
|
||||||
|
} else {
|
||||||
|
abortController.signal.addEventListener('abort', abortHandler);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper to clean up abort listener
|
// Helper to clean up abort listener
|
||||||
|
|||||||
@@ -245,7 +245,7 @@ describe('subprocess.ts', () => {
|
|||||||
const mockProcess = createMockProcess({
|
const mockProcess = createMockProcess({
|
||||||
stdoutLines: ['{"type":"start"}'],
|
stdoutLines: ['{"type":"start"}'],
|
||||||
exitCode: 0,
|
exitCode: 0,
|
||||||
delayMs: 100, // Delay to allow abort
|
delayMs: 200, // Delay to allow abort
|
||||||
});
|
});
|
||||||
|
|
||||||
vi.mocked(cp.spawn).mockReturnValue(mockProcess);
|
vi.mocked(cp.spawn).mockReturnValue(mockProcess);
|
||||||
@@ -258,8 +258,11 @@ describe('subprocess.ts', () => {
|
|||||||
// Start consuming the generator
|
// Start consuming the generator
|
||||||
const promise = collectAsyncGenerator(generator);
|
const promise = collectAsyncGenerator(generator);
|
||||||
|
|
||||||
// Abort after a short delay
|
// Abort after a short delay to ensure generator has started
|
||||||
setTimeout(() => abortController.abort(), 20);
|
// Use setImmediate to ensure the generator has started processing
|
||||||
|
setImmediate(() => {
|
||||||
|
abortController.abort();
|
||||||
|
});
|
||||||
|
|
||||||
await promise;
|
await promise;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user