Unable to lock mutex

client_store does not live long enough
borrowed value does not live long enough

It is not showing error when I remove the loop.

let guard = client_store.lock().await;
join_all(guard.keys().map(|x| {
        let db_cp: PgPool = db.clone();
        let ids = get_id.clone();
        spawn(async move {
            let _ = query_file!(


Hi. Since the code example you posted cannot be executed without the missing context, it’d probably be helpful if you provide the complete compiler error message you’re encountering, as it might contain more information.


The problem might be the usage of spawn, which requires the future being spawned to be free of short-lived references. On first look, I’m not sure I see the reason why spawn should be used here in the first place; could you just leave out the spawn and have the join_all handle those futures directly?

Thanks @steffahn
Cloning the x variable from closure & then move it to spawn did resolve the error.

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.