# Tokio current\_thread::Handle::spawn requires Send?

**URL:** <https://users.rust-lang.org/t/tokio-current-thread-spawn-requires-send/20174>\
**Category:** help\
**Created:** [September 5, 2018, 7:30pm UTC](https://users.rust-lang.org/t/tokio-current-thread-spawn-requires-send/20174 "2018-09-05T19:30:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdg](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mdg/32/1783_2.png) [@mdg](https://users.rust-lang.org/u/mdg)\
**Post date:** [September 5, 2018, 7:30pm UTC](https://users.rust-lang.org/t/tokio-current-thread-spawn-requires-send/20174/1 "2018-09-05T19:30:46Z")

</div>

I'm trying to work with tokio for some IO and I'm using tokio::runtime::current\_thread::Handle.  
But when I try to spawn a future, it requires Send, as indicated in the docs here:

> **[tokio::runtime::current\_thread::Handle - Rust](https://docs.rs/tokio/0.1.22/tokio/runtime/current_thread/struct.Handle.html)**
>
> API documentation for the Rust \`Handle\` struct in crate \`tokio\`.

I was imagining that the purpose of the current\_thread model was that I could get I/O without handling Send issues.  
Can anyone help me understand why this is the case?

Thanks!

---

<div class="post-metadata">

**Author:** ![vitalyd](https://avatars.discourse-cdn.com/v4/letter/v/edb3f5/32.png) [@vitalyd](https://users.rust-lang.org/u/vitalyd)\
**Post date:** [September 5, 2018, 8:52pm UTC](https://users.rust-lang.org/t/tokio-current-thread-spawn-requires-send/20174/2 "2018-09-05T20:52:19Z")

</div>

I think the intent behind `current_thread::Handle` is it allows you to spawn a future onto the associated runtime _from_ a different thread, which requires moving that future.

Is there a reason you can't use `current_thread::Runtime::spawn` (which doesn't require `Send`)? Are you trying to spawn a future from a place where you don't have access to the `current_thread::Runtime`?

One curious thing I noticed while looking at this is `current_thread::Handle::spawn` takes `&self`, whereas `current_thread::Runtime::spawn` takes `&mut self`. If one wanted to keep the `Runtime` shared using an `Rc` (so that its spawn could be called), it would need to be wrapped in a `RefCell` due to the `&mut self` requirement. I wonder why that difference vs `Handle::spawn` exists ...

---

<div class="post-metadata">

**Author:** ![mdg](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mdg/32/1783_2.png) [@mdg](https://users.rust-lang.org/u/mdg)\
**Post date:** [September 5, 2018, 9:20pm UTC](https://users.rust-lang.org/t/tokio-current-thread-spawn-requires-send/20174/3 "2018-09-05T21:20:54Z")

</div>

Oh thank you for the clarification. I was sure I saw that the `current_thread::Runtime` didn't need `Send`, but I had missed that the `Handle` and `Runtime` are different. I recall that there used to be the `Handle` and `Remote` type, but I guess they've consolidated into just the Handle that acts like a `Remote`.  
I'll look into whether I can use `Runtime::spawn()`. The hangup with that is that I'm waiting on `Runtime::block_on()` while I'm currently trying to call `Handle::spawn()` so I can't also borrow the `RefCell` to call `Runtime::spawn()`. I'll see if there's a way around that.  
Thanks for the tip @vitalyd!

---

<div class="post-metadata">

**Author:** ![vitalyd](https://avatars.discourse-cdn.com/v4/letter/v/edb3f5/32.png) [@vitalyd](https://users.rust-lang.org/u/vitalyd)\
**Post date:** [September 5, 2018, 9:30pm UTC](https://users.rust-lang.org/t/tokio-current-thread-spawn-requires-send/20174/4 "2018-09-05T21:30:00Z")

</div>

> [@mdg](#):
>
> The hangup with that is that I’m waiting on `Runtime::block_on()` while I’m currently trying to call `Handle::spawn()` so I can’t also borrow the `RefCell` to call `Runtime::spawn()` . I’ll see if there’s a way around that.

Yeah, I'm actually unsure of how you're supposed to do this with the current API (admittedly, I've not kept fully up-to-date on all the new tokio stuff). On the surface, it would seem like you'd want a `CurrentThreadHandle`, which is `!Send` and whose `spawn` doesn't require a future to be `Send`. But I may very well be missing something crucial.

cc @carllerche as he'll know one way or another

---

<div class="post-metadata">

**Author:** ![mdg](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mdg/32/1783_2.png) [@mdg](https://users.rust-lang.org/u/mdg)\
**Post date:** [September 6, 2018, 3:16am UTC](https://users.rust-lang.org/t/tokio-current-thread-spawn-requires-send/20174/5 "2018-09-06T03:16:47Z")

</div>

I traced it down a few layers and it appears that the `current_thread` types eventually depend on the same executor as the thread pool types which correctly require Send. Specifically the `CurrentThread` implements `tokio_executor::Executor::spawn`.  
[https://github.com/tokio-rs/tokio/blob/master/tokio-current-thread/src/lib.rs#L425-L433](https://github.com/tokio-rs/tokio/blob/master/tokio-current-thread/src/lib.rs#L425-L433)  
So the `current_thread` types are more like just a thread pool with only one thread.

---

<div class="post-metadata">

**Author:** ![vitalyd](https://avatars.discourse-cdn.com/v4/letter/v/edb3f5/32.png) [@vitalyd](https://users.rust-lang.org/u/vitalyd)\
**Post date:** [September 6, 2018, 11:22am UTC](https://users.rust-lang.org/t/tokio-current-thread-spawn-requires-send/20174/6 "2018-09-06T11:22:18Z")

</div>

Yeah, the `Executor` trait requires `Send` (and boxed futures for `spawn`!). This means you can’t (easily, with builtin API) abstract over Send vs !Send executors - only Send ones, which eliminates the current\_thread runtime.

But, if you’re using the concrete current\_thread types, it shouldn’t be an issue.

---

<div class="post-metadata">

**Author:** ![mdg](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mdg/32/1783_2.png) [@mdg](https://users.rust-lang.org/u/mdg)\
**Post date:** [September 6, 2018, 1:32pm UTC](https://users.rust-lang.org/t/tokio-current-thread-spawn-requires-send/20174/7 "2018-09-06T13:32:32Z")

</div>

ok, this helped. I switched to spawning the future with the `tokio_current_thread` module instead.  
It's now `tokio_current_thread::TaskExecutor::current().spawn_local(fut)` and that compiles fine.

Thanks @vitalyd!

---

<div class="post-metadata">

**Author:** ![vitalyd](https://avatars.discourse-cdn.com/v4/letter/v/edb3f5/32.png) [@vitalyd](https://users.rust-lang.org/u/vitalyd)\
**Post date:** [September 6, 2018, 1:34pm UTC](https://users.rust-lang.org/t/tokio-current-thread-spawn-requires-send/20174/8 "2018-09-06T13:34:08Z")

</div>

Ah, the "current thread" stuff was split out into its own `tokio_current_thread` crate - good to know!

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 12, 2023, 8:49am UTC](https://users.rust-lang.org/t/tokio-current-thread-spawn-requires-send/20174/9 "2023-01-12T08:49:31Z")

</div>


