| 3428 | } |
| 3429 | |
| 3430 | static void application_session_cancel(void *context, |
| 3431 | cbm_daemon_runtime_application_session_t *opaque_session) { |
| 3432 | cbm_daemon_application_t *application = context; |
| 3433 | cbm_daemon_application_session_t *session = (cbm_daemon_application_session_t *)opaque_session; |
| 3434 | if (application && session && session->application == application) { |
| 3435 | bool reap_update = false; |
| 3436 | cbm_daemon_application_job_t *join_auto_index = NULL; |
| 3437 | cbm_mutex_lock(&application->mutex); |
| 3438 | /* Runtime may need to keep the session allocation alive until its |
| 3439 | * request callback joins. Cancellation, not the later close, is the |
| 3440 | * ownership boundary for watches and session-scoped index work. */ |
| 3441 | bool newly_cancelled = !session->session_cancelled; |
| 3442 | session->session_cancelled = true; |
| 3443 | if (session->request_active) { |
| 3444 | session->request_cancel_token = session->active_request_token; |
| 3445 | } |
| 3446 | if (session->active_job && session->active_job_subscribed) { |
| 3447 | cbm_daemon_application_job_t *job = session->active_job; |
| 3448 | session->active_job = NULL; |
| 3449 | session->active_job_subscribed = false; |
| 3450 | application_index_record_cut_short_locked(job); |
| 3451 | application_job_unsubscribe_locked(job); |
| 3452 | } |
| 3453 | join_auto_index = application_auto_index_release_locked(session); |
| 3454 | reap_update = application_update_owner_release_locked(session); |
| 3455 | reap_update = |
| 3456 | reap_update || (session->background_eligible && application->update_thread_started && |
| 3457 | application->update_owners == 0); |
| 3458 | application_release_session_watch_locked(session); |
| 3459 | bool final_live_session = newly_cancelled; |
| 3460 | for (cbm_daemon_application_session_t *other = application->sessions; |
| 3461 | final_live_session && other; other = other->next) { |
| 3462 | if (other != session && !other->session_cancelled) { |
| 3463 | final_live_session = false; |
| 3464 | } |
| 3465 | } |
| 3466 | if (final_live_session && !application->permanent) { |
| 3467 | /* The daemon runtime cannot close this allocation until an |
| 3468 | * in-flight callback joins. Stop new admission now and cancel |
| 3469 | * watcher/UI jobs that are not owned by session->active_job. |
| 3470 | * A PERMANENT generation deliberately outlives its sessions and |
| 3471 | * keeps admitting new ones; only the stop/drain paths latch it. */ |
| 3472 | application->stopping = true; |
| 3473 | application_cancel_jobs_locked(application); |
| 3474 | application_update_cancel_locked(application); |
| 3475 | reap_update = reap_update || application->update_thread_started; |
| 3476 | } |
| 3477 | cbm_mutex_unlock(&application->mutex); |
| 3478 | (void)cbm_mcp_server_cancel_active(session->mcp); |
| 3479 | application_auto_index_cancel_join(application, join_auto_index); |
| 3480 | application_jobs_reap_completed(application); |
| 3481 | if (reap_update) { |
| 3482 | if (!application_update_reap(application, true, APPLICATION_BACKGROUND_REAP_MS)) { |
| 3483 | application_cleanup_force_terminate("update_cleanup"); |
| 3484 | } |
| 3485 | } |
| 3486 | } |
| 3487 | } |
nothing calls this directly
no test coverage detected