@@ -359,6 +359,7 @@ typedef enum {
359359 WORKER_RESPONSE_READ_OK = 0 ,
360360 WORKER_RESPONSE_READ_ERROR ,
361361 WORKER_RESPONSE_READ_TOO_LARGE ,
362+ WORKER_RESPONSE_READ_EMPTY , /* the pre-created file was never written (#1300) */
362363} worker_response_read_status_t ;
363364
364365/* Read at most one daemon-application payload. The worker is already reaped,
@@ -384,6 +385,14 @@ static char *slurp_worker_response(const char *path, worker_response_read_status
384385 (void )fclose (f );
385386 return NULL ;
386387 }
388+ if (n == 0 ) {
389+ /* The supervisor creates the response file before spawning, so an
390+ * empty file is a worker that never wrote its result. Reading it as
391+ * "" made that a successful run with an empty response (#1300). */
392+ * status_out = WORKER_RESPONSE_READ_EMPTY ;
393+ (void )fclose (f );
394+ return NULL ;
395+ }
387396 if (fseek (f , 0 , SEEK_SET ) != 0 ) {
388397 (void )fclose (f );
389398 return NULL ;
@@ -405,6 +414,67 @@ static char *slurp_worker_response(const char *path, worker_response_read_status
405414 return buf ;
406415}
407416
417+ enum {
418+ WORKER_LOG_TAIL_BYTES = 64 * 1024 , /* last-phase scan window: bounded, never the whole log */
419+ WORKER_LOG_LINE_CAP = 1024 ,
420+ };
421+
422+ /* Copy the event name of one structured log line ("level=... msg=<event> ..."
423+ * or {"level":...,"event":"<event>",...}) into out. False for any other line
424+ * (plain stderr text, a truncated fragment). */
425+ static bool worker_log_line_event (const char * line , char * out , size_t out_size ) {
426+ static const char text_key [] = " msg=" ;
427+ static const char json_key [] = "\"event\":\"" ;
428+ const char * start = NULL ;
429+ char stop = ' ' ;
430+ if (strncmp (line , "level=" , strlen ("level=" )) == 0 ) {
431+ const char * msg = strstr (line , text_key );
432+ start = msg ? msg + strlen (text_key ) : NULL ;
433+ } else if (strncmp (line , "{\"level\":" , strlen ("{\"level\":" )) == 0 ) {
434+ const char * event = strstr (line , json_key );
435+ start = event ? event + strlen (json_key ) : NULL ;
436+ stop = '"' ;
437+ }
438+ if (!start ) {
439+ return false;
440+ }
441+ size_t length = 0 ;
442+ while (start [length ] && start [length ] != stop && start [length ] != '\n' &&
443+ start [length ] != '\r' ) {
444+ length ++ ;
445+ }
446+ if (length == 0 || length >= out_size ) {
447+ return false;
448+ }
449+ memcpy (out , start , length );
450+ out [length ] = '\0' ;
451+ return true;
452+ }
453+
454+ /* #1300: the last structured event the worker logged before it exited — the
455+ * phase it reached. Scans only the log's final WORKER_LOG_TAIL_BYTES. */
456+ static void worker_log_last_phase (const char * log_path , char * out , size_t out_size ) {
457+ (void )snprintf (out , out_size , "%s" , "unknown" );
458+ FILE * log = cbm_fopen (log_path , "rb" );
459+ if (!log ) {
460+ return ;
461+ }
462+ if (fseek (log , 0 , SEEK_END ) == 0 ) {
463+ long size = ftell (log );
464+ long from = size > WORKER_LOG_TAIL_BYTES ? size - WORKER_LOG_TAIL_BYTES : 0 ;
465+ if (size >= 0 && fseek (log , from , SEEK_SET ) == 0 ) {
466+ char line [WORKER_LOG_LINE_CAP ];
467+ char event [CBM_SZ_128 ];
468+ while (fgets (line , sizeof (line ), log )) {
469+ if (worker_log_line_event (line , event , sizeof (event ))) {
470+ (void )snprintf (out , out_size , "%s" , event );
471+ }
472+ }
473+ }
474+ }
475+ (void )fclose (log );
476+ }
477+
408478enum {
409479 INDEX_WORKER_PATH_CAP = CBM_SZ_4K ,
410480 INDEX_WORKER_ARGV_CAP = 17 ,
@@ -634,6 +704,10 @@ static void worker_terminal_log(cbm_index_worker_handle_t *handle) {
634704 } else if (handle -> result .cancellation_requested ) {
635705 cbm_log_warn ("index.supervisor.worker_cancelled" , "outcome" ,
636706 cbm_proc_outcome_str (handle -> result .outcome ), "log" , handle -> log_path );
707+ } else if (handle -> result .response_missing ) {
708+ /* #1300: keep and name the log — it is the only record of the run. */
709+ cbm_log_error ("index.supervisor.no_response" , "exit_code" , exit_text , "last_phase" ,
710+ handle -> result .last_phase , "log" , handle -> log_path );
637711 } else if (handle -> result .outcome == CBM_PROC_CLEAN && !cbm_profile_active ) {
638712 (void )cbm_unlink (handle -> log_path );
639713 } else if (handle -> result .outcome == CBM_PROC_CLEAN ) {
@@ -828,6 +902,16 @@ cbm_index_worker_poll_t cbm_index_worker_poll(cbm_index_worker_handle_t *handle,
828902 handle -> result .response_rejected = true;
829903 handle -> result .outcome = CBM_PROC_EXIT_NONZERO ;
830904 handle -> result .exit_code = -1 ;
905+ } else if (!handle -> result .response ) {
906+ /* #1300: a clean exit that wrote no response. The outcome stays
907+ * CLEAN (the disposition is FALLBACK: an explicit error, no
908+ * in-process fallback and no recovery re-run of a worker that did
909+ * not crash); the log and the last phase it reached are kept. */
910+ handle -> result .response_missing = true;
911+ worker_log_last_phase (handle -> log_path , handle -> result .last_phase ,
912+ sizeof (handle -> result .last_phase ));
913+ (void )snprintf (handle -> result .worker_log , sizeof (handle -> result .worker_log ), "%s" ,
914+ handle -> log_path );
831915 }
832916 }
833917 (void )cbm_unlink (handle -> response_path );
0 commit comments