Skip to content

Commit

Permalink
Fix reuse of websocket
Browse files Browse the repository at this point in the history
When `receive_messages` is called multiple times on the same manager,
this could lead to a panic "web socket request handler not in use". This
fixes this.

Requires <whisperfish/libsignal-service-rs#274>.
Also currently dependent on my fork of lss.
  • Loading branch information
Schmiddiii committed Dec 30, 2023
1 parent d884ef6 commit 221a79f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions presage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ authors = ["Gabriel Féron <[email protected]>"]
edition = "2021"

[dependencies]
libsignal-service = { git = "https://github.com/whisperfish/libsignal-service-rs", rev = "0a7987e" }
libsignal-service-hyper = { git = "https://github.com/whisperfish/libsignal-service-rs", rev = "0a7987e" }
libsignal-service = { git = "https://github.com/Schmiddiii/libsignal-service-rs", rev = "1561b7587aaf64d29e728b11c161ca89d34b7f4c" }
libsignal-service-hyper = { git = "https://github.com/Schmiddiii/libsignal-service-rs", rev = "1561b7587aaf64d29e728b11c161ca89d34b7f4c" }

base64 = "0.21"
futures = "0.3"
Expand Down
15 changes: 11 additions & 4 deletions presage/src/manager/registered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,16 @@ impl<S: Store> Manager<S, Registered> {
/// Returns the current identified websocket, or creates a new one
///
/// A new one is created if the current websocket is closed, or if there is none yet.
async fn identified_websocket(&self) -> Result<SignalWebSocket, Error<S::Error>> {
async fn identified_websocket(
&self,
require_unused: bool,
) -> Result<SignalWebSocket, Error<S::Error>> {
let mut identified_ws = self.state.identified_websocket.lock().await;
match identified_ws.as_ref().filter(|ws| !ws.is_closed()) {
match identified_ws
.as_ref()
.filter(|ws| !ws.is_closed())
.filter(|ws| !(require_unused && ws.is_used()))
{
Some(ws) => Ok(ws.clone()),
None => {
let headers = &[("X-Signal-Receive-Stories", "false")];
Expand Down Expand Up @@ -472,7 +479,7 @@ impl<S: Store> Manager<S, Registered> {
&mut self,
) -> Result<impl Stream<Item = Result<Incoming, ServiceError>>, Error<S::Error>> {
let credentials = self.credentials().ok_or(Error::NotYetRegisteredError)?;
let ws = self.identified_websocket().await?;
let ws = self.identified_websocket(true).await?;
let pipe = MessagePipe::from_socket(ws, credentials);
Ok(pipe.stream())
}
Expand Down Expand Up @@ -902,7 +909,7 @@ impl<S: Store> Manager<S, Registered> {
uuid: self.state.data.service_ids.aci,
};

let identified_websocket = self.identified_websocket().await?;
let identified_websocket = self.identified_websocket(false).await?;
let unidentified_websocket = self.unidentified_websocket().await?;

Ok(MessageSender::new(
Expand Down

0 comments on commit 221a79f

Please sign in to comment.