Skip to content

Make RouterBuilder::spawn non-async? #3299

@dpc

Description

@dpc

It is kind of annoying, that while setting up everything I already need to bring async in. Conceptually spawning a router should not need to block on anything and indeed, when looking at the source code of fn spawn, the only part that does .await:

        if let Err(err) = self.endpoint.set_alpns(alpns) {
            shutdown(&self.endpoint, protocols.clone()).await;
            return Err(err);
        }

It seems ... weird. Why do we need to block on anything on init error here, but we're fine in the success case? (If the spawn was succesful, and Router immediately drops, somehow everything shuts down OK without async-drop, or await).

In case you wonder why "async" is a problem. I need to pass some reference to my "App" to the protocols as a state, so it can interact with the app as a whole. But I need to put the resulting router somewhere ... likely ... in the "App" itself.

Typically I use Arc::new_cyclic for stuff like that, so one can have a single top-level App, that can pass a weak reference to all components. But new_cyclic is a blocking function, so I can't call spawn.await that easily. I have worked it around with OnceLock, but

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions