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