Skip to content

Commit bd055ed

Browse files
committed
fix(sync): Avoid 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 f31f592 commit bd055ed

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/sync/client.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -169,28 +169,33 @@ impl Client {
169169
.send((buf, tx))
170170
.map_err(err_to_others_err!(e, "Send packet to sender error "))?;
171171

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

193-
Ok(res)
198+
t.join().unwrap()
194199
}
195200
}
196201

0 commit comments

Comments
 (0)