Skip to main content

spawn_probe_task

Function spawn_probe_task 

Source
pub(super) async fn spawn_probe_task(
    state: Arc<AppState>,
    cfg_workers: usize,
    cfg_max_seq: usize,
    per_worker_workspace: usize,
    cgroup_limit_bytes: usize,
    cache_dir: PathBuf,
    model_variant_str: String,
    save_cache: bool,
)
Expand description

Spawns the background probe task with proper permit ownership.

ยงSerialisation protocol

  1. Set probe_status = Running.
  2. Acquire cfg_workers - 1 permits via acquire_many_owned โ€” combined with the 1 permit already reserved at startup, this drains the semaphore to 0 so all incoming /v1/embeddings* requests queue behind the gate while the probe is in flight.
  3. Move the [tokio::sync::OwnedSemaphorePermit] into the spawned task. Its destructor is invoked just before add_permits(cfg_workers) at the end of the task, restoring full traffic concurrency.

Rationale for acquire_many_owned: tokio::spawn returns synchronously before the spawned task starts executing. A permit bound to a local variable in the parent function would be dropped immediately at the end of that function โ€” before the probe begins โ€” leaving the semaphore un-drained and allowing real traffic to contaminate per-shape RSS measurements. acquire_many_owned returns an OwnedSemaphorePermit independent of the source Semaphore lifetime, so it survives the move into the async closure and is held for the full duration of the probe.