| 365 | } |
| 366 | |
| 367 | private onMessage(w: ParsePoolWorker, m: ParseWorkerMessage): void { |
| 368 | if (m.type === 'grammars-loaded') { |
| 369 | this.loadSettled(w); |
| 370 | if (!this.workers.has(w)) return; // recycled/destroyed before ready |
| 371 | this.pending.delete(w); |
| 372 | this.idle.push(w); |
| 373 | this.drain(); |
| 374 | return; |
| 375 | } |
| 376 | if (m.type === 'parse-result') { |
| 377 | const job = this.inflight.get(w); |
| 378 | if (!job || (m.id !== undefined && m.id !== job.id)) return; // stale (post-recycle) |
| 379 | this.inflight.delete(w); |
| 380 | this.parsesDone++; |
| 381 | if (typeof m.parseMs === 'number' && this.parseCounts.get(w) !== 1) { |
| 382 | this.parsesMeasured++; |
| 383 | this.parseMsMeasured += m.parseMs; |
| 384 | } |
| 385 | if (job.timerExpired) { |
| 386 | // The base timer fired before this result was processed. That almost |
| 387 | // always means the MAIN THREAD was stalled (sync SQLite store on slow |
| 388 | // disks) while the parse itself finished long ago — the worker's own |
| 389 | // clock (parseMs) tells the two apart. Either way the result is here |
| 390 | // and valid: accept it instead of the old behaviour (kill worker + |
| 391 | // reject), which turned every main-thread stall into false timeouts |
| 392 | // and dropped files (issue #1231). |
| 393 | const parseMs = typeof m.parseMs === 'number' ? Math.round(m.parseMs) : undefined; |
| 394 | const detail = parseMs === undefined |
| 395 | ? '' |
| 396 | : parseMs < (job.budgetMs ?? this.parseTimeoutMs) |
| 397 | ? ` (parse took ${parseMs}ms in-worker — the main thread was stalled, not the parse)` |
| 398 | : ` (parse genuinely took ${parseMs}ms)`; |
| 399 | this.log(`Late parse-result accepted: ${job.task.filePath}${detail}`); |
| 400 | } |
| 401 | // Recycle the worker once it's done enough parses to have grown its WASM |
| 402 | // heap; otherwise return it to the idle set for the next job. |
| 403 | if ((this.parseCounts.get(w) ?? 0) >= this.recycleInterval) { |
| 404 | this.recycle(w); |
| 405 | } else { |
| 406 | this.idle.push(w); |
| 407 | } |
| 408 | this.settle(job, m.result); |
| 409 | this.drain(); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /** A worker died (crash hook / OOM exit / spawn error). Reject its in-flight |
| 414 | * parse so the caller's retry pass can re-attempt it, then respawn. */ |