# Back to async task from ffi

**URL:** <https://users.rust-lang.org/t/back-to-async-task-from-ffi/39116>\
**Category:** uncategorized\
**Created:** [March 5, 2020, 6:09pm UTC](https://users.rust-lang.org/t/back-to-async-task-from-ffi/39116 "2020-03-05T18:09:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johalun](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/johalun/32/3578_2.png) [@johalun](https://users.rust-lang.org/u/johalun)\
**Post date:** [March 5, 2020, 6:09pm UTC](https://users.rust-lang.org/t/back-to-async-task-from-ffi/39116/1 "2020-03-05T18:09:27Z")

</div>

Hi!

I'm calling into C code from a LocalPool executor. Then in the C code I can also call back into Rust code (still running in the same async task). Can I somehow convince the compiler that I'm still in the async task context so I can do .await when calling back into Rust code?

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [March 5, 2020, 7:19pm UTC](https://users.rust-lang.org/t/back-to-async-task-from-ffi/39116/2 "2020-03-05T19:19:54Z")

</div>

You can't do that directly, because `async` code that can use `.await` is not a regular code that "runs", but a state machine built by the compiler and expressed as a `Future` object. C has no idea what that is, so it won't transform itself into a compatible kind of re-entrant state machine.

If C only calls into async Rust, but _doesn't need to see the result or wait for it to run_, then it can make the call. You will have to wrap the result of the call in `BoxFuture`, which you give back to C, and which C will give back to the async Rust function which called it, and then the async function can `.await` it.

If you can change the C code to be split into two parts: before the async call, and after the async call, then you can make C return control back to Rust, have Rust wait for whatever async operation you need, and have Rust call C again with the result.

If C expects to call synchronous blocking callback, then you have no choice but to make Rust code it calls synchronous blocking code. You may need to spawn the call to C on a separate thread with `spawn_blocking` (so that C can block the thread while waiting), and then in Rust callback use things like `handle.block_on(future)` to wait for an async operation.

---

<div class="post-metadata">

**Author:** ![johalun](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/johalun/32/3578_2.png) [@johalun](https://users.rust-lang.org/u/johalun)\
**Post date:** [March 5, 2020, 7:30pm UTC](https://users.rust-lang.org/t/back-to-async-task-from-ffi/39116/3 "2020-03-05T19:30:53Z")

</div>

Thanks for an excellent answer! Not sure which approach would be best. I have to noodle on it.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [June 3, 2020, 7:45pm UTC](https://users.rust-lang.org/t/back-to-async-task-from-ffi/39116/4 "2020-06-03T19:45:23Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
