The synchronous contract: wait for the whole index (queueing behind the * physical job limit); a cancelled request drops only its own reference. */
| 2639 | /* The synchronous contract: wait for the whole index (queueing behind the |
| 2640 | * physical job limit); a cancelled request drops only its own reference. */ |
| 2641 | static char *application_index_execute_sync(cbm_daemon_application_session_t *session, |
| 2642 | const char *root_path, const char *args_json) { |
| 2643 | char *project_key = application_index_project_key(root_path, args_json); |
| 2644 | if (!project_key) { |
| 2645 | return cbm_mcp_text_result("failed to derive index project identity", true); |
| 2646 | } |
| 2647 | application_job_subscribe_status_t subscribe_status = APPLICATION_JOB_SUBSCRIBE_UNAVAILABLE; |
| 2648 | cbm_daemon_application_job_t *job = NULL; |
| 2649 | for (;;) { |
| 2650 | job = application_job_subscribe(session->application, project_key, root_path, args_json, |
| 2651 | &subscribe_status); |
| 2652 | if (job || (subscribe_status != APPLICATION_JOB_SUBSCRIBE_BUSY && |
| 2653 | subscribe_status != APPLICATION_JOB_SUBSCRIBE_CANCELLING)) { |
| 2654 | break; |
| 2655 | } |
| 2656 | /* Physical job limit reached (or a same-project cancel still |
| 2657 | * draining): QUEUE instead of surfacing a raw busy error. The |
| 2658 | * request thread blocks for the whole index anyway, so waiting for |
| 2659 | * a slot is the same contract — and the wait stays cancellable and |
| 2660 | * shutdown-aware (a stopping coordinator answers UNAVAILABLE, which |
| 2661 | * exits this loop with the error below). */ |
| 2662 | atomic_fetch_add_explicit(&g_application_busy_queue_waits_for_test, 1, |
| 2663 | memory_order_release); |
| 2664 | cbm_mutex_lock(&session->application->mutex); |
| 2665 | bool queued_cancelled = application_request_cancelled_locked(session); |
| 2666 | if (queued_cancelled) { |
| 2667 | application_index_record_mark_cut_short_locked(session->application, project_key); |
| 2668 | } |
| 2669 | cbm_mutex_unlock(&session->application->mutex); |
| 2670 | if (queued_cancelled) { |
| 2671 | free(project_key); |
| 2672 | return cbm_mcp_text_result( |
| 2673 | "index operation cancelled for this session. " CBM_MCP_INDEX_ASYNC_HINT, true); |
| 2674 | } |
| 2675 | cbm_usleep(APPLICATION_JOB_POLL_US); |
| 2676 | } |
| 2677 | free(project_key); |
| 2678 | if (!job) { |
| 2679 | return cbm_mcp_text_result(application_index_admission_error(subscribe_status), true); |
| 2680 | } |
| 2681 | cbm_mutex_lock(&session->application->mutex); |
| 2682 | if (application_request_cancelled_locked(session)) { |
| 2683 | application_index_record_cut_short_locked(job); |
| 2684 | application_job_unsubscribe_locked(job); |
| 2685 | cbm_mutex_unlock(&session->application->mutex); |
| 2686 | return cbm_mcp_text_result( |
| 2687 | "index operation cancelled for this session. " CBM_MCP_INDEX_ASYNC_HINT, true); |
| 2688 | } |
| 2689 | if (session->active_job) { |
| 2690 | application_job_unsubscribe_locked(job); |
| 2691 | cbm_mutex_unlock(&session->application->mutex); |
| 2692 | return cbm_mcp_text_result("this session already has an active index operation", true); |
| 2693 | } |
| 2694 | session->active_job = job; |
| 2695 | session->active_job_subscribed = true; |
| 2696 | cbm_mutex_unlock(&session->application->mutex); |
| 2697 | return application_job_wait_for_session(session, job); |
| 2698 | } |
no test coverage detected