| 503 | } |
| 504 | |
| 505 | static void *frontend_worker(void *opaque) { |
| 506 | frontend_state_t *state = opaque; |
| 507 | for (;;) { |
| 508 | frontend_item_t item = {0}; |
| 509 | if (!frontend_pop_begin(state, &item)) { |
| 510 | if (frontend_should_stop(state)) { |
| 511 | break; |
| 512 | } |
| 513 | #if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS |
| 514 | atomic_fetch_add_explicit(&frontend_test_worker_idle_count, 1, memory_order_release); |
| 515 | #endif |
| 516 | cbm_usleep(FRONTEND_IDLE_WAIT_US); |
| 517 | continue; |
| 518 | } |
| 519 | uint8_t *response = NULL; |
| 520 | uint32_t response_length = 0; |
| 521 | bool failed = false; |
| 522 | if (item.cancelled) { |
| 523 | failed = !frontend_write_cancelled_response(state->out, &item); |
| 524 | } else { |
| 525 | cbm_daemon_runtime_application_status_t status = |
| 526 | cbm_daemon_application_client_mcp_tagged(state->client, item.request_token, |
| 527 | item.message, &response, &response_length, |
| 528 | FRONTEND_REQUEST_TIMEOUT_MS); |
| 529 | if (status == CBM_DAEMON_RUNTIME_APPLICATION_CANCELLED) { |
| 530 | failed = !frontend_write_cancelled_response(state->out, &item); |
| 531 | } else { |
| 532 | failed = status != CBM_DAEMON_RUNTIME_APPLICATION_OK; |
| 533 | } |
| 534 | if (status == CBM_DAEMON_RUNTIME_APPLICATION_OK && !failed && response && |
| 535 | response_length > 0) { |
| 536 | failed = !frontend_write_response(state->out, response, response_length, |
| 537 | item.content_length_framed); |
| 538 | } |
| 539 | } |
| 540 | free(response); |
| 541 | bool expected_stop = failed && frontend_should_stop(state); |
| 542 | frontend_end_request(state, failed && !expected_stop); |
| 543 | frontend_item_free(&item); |
| 544 | atomic_fetch_add_explicit(&state->completed_items, 1, memory_order_release); |
| 545 | if (failed) { |
| 546 | if (!expected_stop) { |
| 547 | /* A failed daemon transport cannot wake a thread blocked in |
| 548 | * stdio portably. This frontend owns no state: immediate |
| 549 | * process exit closes the kernel IPC handle, which cancels |
| 550 | * daemon session ownership without a detached reader or an |
| 551 | * unsafe cross-thread fclose. Logging or flushing here could |
| 552 | * itself block on agent-owned stdout/stderr. */ |
| 553 | _Exit(EXIT_FAILURE); |
| 554 | } |
| 555 | break; |
| 556 | } |
| 557 | } |
| 558 | frontend_worker_mark_done(state); |
| 559 | return NULL; |
| 560 | } |
| 561 | |
| 562 | static bool frontend_enqueue(frontend_state_t *state, char *message, bool content_length_framed) { |
nothing calls this directly
no test coverage detected