Skip to content

0.8: client ServerStream::message() future is not Send with concrete generated types (blocks consuming a server-stream in tokio::spawn) #214

Closed

Description

@christopherwxyz

Summary

In 0.8, the client server-stream API ServerStream::message() returns a future that is not Send once instantiated with a concrete generated message/view type — even though ServerStream, the response view, StreamMessage, and the view handle are all Send. This makes it impossible to consume a server-streaming client response inside a Send task (tokio::spawn), which is the normal way to drive a request off the accept path on a multi-threaded runtime.

Interestingly, the generic message() is Send — the failure only appears after monomorphizing with a concrete generated V::Owned/V, so the trigger is in the generated-type instantiation, not in caller code.

Reproduction (self-contained, generated code only)

No hand-written message types; a trivial server-streaming service compiled by connectrpc-build, consumed with the canonical stream.message().await pattern.

Cargo.toml:

[package]
name = "cr-send-repro-gen"
version = "0.0.0"
edition = "2021"

[dependencies]
connectrpc = { version = "0.8.0", features = ["client"] }
buffa = { version = "0.8", features = ["std", "json"] }
bytes = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

[build-dependencies]
connectrpc-build = "0.8.0"

proto/repro.proto:

syntax = "proto3";
package repro.v1;

message Ping { string note = 1; }

message Pong {
  string note = 1;
  oneof kind { Ping echo = 2; }
}

service ReproService {
  rpc Stream(Ping) returns (stream Pong);
}

build.rs:

fn main() -> Result<(), Box<dyn std::error::Error>> {
    connectrpc_build::Config::new()
        .files(&["proto/repro.proto"])
        .includes(&["proto/"])
        .generate_json(true)
        .include_file("_connectrpc.rs")
        .compile()?;
    Ok(())
}

src/lib.rs:

pub mod proto {
    connectrpc::include_generated!();
}

use proto::repro::v1::{Pong, PongView};

fn is_send<T: Send>() {}
fn snd<T: Send>(_: T) {}

pub fn repro<B>(mut stream: connectrpc::client::ServerStream<B, PongView<'static>>)
where
    B: connectrpc::http_body::Body<Data = bytes::Bytes> + Send + Unpin + 'static,
    B::Error: std::fmt::Display,
{
    // All the data types are `Send`:
    is_send::<connectrpc::client::ServerStream<B, PongView<'static>>>();
    is_send::<PongView<'static>>();
    is_send::<connectrpc::StreamMessage<Pong>>();
    is_send::<<Pong as connectrpc::HasMessageView>::ViewHandle>();

    // ...but the `message()` future is NOT `Send` — this line fails to compile:
    snd(async move {
        let _ = stream.message().await;
    });
}

cargo check fails only on the message().await block; the four is_send::<…>() assertions above it all pass.

Diagnostics

Stable rustc 1.95.0 and 1.96.1 (current trait solver):

error: implementation of `MessageView` is not general enough
  --> src/lib.rs:27:5
   |
27 | /     snd(async move {
28 | |         let _ = stream.message().await;
29 | |     });
   | |______^ implementation of `MessageView` is not general enough
   |
   = note: `MessageView<'static>` would have to be implemented for the type `PongView<'0>`, for any lifetime `'0`...
   = note: ...but `MessageView<'1>` is actually implemented for the type `PongView<'1>`, for some specific lifetime `'1`

Nightly with the next-generation trait solver (RUSTFLAGS="-Znext-solver=globally"), which reports it as a genuine non-Send rather than a lifetime-generality failure:

error[E0277]: `{coroutine witness@src/lib.rs:27:9: 27:19}` cannot be sent between threads safely
  --> src/lib.rs:27:5
   = help: within `{async block@…}`, the trait `Send` is not implemented for `{coroutine witness@…}`

What I narrowed down

  • ServerStream<B, PongView<'static>>, PongView<'static>, StreamMessage<Pong>, and <Pong as HasMessageView>::ViewHandle are all Send (the assertions pass).
  • A version of message() where the response view is a generic parameter bounded V: MessageView<'static> + Send / V::Owned: … + HasMessageView<View<'static> = V> + Send is Send — so the non-Send only appears when monomorphized against a concrete generated view. (I can share that generic variant too.)
  • The only awaits inside message()'s call graph (next_message_or_end → poll_body → with_deadline(… body.frame().await)) hold &mut self, and ServerStream is Send, so it isn't obvious from the source why the monomorphized future loses Send. It looks like an interaction between the message() bound RespView::Owned: HasMessageView<View<'static> = RespView> and auto-trait inference over the generated View<'a> GAT.

Impact

There is no other public API to pull messages off a client ServerStream, so a streaming client cannot be consumed inside tokio::spawn or any Send-bounded task with concrete generated types. The equivalent server-streaming client consumption compiled fine in a spawned Send context under 0.7.

Questions

  1. Is ServerStream::message() intended to be usable inside a Send task? If not, is there a supported pattern for driving a client server-stream off the accept path on a multi-threaded runtime?
  2. If it is intended, the concrete-monomorphization !Send looks like a regression / codegen-interaction bug — happy to test a patch or provide the generic-vs-concrete variants side by side.

Environment: connectrpc/connectrpc-build 0.8.0, buffa 0.8.1, rustc 1.95.0 / 1.96.1 (stable) and 1.96.0-nightly.

Activity

  1. christopherwxyz commented on Jul 2, 2026

    @christopherwxyz
    ContributorAuthor

    Checked how the repo's own examples/tests consume streams, since the asymmetry seems relevant to whether this is intended:

    • Client ServerStream (.message().await) is consumed inline on the task that opened it everywhere it appears — examples/streaming-tour/src/client.rs, examples/eliza/src/client.rs, tests/streaming/src/lib.rs. It's never moved into a tokio::spawn.
    • Server inbound ServiceStream<StreamMessage<…>> (.next().await), by contrast, is spawned into a background task in every bidi_stream handler, with an explicit comment that it's Send + 'static so it "can be moved into a spawned task and read zero-copy there" (tests/streaming/src/lib.rs).

    So the non-Send client future lines up with a codebase where client streams are only ever consumed inline. The question is whether that's intended (client ServerStream is inline-only by design) or an oversight given the server-side stream is deliberately Send-spawnable. The blocking use case is a client that spawns the stream consumer off the accept path (e.g. fast-ack a webhook, then drive the turn in a task) — which needs the client future to be Send, and no public API other than .message() exists to pull items.

  2. iainmcgin commented on Jul 2, 2026

    @iainmcgin
    Collaborator

    thanks for reporting, this is an important ergonomics issue and I'm investigating now

  3. christopherwxyz commented on Jul 2, 2026

    @christopherwxyz
    ContributorAuthor

    No problem!

    As well, happy to contribute a PR.

  4. iainmcgin commented on Jul 2, 2026

    @iainmcgin
    Collaborator

    [claude code] Confirmed and fixed in #215 — thank you for an outstanding report; the generic-vs-concrete isolation pointed directly at the cause. The 0.8.0 bound on message() projected RespView::Owned in the where-clause, which trips rustc's coroutine-witness auto-trait check on monomorphization (your nightly -Znext-solver diagnosis was exactly right). The fix reshapes it as an output type parameter — message<M>() where RespView: MessageView<'static, Owned = M>, M: HasMessageView<View<'static> = RespView> — which is equivalent for every caller (M is inferred) and restores Send; verified against your exact repro shape including the message-typed oneof, plus a live spawned consumer over server-stream/bidi/mid-stream-error paths. To answer your question 1 directly: yes, consuming a stream inside tokio::spawn is absolutely intended usage — this was a regression, not a design choice. It will ship as 0.8.1 shortly after #215 lands.

  5. iainmcgin commented on Jul 2, 2026

    @iainmcgin
    Collaborator

    Should be fixed with 0.8.1 - can you verify, @christopherwxyz ?

  6. iainmcgin commented on Jul 2, 2026

    @iainmcgin
    Collaborator

    [claude code] connectrpc 0.8.1 is on crates.io with the fix — a plain cargo update picks it up (the "0.8"/"0.8.0" requirements both resolve to it). Your repro pattern is also now a permanent compile-time regression pin in CI, asserted through the exact tokio::spawn-shaped coroutine where the failure manifests (interestingly, asserting Send on the bare message() future passes even with the buggy bound — the failure lives in the enclosing coroutine's witness, which your report's spawn-shaped repro captured exactly right). Thanks again for the isolation quality; it turned this around in about a day.

  7. christopherwxyz commented on Jul 2, 2026

    @christopherwxyz
    ContributorAuthor

    [claude code] connectrpc 0.8.1 is on crates.io with the fix — a plain cargo update picks it up (the "0.8"/"0.8.0" requirements both resolve to it). Your repro pattern is also now a permanent compile-time regression pin in CI, asserted through the exact tokio::spawn-shaped coroutine where the failure manifests (interestingly, asserting Send on the bare message() future passes even with the buggy bound — the failure lives in the enclosing coroutine's witness, which your report's spawn-shaped repro captured exactly right). Thanks again for the isolation quality; it turned this around in about a day.

    Yes! It worked.

    🫡

  8. added 2 commits that reference this issue on Jul 7, 2026
    fb64539
    0b9e25a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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