MotoreAdapter skips poll_ready, violating tower's Service contract for backpressure layers

Open
#57 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
64/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
backend

Research direction

Start at motore::service::MotoreAdapter behind the tower feature and read the tower Service docs on cloning inner services. Reproduce with the issue's ConcurrencyLimit snippet, then change call so the exact cloned tower service it dispatches to has poll_ready awaited first (for example via oneshot or a poll_fn). Done means the snippet prints Ok(1) instead of panicking, with a regression test using a backpressure layer such as ConcurrencyLimit.

Written by the indexing model from the issue text.

Description

Bug Report

Version

motore 0.4.1 (crates.io; also present on main @ 4ffc9998 / 0.4.3)
motore-macros 0.4.1
tower 0.4.13 (features = ["limit", "util"])

Platform

Linux 6.12.113+deb13-amd64 x86_64

Crates

motore(tower feature)、tower(limit / util feature)。

Description

MotoreAdapter(tower Service 包装成 motore Service)clone tower 服务后直接 call,没有对 clone 出来的那个服务调用 poll_ready

tower 的 Service 要求在 call 之前 poll_ready 必须返回 Ready,否则实现方允许 panic:

"Before dispatching a request, poll_ready must be called and return Poll::Ready(Ok(()))."

"Implementations are permitted to panic if call is invoked without obtaining Poll::Ready(Ok(())) from poll_ready."

https://docs.rs/tower-service/0.3.3/tower_service/trait.Service.html

这段代码会出问题

use std::convert::Infallible;

use motore::service::MotoreAdapter;
use motore::Service as MotoreService;
use tower::limit::ConcurrencyLimit;
use tower::service_fn;

fn main() {
    let tower_svc = ConcurrencyLimit::new(
        service_fn(|req: u64| async move { Ok::<_, Infallible>(req) }),
        1,
    );

    // tower::Service -> motore::Service
    let motore_svc = tower_svc.motore(|_cx: &mut (), req: u64| req);

    let mut cx = ();
    let res = futures::executor::block_on(motore_svc.call(&mut cx, 1));
    println!("{res:?}");
}

依赖:

motore = { version = "0.4.1", features = ["tower"] }
tower = { version = "0.4.13", features = ["limit", "util"] }
futures = "0.3"

期望看到:motore_svc.call 先对克隆出来的 tower 服务执行 poll_ready,然后 call,返回 Ok(1)。

实际看到:

thread 'main' panicked at tower-0.4.13/src/limit/concurrency/service.rs:87:14:
max requests in-flight; poll_ready must be called first

tower 的文档里有一段 "Be careful when cloning inner services" https://docs.rs/tower-service/latest/tower_service/trait.Service.html 说这种写法是 wrong and might panic 的。

    fn call(&mut self, req: R) -> Self::Future {
        let mut inner = self.inner.clone();
        Box::pin(async move {
            // `inner` might not be ready since its a clone
            inner.call(req).await
        })
    }

刚好和 MotoreAdapter 的一模一样!

Dominant language
Rust
Stars
282
Forks
31
Avg merge
3m
Merged PRs (30d)
1

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloudwego/motore

All issues in cloudwego/motore

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.