# How to lock().await a futures::lock⁠::Mutex on non async code

**URL:** <https://users.rust-lang.org/t/how-to-lock-await-a-futures-lock-mutex-on-non-async-code/59726>\
**Category:** help\
**Created:** [May 12, 2021, 9:10pm UTC](https://users.rust-lang.org/t/how-to-lock-await-a-futures-lock-mutex-on-non-async-code/59726 "2021-05-12T21:10:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![guerlando](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@guerlando](https://users.rust-lang.org/u/guerlando)\
**Post date:** [May 12, 2021, 9:10pm UTC](https://users.rust-lang.org/t/how-to-lock-await-a-futures-lock-mutex-on-non-async-code/59726/1 "2021-05-12T21:10:21Z")

</div>

```
    let vpn_client: Arc<futures::lock::Mutex<dyn VpnClient>> = ...

    let on_virtual_tun_write = Arc::new(
        move |f: &mut dyn FnMut(&mut [u8]),size: usize| -> std::result::Result<(), VirtualTunWriteError> {
            let mut buffer = vec![0; size];
            f(buffer.as_mut_slice());
            match vpn_client_.lock().await.send(buffer.as_slice())
            //...

```

As you can see in the example above, I have an `Arc<futures::lock::Mutex<dyn VpnClient>>`. I use it for other things in async code, but there's a library that which accepts `on_virtual_tun_write` which is not async, , so I cannot `lock()` this mutex inside of this callback.

How could I `lock().await` this mutex outside of some `async` code? And is this a good solution? I cannot use non async Mutex because of the rest of the code.

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [May 12, 2021, 9:15pm UTC](https://users.rust-lang.org/t/how-to-lock-await-a-futures-lock-mutex-on-non-async-code/59726/2 "2021-05-12T21:15:12Z")

</div>

It depends on where the callback is called. If the callback is called from inside the async context, then you can't, period. If it's outside the async context, you can use `block_on` on the `lock` call.

Here async context means any function that has an async fn anywhere above it in the callstack.

---

<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:** [August 10, 2021, 9:15pm UTC](https://users.rust-lang.org/t/how-to-lock-await-a-futures-lock-mutex-on-non-async-code/59726/3 "2021-08-10T21:15:50Z")

</div>

Closed on August 10, 2021, 9:15pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
