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 Jan 4, 2024
1 parent d884ef6 commit 859e41d
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/whisperfish/libsignal-service-rs", rev = "9d55addebe010f0acbcabdfc02ab41979c1863e0" }
libsignal-service-hyper = { git = "https://github.com/whisperfish/libsignal-service-rs", rev = "9d55addebe010f0acbcabdfc02ab41979c1863e0" }

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 859e41d

Please sign in to comment.