Skip to content
This repository was archived by the owner on Sep 4, 2024. It is now read-only.

Commit 36708d6

Browse files
committed
simple_http: tighten mutex lifetime
1 parent 7ea2199 commit 36708d6

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/simple_http.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,11 @@ impl SimpleHttpTransport {
7979
where
8080
R: for<'a> serde::de::Deserialize<'a>,
8181
{
82-
// `try_request` should not panic, so the mutex shouldn't be poisoned
83-
// and unwrapping should be safe
84-
let mut sock = self.sock.lock().expect("poisoned mutex");
85-
match self.try_request(req, &mut sock) {
82+
match self.try_request(req) {
8683
Ok(response) => Ok(response),
8784
Err(err) => {
88-
*sock = None;
85+
// No part of this codebase should panic, so unwrapping a mutex lock is fine
86+
*self.sock.lock().expect("poisoned mutex") = None;
8987
Err(err)
9088
}
9189
}
@@ -94,13 +92,14 @@ impl SimpleHttpTransport {
9492
fn try_request<R>(
9593
&self,
9694
req: impl serde::Serialize,
97-
sock: &mut Option<TcpStream>,
9895
) -> Result<R, Error>
9996
where
10097
R: for<'a> serde::de::Deserialize<'a>,
10198
{
10299
// Open connection
103100
let request_deadline = Instant::now() + self.timeout;
101+
// No part of this codebase should panic, so unwrapping a mutex lock is fine
102+
let mut sock = self.sock.lock().expect("poisoned mutex");
104103
if sock.is_none() {
105104
*sock = Some({
106105
#[cfg(feature = "proxy")]
@@ -127,6 +126,8 @@ impl SimpleHttpTransport {
127126
}
128127
})
129128
};
129+
// In the immediately preceding block, we made sure that `sock` is non-`None`,
130+
// so unwrapping here is fine.
130131
let sock = sock.as_mut().unwrap();
131132

132133
// Serialize the body first so we can set the Content-Length header.
@@ -196,6 +197,7 @@ impl SimpleHttpTransport {
196197
// Even if it's != 200, we parse the response as we may get a JSONRPC error instead
197198
// of the less meaningful HTTP error code.
198199
reader.read_exact(&mut buffer)?;
200+
drop(reader); // Unlock the mutex
199201
match serde_json::from_slice(&buffer) {
200202
Ok(s) => Ok(s),
201203
Err(e) => {

0 commit comments

Comments
 (0)