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

Commit abc0881

Browse files
committed
Fix panic when using sync client from a GCC destructor
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>
1 parent 889755b commit abc0881

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/sync/client.rs

+24-20
Original file line numberDiff line numberDiff line change
@@ -216,27 +216,31 @@ impl Client {
216216
.send((buf, tx))
217217
.map_err(err_to_others_err!(e, "Send packet to sender error "))?;
218218

219-
let result = if req.timeout_nano == 0 {
220-
rx.recv()
221-
.map_err(err_to_others_err!(e, "Receive packet from recver error: "))?
222-
} else {
223-
rx.recv_timeout(Duration::from_nanos(req.timeout_nano as u64))
224-
.map_err(err_to_others_err!(
225-
e,
226-
"Receive packet from recver timeout: "
227-
))?
228-
};
229-
230-
let buf = result?;
231-
let res =
232-
Response::decode(&buf).map_err(err_to_others_err!(e, "Unpack response error "))?;
233-
234-
let status = res.status();
235-
if status.code() != Code::OK {
236-
return Err(Error::RpcStatus((*status).clone()));
237-
}
219+
let t = thread::spawn(move || {
220+
let result = if req.timeout_nano == 0 {
221+
rx.recv()
222+
.map_err(err_to_others_err!(e, "Receive packet from recver error: "))?
223+
} else {
224+
rx.recv_timeout(Duration::from_nanos(req.timeout_nano as u64))
225+
.map_err(err_to_others_err!(
226+
e,
227+
"Receive packet from recver timeout: "
228+
))?
229+
};
230+
231+
let buf = result?;
232+
let res =
233+
Response::decode(&buf).map_err(err_to_others_err!(e, "Unpack response error "))?;
234+
235+
let status = res.status();
236+
if status.code() != Code::OK {
237+
return Err(Error::RpcStatus((*status).clone()));
238+
}
239+
240+
Ok(res)
241+
});
238242

239-
Ok(res)
243+
t.join().unwrap()
240244
}
241245
}
242246

0 commit comments

Comments
 (0)