Skip to content

fix(pool): Singleton shared calls pass baton if one cancels#299

Open
seanmonstar wants to merge 1 commit into
masterfrom
sean/skyttzrvqvsr
Open

fix(pool): Singleton shared calls pass baton if one cancels#299
seanmonstar wants to merge 1 commit into
masterfrom
sean/skyttzrvqvsr

Conversation

@seanmonstar

Copy link
Copy Markdown
Member

Previously, with a bunch of calls all funneled into a Singleton, the first call was the "driver", and the rest all waited for the driver to finish and send a clone to them. The downside is that if the first call canceled, all the subsequent waiters would just get a "canceled" error.

This changes to a baton-passing strategy. If the first future gets canceled, it passes the baton to keep driving the future to one of the other waiters. Only once all of them are canceled will it cancel the connection attempt.

cc @aajtodd

@aajtodd

aajtodd commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Pulled the branch and ran it against the maker-future-dropped case (driver's future dropped mid-make): the surviving waiter inherits the drive and connects, where master resolves Canceled. Also confirmed the existing UseOther-bounce cancellation from #4119 stays fixed. LGTM.

One additive coverage note: the existing cancellation tests promote a waiter that was registered but not yet polled (its stored waker is None). The path where an already-polled, parked waiter is promoted on driver drop, where the promotion carries a stored Waker, isn't exercised (I think).

  #[tokio::test]
  async fn cancel_driver_promotes_parked_waiter() {
      let (mock_svc, mut handle) = tower_test::mock::pair::<(), &'static str>();
      let mut singleton = Singleton::new(mock_svc);

      std::future::poll_fn(|cx| singleton.poll_ready(cx)).await.unwrap();

      let mut fut1 = singleton.call(()); // driver
      let mut fut2 = singleton.call(()); // waiter

      // Poll both once so the waiter parks with a stored waker (not just registered).
      std::future::poll_fn(|cx| {
          assert!(Pin::new(&mut fut1).poll(cx).is_pending());
          assert!(Pin::new(&mut fut2).poll(cx).is_pending());
          Poll::Ready(())
      })
      .await;

      drop(fut1); // promote the parked waiter to driver

      let ((), send_response) = handle.next_request().await.unwrap();
      send_response.send_response("svc");
      fut2.await.unwrap();
  }

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants