Skip to content

TaskContext::request_input leaves a request outstanding after its future is dropped #1579

Todo

Description

@felipecrs

A task operation that stops waiting for an answer, on its own timeout for example, can't take its question back. TaskContext::request_input (task_manager.rs, rmcp 3.12.1 and current main) inserts the request into the task's pending_inputs and awaits a oneshot. Nothing removes the entry when that future is dropped. So the task keeps reporting input_required, with a question nobody waits for, until the whole task ends, and the client keeps showing it to the user. An answer that arrives later goes to a dropped receiver.

let asked = ctx.request_input("ask-0", InputRequest::Elicitation(request));
match tokio::time::timeout(Duration::from_secs(300), asked).await {
    Ok(answer) => { /* … */ }
    // The task goes on without an answer, but `tasks/get` still lists `ask-0`
    // in `inputRequests`, and the status stays `input_required`.
    Err(_) => { /* … */ }
}

The spec leaves room for this. ext-tasks' tasks/update says each inputResponses key "MUST correspond to a currently-outstanding inputRequest key", so the set of outstanding requests is the server's current view, and the state machine has input_required → working.

Possible fix: remove the entry from pending_inputs when the request_input future is dropped before it resolves (a drop guard), and leave used_input_keys as it is so the key stays unique. The status then goes back to working once no other request is outstanding. An explicit TaskContext::withdraw_input(key) would do too, but the drop covers timeouts and select! without changes in callers.

Found in ssoxy-webtools, which asks the user through a task and gives up on the question after five minutes.

Activity

  1. github-actions commented on Dec 5, 2026

    @github-actions
    Contributor

    @connor4312 is on triage rotation this week and is the reviewer for this one.

    Automated classification did not run for this one, so it needs a full manual pass.

    Automated triage. Labels are a first pass and the assigned maintainer has the final say.

  2. jokemanfire commented on Dec 5, 2026

    @jokemanfire
    Member

    Confirmed on 37257723, and I agree with the diagnosis. Reading crates/rmcp/src/task_manager.rs:

    • TaskContext::request_input (:71-99) inserts into entry.pending_inputs and then only owns rx. The insert is never undone when the future is dropped: the only removals are update_task (:428), cancel_task (:467) and the terminal cleanup (:370, :525).
    • current_status() (:197-203) derives InputRequired purely from pending_inputs.is_empty(), and detailed() (:205-215) lists those keys, so timeout()/select! on the caller side leaves the task reporting input_required with a question nobody waits for.
    • The late-answer half is real too: update_task would remove the key and tx.send(value) into the dropped receiver (:428-430), and silently drops the error.

    A drop guard is the right shape and is small. Notes from the rest of the module:

    1. Late responses need no extra work. update_task already ignores unknown keys ("Unknown, already-answered, or superseded keys are ignored per spec", :414-415), so once the guard removes the key, a response that arrives afterwards is a no-op.
    2. Removing by key is safe. used_input_keys makes a key belong to one request_input call for the life of the task, so the guard cannot remove somebody else's entry, and the lost race (answer delivered, then the future dropped) is a no-op because update_task removed the entry first.
    3. Do not write task.status in the guard. get_task (:409) and update_task (:434) both recompute it from current_status(), so dropping the guard restores working on the next read without touching the status field.
    4. Use lock()/lock().ok() in Drop, not expect(). Every other site panics on a poisoned lock, but panicking from a Drop during unwinding aborts the process.
    5. Keep used_input_keys as you propose, and say so in the request_input doc (:63-70): today it only says keys must be unique over the task's lifetime, which reads as "no reuse ever" without saying why a retry after a timeout needs a new key.

    Sketch:

    struct PendingInputGuard {
        inner: Arc<Mutex<TaskManagerInner>>,
        task_id: String,
        key: String,
    }
    
    impl Drop for PendingInputGuard {
        fn drop(&mut self) {
            let Ok(mut inner) = self.inner.lock() else { return };
            if let Some(entry) = inner.tasks.get_mut(&self.task_id)
                && entry.pending_inputs.remove(&self.key).is_some()
            {
                entry.touch();
            }
        }
    }

    created before rx.await, so it fires on the timeout, on select!, and on tasks/cancel (where it is a no-op because cancel_task already cleared the map).

    On the option set:

    • A. Drop guard (yours): covers timeouts and select! without changing callers; ~15 lines plus two tests. Cannot withdraw while still awaiting something else.
    • B. TaskContext::withdraw_input(key): needed only if the operation wants to retract a question and keep running (e.g. replace it with a different one) while the original future is still alive. Can be added later on top of A; A is not a substitute for it, but nobody has asked for it yet.
    • C. Expiry marker in tasks/get: no such state in the spec; an input_required task with a request nobody waits for is exactly the state you are removing. I would not add it.

    Two tests would pin it:

    1. timeout(10ms, ctx.request_input("ask-0", …)) → get_task reports working and an empty input_requests, while used_input_keys still rejects re-using ask-0.
    2. A late update_task(task_id, [("ask-0", value)]) after the timeout returns Ok and leaves the task working (this is the case that today silently feeds a dropped receiver).

    Client-side note: withdrawing makes the key disappear from inputRequests, which is the only signal a polling client gets; a client that caches the question has to reconcile on the next tasks/get. That is consistent with the tasks/update rule you quoted (every inputResponses key must name a currently-outstanding request), so it is fine, but it is worth one line in the request_input doc.

    Happy to review a PR that adds the guard and the two tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions