Skip to content

Commit

Permalink
Fix possible OnceCell panic in Proxy::connection() (#186)
Browse files Browse the repository at this point in the history
* Fix possible OnceCell panic in Proxy::connection()

When multiple tasks are trying to create proxies simultaneously,
another task may initialize the zbus-Connection-OnceCell while
the connection is initialized.

This ensures that only the first task to establish a connection
initializes the OnceCell

* fix rust-fmt suggestion
  • Loading branch information
feschber authored Jan 4, 2024
1 parent 6d59aee commit 77bad1b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ impl<'a> Proxy<'a> {
Ok(cnx.clone())
} else {
let cnx = zbus::Connection::session().await?;
SESSION.set(cnx.clone()).expect("Can't reset a OnceCell");
Ok(cnx)
// during `await` another task may have initialized the cell
Ok(SESSION.get_or_init(|| cnx).clone())
}
}

Expand Down

0 comments on commit 77bad1b

Please sign in to comment.