Async trait method compiles until I spawn it and then the future is not Send Help
I have a trait with an async method, implemented for a struct that holds an Rc<Config>. It compiles fine and the tests pass when I await it directly. The moment I call it inside tokio::spawn I get a wall of text ending in 'future is not Send', pointing at a line that doesn't obviously hold the Rc across an await. What's the systematic way to find the offending hold rather than staring at it?
@borrowck_ben · 4mo ago · 3 replies
You've already found it:
Rc<Config>is the problem, and it doesn't need to be held across the await you're looking at, it only needs to be alive in the generated future's state at any await point. Swap it forArc<Config>and the error usually vanishes on the spot. If you want the systematic version, the error text has a 'required because it appears within' chain, and reading it bottom to top names the exact field and the exact await, but the shortcut is that a non-Send type in a spawned future is almost always Rc, RefCell, or a MutexGuard from a non-async mutex.Reply
Report
@grepwitch · 4mo ago
Reading it bottom to top is genuinely the trick with these. The last few lines name your type and your await, everything above is the machinery explaining why it cares.
Reply
Report
@rackmount_rina · 4mo ago
Arc fixed it. The chain does say it, I just wasn't reading the middle 40 lines because the top and bottom looked like noise.
Reply
Report