mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-01-31 06:42:03 +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');
|
||||
};
|
||||
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
|
||||
|
||||
@@ -245,7 +245,7 @@ describe('subprocess.ts', () => {
|
||||
const mockProcess = createMockProcess({
|
||||
stdoutLines: ['{"type":"start"}'],
|
||||
exitCode: 0,
|
||||
delayMs: 100, // Delay to allow abort
|
||||
delayMs: 200, // Delay to allow abort
|
||||
});
|
||||
|
||||
vi.mocked(cp.spawn).mockReturnValue(mockProcess);
|
||||
@@ -258,8 +258,11 @@ describe('subprocess.ts', () => {
|
||||
// Start consuming the generator
|
||||
const promise = collectAsyncGenerator(generator);
|
||||
|
||||
// Abort after a short delay
|
||||
setTimeout(() => abortController.abort(), 20);
|
||||
// Abort after a short delay to ensure generator has started
|
||||
// Use setImmediate to ensure the generator has started processing
|
||||
setImmediate(() => {
|
||||
abortController.abort();
|
||||
});
|
||||
|
||||
await promise;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user