MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / application_job_subscribe_locked

Function application_job_subscribe_locked

src/daemon/application.c:2004–2067  ·  view source on GitHub ↗

Caller holds application->mutex. Keeping watcher ownership validation and * this admission in the same critical section closes the unwatch race. */

Source from the content-addressed store, hash-verified

2002/* Caller holds application->mutex. Keeping watcher ownership validation and
2003 * this admission in the same critical section closes the unwatch race. */
2004static cbm_daemon_application_job_t *application_job_subscribe_locked(
2005 cbm_daemon_application_t *application, const char *project_key, const char *root_path,
2006 const char *args_json, application_job_subscribe_status_t *status_out) {
2007 *status_out = APPLICATION_JOB_SUBSCRIBE_UNAVAILABLE;
2008 if (application->stopping) {
2009 return NULL;
2010 }
2011 cbm_daemon_application_job_t *job =
2012 application_find_active_job_locked(application, project_key);
2013 if (job) {
2014 if (job->cancel_requested) {
2015 *status_out = APPLICATION_JOB_SUBSCRIBE_CANCELLING;
2016 return NULL;
2017 }
2018 if (!application_index_args_equal(job->args_json, args_json)) {
2019 *status_out = APPLICATION_JOB_SUBSCRIBE_OPTIONS_CONFLICT;
2020 return NULL;
2021 }
2022 job->subscribers++;
2023 *status_out = APPLICATION_JOB_SUBSCRIBE_OK;
2024 return job;
2025 }
2026
2027 if (application_active_job_count_locked(application) >= application->physical_job_limit) {
2028 char limit[32];
2029 (void)snprintf(limit, sizeof(limit), "%zu", application->physical_job_limit);
2030 cbm_log_warn("daemon.index.admission_busy", "limit", limit, "project", project_key);
2031 *status_out = APPLICATION_JOB_SUBSCRIBE_BUSY;
2032 return NULL;
2033 }
2034
2035 job = calloc(1, sizeof(*job));
2036 if (job) {
2037 job->project_key = strdup(project_key);
2038 job->root_path = strdup(root_path);
2039 job->args_json = strdup(args_json);
2040 }
2041 if (!job || !job->project_key || !job->root_path || !job->args_json) {
2042 application_job_free(job);
2043 *status_out = APPLICATION_JOB_SUBSCRIBE_ALLOCATION_FAILED;
2044 return NULL;
2045 }
2046 job->application = application;
2047 job->subscribers = 1;
2048 job->next = application->jobs;
2049 application->jobs = job;
2050 if (application_job_thread_create(&job->thread, job) == 0) {
2051 job->thread_started = true;
2052 /* Still under the mutex, so this precedes the thread's publish. */
2053 application_index_record_begin_locked(job);
2054 } else {
2055 /* The job was linked only so a concurrently started thread could
2056 * observe its reservation. No thread exists on this path, so roll the
2057 * reservation back synchronously and let background callers retry. */
2058 application->jobs = job->next;
2059 job->next = NULL;
2060 application_job_free(job);
2061 *status_out = APPLICATION_JOB_SUBSCRIBE_UNAVAILABLE;

Tested by

no test coverage detected