Skip to content

Commit fa52fde

Browse files
crop2000crop
andauthored
fix deadlock issue #219 (#220)
Co-authored-by: crop <[email protected]>
1 parent e447d7d commit fa52fde

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/client/async_client.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,32 +96,30 @@ impl<N, P> AsyncClient<N, P> {
9696
/// therefore unsafe to continue using.
9797
pub fn deactivate(self) -> Result<(Client, N, P), Error> {
9898
let mut c = self;
99-
unsafe {
100-
c.maybe_deactivate()
101-
.map(|c| (c.client, c.notification, c.process))
102-
}
99+
c.maybe_deactivate()
100+
.map(|c| (c.client, c.notification, c.process))
103101
}
104102

105103
// Helper function for deactivating. Any function that calls this should
106104
// have ownership of self and no longer use it after this call.
107-
unsafe fn maybe_deactivate(&mut self) -> Result<Box<CallbackContext<N, P>>, Error> {
108-
let _m = CREATE_OR_DESTROY_CLIENT_MUTEX.lock().ok();
105+
fn maybe_deactivate(&mut self) -> Result<Box<CallbackContext<N, P>>, Error> {
106+
let m = CREATE_OR_DESTROY_CLIENT_MUTEX.lock();
109107
if self.callback.is_none() {
108+
drop(m);
110109
return Err(Error::ClientIsNoLongerAlive);
111110
}
112111
let cb = self.callback.take().ok_or(Error::ClientIsNoLongerAlive)?;
113-
let client_ptr = cb.client.raw();
114-
115112
// deactivate
116-
if j::jack_deactivate(client_ptr) != 0 {
113+
if unsafe { j::jack_deactivate(cb.client.raw()) } != 0 {
114+
drop(m);
117115
return Err(Error::ClientDeactivationError);
118116
}
119117

120118
// clear the callbacks
121-
clear_callbacks(client_ptr)?;
119+
unsafe { clear_callbacks(cb.client.raw()) }?;
122120
// done, take ownership of callback
123121
if cb.has_panic.load(std::sync::atomic::Ordering::Relaxed) {
124-
std::mem::forget(cb);
122+
drop(m);
125123
return Err(Error::ClientPanicked);
126124
}
127125
Ok(cb)
@@ -132,7 +130,7 @@ impl<N, P> AsyncClient<N, P> {
132130
impl<N, P> Drop for AsyncClient<N, P> {
133131
// Deactivate and close the client.
134132
fn drop(&mut self) {
135-
let _ = unsafe { self.maybe_deactivate() };
133+
let _ = self.maybe_deactivate();
136134
}
137135
}
138136

0 commit comments

Comments
 (0)