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
3 changes: 0 additions & 3 deletions gitnexus/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions gitnexus/src/core/ingestion/workers/worker-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,22 @@ export const createWorkerPool = (
if (!settled) {
settled = true;
cleanup();
activeWorkers--;
inFlightProgress[workerIndex] = 0;
const shouldContinue = requeueAfterTimeout(workerIndex, job, lastProgress);
if (!shouldContinue) return;
await replaceWorker(workerIndex);
if (!shouldContinue) {
activeWorkers--;
return;
}
try {
await replaceWorker(workerIndex);
} catch (err) {
void fail(
err instanceof Error ? err : new Error(`Worker replacement failed: ${err}`),
);
return;
} finally {
activeWorkers--;
}
reportProgress();
runWorker(workerIndex);
maybeDone();
Expand Down
59 changes: 58 additions & 1 deletion gitnexus/test/integration/worker-pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe('worker pool integration', () => {
}).toThrow(/Worker script not found/);
});

// ─── Unhappy paths ──────────────────────────────────────────────────
// --- Unhappy paths -----------------------------------------------------

it.skipIf(!hasDistWorker)('dispatch after terminate rejects', async () => {
const workerUrl = pathToFileURL(DIST_WORKER) as URL;
Expand Down Expand Up @@ -473,6 +473,63 @@ describe('worker pool integration', () => {
}
});

it('completes split-and-retry when the timed-out worker is the only active worker', async () => {
// Regression test for: the split-and-retry path resolving early when no other
// workers are active (activeWorkers === 0 during await replaceWorker).
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gitnexus-worker-sole-active-'));
const markerPath = path.join(tempDir, 'stalled-once.txt');
const workerPath = path.join(tempDir, 'worker.js');
fs.writeFileSync(
workerPath,
`
const fs = require('node:fs');
const { parentPort } = require('node:worker_threads');
const markerPath = ${JSON.stringify(markerPath)};
let current = [];
parentPort.on('message', (msg) => {
if (msg && msg.type === 'sub-batch') {
current = msg.files.map((file) => file.path);
if (current.length > 1 && !fs.existsSync(markerPath)) {
fs.writeFileSync(markerPath, 'stall once');
return;
}
parentPort.postMessage({ type: 'progress', filesProcessed: current.length });
parentPort.postMessage({ type: 'sub-batch-done' });
return;
}
if (msg && msg.type === 'flush') {
parentPort.postMessage({ type: 'result', data: { fileCount: current.length, paths: current } });
}
});
`,
);

const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
// 2 workers but subBatchSize=4 means all 4 items form 1 job; second worker stays idle.
pool = createWorkerPool(pathToFileURL(workerPath) as URL, 2, {
subBatchSize: 4,
subBatchIdleTimeoutMs: 300,
maxTimeoutRetries: 0,
timeoutBackoffFactor: 3,
});

try {
const results = await pool.dispatch<any, any>([
{ path: 'a.ts', content: '' },
{ path: 'b.ts', content: '' },
{ path: 'c.ts', content: '' },
{ path: 'd.ts', content: '' },
]);

const allPaths = results.flatMap((r: any) => r.paths);
expect(allPaths.sort()).toEqual(['a.ts', 'b.ts', 'c.ts', 'd.ts']);
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('Splitting into'));
} finally {
warnSpy.mockRestore();
fs.rmSync(tempDir, { recursive: true, force: true });
}
}, 15_000);

it('fails fast on a result message that violates the worker protocol', async () => {
const { tempDir, workerPath } = writeTempWorker(
'gitnexus-worker-protocol-',
Expand Down
Loading