Skip to content

Commit

Permalink
Fix panic when using sync client from a GCC destructor
Browse files Browse the repository at this point in the history
Calling Rust from a GCC destructor can result in panics when using pthreads (rust-lang/rust#28129)

Move client request code to a separate thread to work around this issue

Signed-off-by: Kostis Papazafeiropoulos <papazof@gmail.com>
  • Loading branch information
papazof committed Sep 30, 2024
1 parent c770d09 commit 751afd6
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/sync/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,28 +168,33 @@ impl Client {
.send((buf, tx))
.map_err(err_to_others_err!(e, "Send packet to sender error "))?;

let result = if req.timeout_nano == 0 {
rx.recv().map_err(err_to_others_err!(
e,
"Receive packet from Receiver error: "
))?
} else {
rx.recv_timeout(Duration::from_nanos(req.timeout_nano as u64))
.map_err(err_to_others_err!(
e,
"Receive packet from Receiver timeout: "
))?
};

let buf = result?;
let res = Response::decode(buf).map_err(err_to_others_err!(e, "Unpack response error "))?;

let status = res.status();
if status.code() != Code::OK {
return Err(Error::RpcStatus((*status).clone()));
}
let t = thread::spawn(move || {
let result = if req.timeout_nano == 0 {
rx.recv().map_err(err_to_others_err!(
e,
"Receive packet from Receiver error: "
))?
} else {
rx.recv_timeout(Duration::from_nanos(req.timeout_nano as u64))
.map_err(err_to_others_err!(
e,
"Receive packet from Receiver timeout: "
))?
};

let buf = result?;
let res = Response::decode(buf)
.map_err(err_to_others_err!(e, "Unpack response error "))?;

let status = res.status();
if status.code() != Code::OK {
return Err(Error::RpcStatus((*status).clone()));
}

Ok(res)
});

Ok(res)
t.join().unwrap()
}
}

Expand Down

0 comments on commit 751afd6

Please sign in to comment.