Skip to content

Commit f45e028

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 <[email protected]>
1 parent d8f0806 commit f45e028

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/sync/client.rs

+26-21
Original file line numberDiff line numberDiff line change
@@ -168,28 +168,33 @@ impl Client {
168168
.send((buf, tx))
169169
.map_err(err_to_others_err!(e, "Send packet to sender error "))?;
170170

171-
let result = if req.timeout_nano == 0 {
172-
rx.recv().map_err(err_to_others_err!(
173-
e,
174-
"Receive packet from Receiver error: "
175-
))?
176-
} else {
177-
rx.recv_timeout(Duration::from_nanos(req.timeout_nano as u64))
178-
.map_err(err_to_others_err!(
179-
e,
180-
"Receive packet from Receiver timeout: "
181-
))?
182-
};
183-
184-
let buf = result?;
185-
let res = Response::decode(buf).map_err(err_to_others_err!(e, "Unpack response error "))?;
186-
187-
let status = res.status();
188-
if status.code() != Code::OK {
189-
return Err(Error::RpcStatus((*status).clone()));
190-
}
171+
let t = thread::spawn(move || {
172+
let result = if req.timeout_nano == 0 {
173+
rx.recv().map_err(err_to_others_err!(
174+
e,
175+
"Receive packet from Receiver error: "
176+
))?
177+
} else {
178+
rx.recv_timeout(Duration::from_nanos(req.timeout_nano as u64))
179+
.map_err(err_to_others_err!(
180+
e,
181+
"Receive packet from Receiver timeout: "
182+
))?
183+
};
184+
185+
let buf = result?;
186+
let res = Response::decode(buf)
187+
.map_err(err_to_others_err!(e, "Unpack response error "))?;
188+
189+
let status = res.status();
190+
if status.code() != Code::OK {
191+
return Err(Error::RpcStatus((*status).clone()));
192+
}
193+
194+
Ok(res)
195+
});
191196

192-
Ok(res)
197+
t.join().unwrap()
193198
}
194199
}
195200

0 commit comments

Comments
 (0)