@@ -213,9 +213,42 @@ static const char *cbm_string_read(void *payload, uint32_t byte, TSPoint point,
213213
214214// --- Parse timeout callback ---
215215
216+ /* Budget for the tree-sitter progress callback. The PRIMARY gate is per-thread
217+ * CPU time: a worker descheduled under CI contention burns WALL time but not
218+ * CPU, so a starved-but-parseable file must NOT be abandoned merely because the
219+ * 5 s budget elapsed in wall-clock against near-zero CPU. That false "parse
220+ * timeout" silently dropped a file's defs and, with them, every cross-file edge
221+ * those defs anchored (e.g. a Celery.send_task definition backing an ASYNC_CALLS
222+ * edge). A generous WALL ceiling stays as a backstop so a genuinely
223+ * stuck/spinning parse still terminates in bounded time. */
224+ #define CBM_PARSE_WALL_CEILING_FACTOR 12ULL /* ~60 s ceiling for the 5 s CPU budget */
225+
226+ typedef struct {
227+ uint64_t cpu_deadline_ns ; // trip once this thread's CPU time passes it
228+ uint64_t wall_ceiling_ns ; // hard wall backstop for a spinning/stuck parse
229+ } CBMParseBudget ;
230+
231+ #ifdef CBM_ENABLE_TEST_SEAMS
232+ /* Deterministic RED-repro seam (armed by CBM_TEST_WALL_STALL_ON in
233+ * cbm_extract_file_ex): when set, the timeout callback reads the WALL clock this
234+ * many ns ahead of reality while CPU time is untouched — emulating a worker
235+ * descheduled long enough for the old wall-only budget to elapse against
236+ * near-zero CPU. With the CPU-time budget the parse still completes; a wall-only
237+ * / tight-ceiling budget drops the file. Thread-local so it cannot leak across
238+ * worker threads. Compiled only into seam-enabled test artifacts. */
239+ static CBM_TLS uint64_t tl_parse_wall_seam_offset_ns = 0 ;
240+ #endif
241+
242+ /* tree-sitter's TSProgressCallback mandates a non-const TSParseState*; a const
243+ * parameter here would not match the opts.progress_callback assignment below. */
244+ // cppcheck-suppress constParameterCallback
216245static bool cbm_timeout_cb (TSParseState * state ) {
217- uint64_t deadline = * (uint64_t * )state -> payload ;
218- return now_ns () > deadline ;
246+ const CBMParseBudget * budget = (const CBMParseBudget * )state -> payload ;
247+ uint64_t wall = now_ns ();
248+ #ifdef CBM_ENABLE_TEST_SEAMS
249+ wall += tl_parse_wall_seam_offset_ns ;
250+ #endif
251+ return cbm_thread_cpu_time_ns () > budget -> cpu_deadline_ns || wall > budget -> wall_ceiling_ns ;
219252}
220253
221254// --- Thread-local parser pool ---
@@ -1620,11 +1653,26 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
16201653 };
16211654
16221655 TSParseOptions opts = {0 };
1623- uint64_t deadline_ns = 0 ; // cppcheck-suppress unreadVariable
1656+ CBMParseBudget budget = { 0 } ; // cppcheck-suppress unreadVariable
16241657 if (timeout_micros > 0 ) {
1625- deadline_ns = t0 + ((uint64_t )timeout_micros * USEC_TO_NSEC );
1626- opts .payload = & deadline_ns ;
1658+ uint64_t budget_ns = (uint64_t )timeout_micros * USEC_TO_NSEC ;
1659+ // Descheduling burns wall time but not CPU: gate on this thread's CPU
1660+ // time so a starved-but-parseable file is not abandoned, with a generous
1661+ // wall ceiling as a backstop against a genuinely spinning/stuck parse.
1662+ budget .cpu_deadline_ns = cbm_thread_cpu_time_ns () + budget_ns ;
1663+ budget .wall_ceiling_ns = t0 + budget_ns * CBM_PARSE_WALL_CEILING_FACTOR ;
1664+ opts .payload = & budget ;
16271665 opts .progress_callback = cbm_timeout_cb ;
1666+ #ifdef CBM_ENABLE_TEST_SEAMS
1667+ tl_parse_wall_seam_offset_ns = 0 ;
1668+ const char * stall_on = getenv ("CBM_TEST_WALL_STALL_ON" );
1669+ if (stall_on && stall_on [0 ] && rel_path && strstr (rel_path , stall_on )) {
1670+ // Push the wall reading past the 1x budget (the old wall-only budget
1671+ // trips) but well under the generous ceiling (the CPU-time budget
1672+ // survives): budget + 1 s, deterministic, no real timing involved.
1673+ tl_parse_wall_seam_offset_ns = budget_ns + NSEC_PER_SEC ;
1674+ }
1675+ #endif
16281676 }
16291677
16301678 TSTree * tree = ts_parser_parse_with_options (parser , NULL , ts_input , opts );
0 commit comments