提交 d098a4e3 authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi

Finally (hopefully), fix deadlocks in Rust

上级 e4b99dba
...@@ -86,7 +86,7 @@ struct Inner { ...@@ -86,7 +86,7 @@ struct Inner {
closed: AtomicBool, closed: AtomicBool,
// Drop subscription to audio speaker observer-specific notifications when observer itself is // Drop subscription to audio speaker observer-specific notifications when observer itself is
// dropped // dropped
subscription_handler: Mutex<Option<SubscriptionHandler>>, _subscription_handler: Mutex<Option<SubscriptionHandler>>,
_on_router_close_handler: Mutex<HandlerId>, _on_router_close_handler: Mutex<HandlerId>,
} }
...@@ -105,8 +105,6 @@ impl Inner { ...@@ -105,8 +105,6 @@ impl Inner {
self.handlers.close.call_simple(); self.handlers.close.call_simple();
let subscription_handler = self.subscription_handler.lock().take();
if close_request { if close_request {
let channel = self.channel.clone(); let channel = self.channel.clone();
let request = RtpObserverCloseRequest { let request = RtpObserverCloseRequest {
...@@ -121,18 +119,6 @@ impl Inner { ...@@ -121,18 +119,6 @@ impl Inner {
if let Err(error) = channel.request(request).await { if let Err(error) = channel.request(request).await {
error!("active speaker observer closing failed on drop: {}", error); error!("active speaker observer closing failed on drop: {}", error);
} }
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handler);
})
.detach();
} else {
self.executor
.spawn(async move {
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handler);
}) })
.detach(); .detach();
} }
...@@ -368,7 +354,7 @@ impl ActiveSpeakerObserver { ...@@ -368,7 +354,7 @@ impl ActiveSpeakerObserver {
app_data, app_data,
router, router,
closed: AtomicBool::new(false), closed: AtomicBool::new(false),
subscription_handler: Mutex::new(subscription_handler), _subscription_handler: Mutex::new(subscription_handler),
_on_router_close_handler: Mutex::new(on_router_close_handler), _on_router_close_handler: Mutex::new(on_router_close_handler),
}); });
......
...@@ -97,7 +97,7 @@ struct Inner { ...@@ -97,7 +97,7 @@ struct Inner {
closed: AtomicBool, closed: AtomicBool,
// Drop subscription to audio level observer-specific notifications when observer itself is // Drop subscription to audio level observer-specific notifications when observer itself is
// dropped // dropped
subscription_handler: Mutex<Option<SubscriptionHandler>>, _subscription_handler: Mutex<Option<SubscriptionHandler>>,
_on_router_close_handler: Mutex<HandlerId>, _on_router_close_handler: Mutex<HandlerId>,
} }
...@@ -116,8 +116,6 @@ impl Inner { ...@@ -116,8 +116,6 @@ impl Inner {
self.handlers.close.call_simple(); self.handlers.close.call_simple();
let subscription_handler = self.subscription_handler.lock().take();
if close_request { if close_request {
let channel = self.channel.clone(); let channel = self.channel.clone();
let request = RtpObserverCloseRequest { let request = RtpObserverCloseRequest {
...@@ -132,18 +130,6 @@ impl Inner { ...@@ -132,18 +130,6 @@ impl Inner {
if let Err(error) = channel.request(request).await { if let Err(error) = channel.request(request).await {
error!("audio level observer closing failed on drop: {}", error); error!("audio level observer closing failed on drop: {}", error);
} }
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handler);
})
.detach();
} else {
self.executor
.spawn(async move {
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handler);
}) })
.detach(); .detach();
} }
...@@ -386,7 +372,7 @@ impl AudioLevelObserver { ...@@ -386,7 +372,7 @@ impl AudioLevelObserver {
app_data, app_data,
router, router,
closed: AtomicBool::new(false), closed: AtomicBool::new(false),
subscription_handler: Mutex::new(subscription_handler), _subscription_handler: Mutex::new(subscription_handler),
_on_router_close_handler: Mutex::new(on_router_close_handler), _on_router_close_handler: Mutex::new(on_router_close_handler),
}); });
......
...@@ -19,10 +19,10 @@ use event_listener_primitives::{Bag, BagOnce, HandlerId}; ...@@ -19,10 +19,10 @@ use event_listener_primitives::{Bag, BagOnce, HandlerId};
use log::{debug, error}; use log::{debug, error};
use parking_lot::Mutex; use parking_lot::Mutex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt;
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
use std::{fmt, mem};
uuid_based_wrapper_type!( uuid_based_wrapper_type!(
/// [`Consumer`] identifier. /// [`Consumer`] identifier.
...@@ -378,7 +378,7 @@ struct Inner { ...@@ -378,7 +378,7 @@ struct Inner {
weak_producer: WeakProducer, weak_producer: WeakProducer,
closed: Arc<AtomicBool>, closed: Arc<AtomicBool>,
// Drop subscription to consumer-specific notifications when consumer itself is dropped // Drop subscription to consumer-specific notifications when consumer itself is dropped
subscription_handlers: Mutex<Vec<Option<SubscriptionHandler>>>, _subscription_handlers: Mutex<Vec<Option<SubscriptionHandler>>>,
_on_transport_close_handler: Mutex<HandlerId>, _on_transport_close_handler: Mutex<HandlerId>,
} }
...@@ -397,8 +397,6 @@ impl Inner { ...@@ -397,8 +397,6 @@ impl Inner {
self.handlers.close.call_simple(); self.handlers.close.call_simple();
let subscription_handlers: Vec<_> = mem::take(&mut self.subscription_handlers.lock());
if close_request { if close_request {
let channel = self.channel.clone(); let channel = self.channel.clone();
let request = ConsumerCloseRequest { let request = ConsumerCloseRequest {
...@@ -418,18 +416,6 @@ impl Inner { ...@@ -418,18 +416,6 @@ impl Inner {
error!("consumer closing failed on drop: {}", error); error!("consumer closing failed on drop: {}", error);
} }
} }
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handlers);
})
.detach();
} else {
self.executor
.spawn(async move {
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handlers);
}) })
.detach(); .detach();
} }
...@@ -513,7 +499,15 @@ impl Consumer { ...@@ -513,7 +499,15 @@ impl Consumer {
let maybe_inner = let maybe_inner =
inner_weak.lock().as_ref().and_then(Weak::upgrade); inner_weak.lock().as_ref().and_then(Weak::upgrade);
if let Some(inner) = maybe_inner { if let Some(inner) = maybe_inner {
inner.close(false); inner
.executor
.clone()
.spawn(async move {
// Potential drop needs to happen from a different
// thread to prevent potential deadlock
inner.close(false);
})
.detach();
} }
} }
} }
...@@ -608,7 +602,7 @@ impl Consumer { ...@@ -608,7 +602,7 @@ impl Consumer {
transport, transport,
weak_producer: producer.downgrade(), weak_producer: producer.downgrade(),
closed, closed,
subscription_handlers: Mutex::new(vec![ _subscription_handlers: Mutex::new(vec![
subscription_handler, subscription_handler,
payload_subscription_handler, payload_subscription_handler,
]), ]),
......
...@@ -19,10 +19,10 @@ use log::{debug, error}; ...@@ -19,10 +19,10 @@ use log::{debug, error};
use parking_lot::Mutex; use parking_lot::Mutex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::borrow::Cow; use std::borrow::Cow;
use std::fmt;
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
use std::{fmt, mem};
uuid_based_wrapper_type!( uuid_based_wrapper_type!(
/// [`DataConsumer`] identifier. /// [`DataConsumer`] identifier.
...@@ -207,8 +207,9 @@ struct Inner { ...@@ -207,8 +207,9 @@ struct Inner {
transport: Arc<dyn Transport>, transport: Arc<dyn Transport>,
weak_data_producer: WeakDataProducer, weak_data_producer: WeakDataProducer,
closed: Arc<AtomicBool>, closed: Arc<AtomicBool>,
// Drop subscription to consumer-specific notifications when consumer itself is dropped // Drop subscription to data consumer-specific notifications when data consumer itself is
subscription_handlers: Mutex<Vec<Option<SubscriptionHandler>>>, // dropped
_subscription_handlers: Mutex<Vec<Option<SubscriptionHandler>>>,
_on_transport_close_handler: Mutex<HandlerId>, _on_transport_close_handler: Mutex<HandlerId>,
} }
...@@ -227,8 +228,6 @@ impl Inner { ...@@ -227,8 +228,6 @@ impl Inner {
self.handlers.close.call_simple(); self.handlers.close.call_simple();
let subscription_handlers: Vec<_> = mem::take(&mut self.subscription_handlers.lock());
if close_request { if close_request {
let channel = self.channel.clone(); let channel = self.channel.clone();
let request = DataConsumerCloseRequest { let request = DataConsumerCloseRequest {
...@@ -248,18 +247,6 @@ impl Inner { ...@@ -248,18 +247,6 @@ impl Inner {
error!("consumer closing failed on drop: {}", error); error!("consumer closing failed on drop: {}", error);
} }
} }
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handlers);
})
.detach();
} else {
self.executor
.spawn(async move {
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handlers);
}) })
.detach(); .detach();
} }
...@@ -388,7 +375,15 @@ impl DataConsumer { ...@@ -388,7 +375,15 @@ impl DataConsumer {
let maybe_inner = let maybe_inner =
inner_weak.lock().as_ref().and_then(Weak::upgrade); inner_weak.lock().as_ref().and_then(Weak::upgrade);
if let Some(inner) = maybe_inner { if let Some(inner) = maybe_inner {
inner.close(false); inner
.executor
.clone()
.spawn(async move {
// Potential drop needs to happen from a different
// thread to prevent potential deadlock
inner.close(false);
})
.detach();
} }
} }
} }
...@@ -461,7 +456,7 @@ impl DataConsumer { ...@@ -461,7 +456,7 @@ impl DataConsumer {
transport, transport,
weak_data_producer: data_producer.downgrade(), weak_data_producer: data_producer.downgrade(),
closed, closed,
subscription_handlers: Mutex::new(vec![ _subscription_handlers: Mutex::new(vec![
subscription_handler, subscription_handler,
payload_subscription_handler, payload_subscription_handler,
]), ]),
......
...@@ -25,9 +25,9 @@ use log::{debug, error}; ...@@ -25,9 +25,9 @@ use log::{debug, error};
use nohash_hasher::IntMap; use nohash_hasher::IntMap;
use parking_lot::Mutex; use parking_lot::Mutex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
use std::{fmt, mem};
/// [`DirectTransport`] options. /// [`DirectTransport`] options.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
...@@ -147,7 +147,7 @@ struct Inner { ...@@ -147,7 +147,7 @@ struct Inner {
router: Router, router: Router,
closed: AtomicBool, closed: AtomicBool,
// Drop subscription to transport-specific notifications when transport itself is dropped // Drop subscription to transport-specific notifications when transport itself is dropped
subscription_handlers: Mutex<Vec<Option<SubscriptionHandler>>>, _subscription_handlers: Mutex<Vec<Option<SubscriptionHandler>>>,
_on_router_close_handler: Mutex<HandlerId>, _on_router_close_handler: Mutex<HandlerId>,
} }
...@@ -166,8 +166,6 @@ impl Inner { ...@@ -166,8 +166,6 @@ impl Inner {
self.handlers.close.call_simple(); self.handlers.close.call_simple();
let subscription_handlers: Vec<_> = mem::take(&mut self.subscription_handlers.lock());
if close_request { if close_request {
let channel = self.channel.clone(); let channel = self.channel.clone();
let request = TransportCloseRequest { let request = TransportCloseRequest {
...@@ -182,20 +180,8 @@ impl Inner { ...@@ -182,20 +180,8 @@ impl Inner {
if let Err(error) = channel.request(request).await { if let Err(error) = channel.request(request).await {
error!("transport closing failed on drop: {}", error); error!("transport closing failed on drop: {}", error);
} }
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handlers);
}) })
.detach(); .detach();
} else {
self.executor
.spawn(async move {
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handlers);
})
.detach()
} }
} }
} }
...@@ -513,7 +499,7 @@ impl DirectTransport { ...@@ -513,7 +499,7 @@ impl DirectTransport {
app_data, app_data,
router, router,
closed: AtomicBool::new(false), closed: AtomicBool::new(false),
subscription_handlers: Mutex::new(vec![ _subscription_handlers: Mutex::new(vec![
subscription_handler, subscription_handler,
payload_subscription_handler, payload_subscription_handler,
]), ]),
......
...@@ -198,7 +198,7 @@ struct Inner { ...@@ -198,7 +198,7 @@ struct Inner {
router: Router, router: Router,
closed: AtomicBool, closed: AtomicBool,
// Drop subscription to transport-specific notifications when transport itself is dropped // Drop subscription to transport-specific notifications when transport itself is dropped
subscription_handler: Mutex<Option<SubscriptionHandler>>, _subscription_handler: Mutex<Option<SubscriptionHandler>>,
_on_router_close_handler: Mutex<HandlerId>, _on_router_close_handler: Mutex<HandlerId>,
} }
...@@ -217,8 +217,6 @@ impl Inner { ...@@ -217,8 +217,6 @@ impl Inner {
self.handlers.close.call_simple(); self.handlers.close.call_simple();
let subscription_handler = self.subscription_handler.lock().take();
if close_request { if close_request {
let channel = self.channel.clone(); let channel = self.channel.clone();
let request = TransportCloseRequest { let request = TransportCloseRequest {
...@@ -233,18 +231,6 @@ impl Inner { ...@@ -233,18 +231,6 @@ impl Inner {
if let Err(error) = channel.request(request).await { if let Err(error) = channel.request(request).await {
error!("transport closing failed on drop: {}", error); error!("transport closing failed on drop: {}", error);
} }
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handler);
})
.detach();
} else {
self.executor
.spawn(async move {
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handler);
}) })
.detach(); .detach();
} }
...@@ -552,7 +538,7 @@ impl PipeTransport { ...@@ -552,7 +538,7 @@ impl PipeTransport {
app_data, app_data,
router, router,
closed: AtomicBool::new(false), closed: AtomicBool::new(false),
subscription_handler: Mutex::new(subscription_handler), _subscription_handler: Mutex::new(subscription_handler),
_on_router_close_handler: Mutex::new(on_router_close_handler), _on_router_close_handler: Mutex::new(on_router_close_handler),
}); });
......
...@@ -235,7 +235,7 @@ struct Inner { ...@@ -235,7 +235,7 @@ struct Inner {
router: Router, router: Router,
closed: AtomicBool, closed: AtomicBool,
// Drop subscription to transport-specific notifications when transport itself is dropped // Drop subscription to transport-specific notifications when transport itself is dropped
subscription_handler: Mutex<Option<SubscriptionHandler>>, _subscription_handler: Mutex<Option<SubscriptionHandler>>,
_on_router_close_handler: Mutex<HandlerId>, _on_router_close_handler: Mutex<HandlerId>,
} }
...@@ -254,8 +254,6 @@ impl Inner { ...@@ -254,8 +254,6 @@ impl Inner {
self.handlers.close.call_simple(); self.handlers.close.call_simple();
let subscription_handler = self.subscription_handler.lock().take();
if close_request { if close_request {
let channel = self.channel.clone(); let channel = self.channel.clone();
let request = TransportCloseRequest { let request = TransportCloseRequest {
...@@ -270,18 +268,6 @@ impl Inner { ...@@ -270,18 +268,6 @@ impl Inner {
if let Err(error) = channel.request(request).await { if let Err(error) = channel.request(request).await {
error!("transport closing failed on drop: {}", error); error!("transport closing failed on drop: {}", error);
} }
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handler);
})
.detach();
} else {
self.executor
.spawn(async move {
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handler);
}) })
.detach(); .detach();
} }
...@@ -592,7 +578,7 @@ impl PlainTransport { ...@@ -592,7 +578,7 @@ impl PlainTransport {
app_data, app_data,
router, router,
closed: AtomicBool::new(false), closed: AtomicBool::new(false),
subscription_handler: Mutex::new(subscription_handler), _subscription_handler: Mutex::new(subscription_handler),
_on_router_close_handler: Mutex::new(on_router_close_handler), _on_router_close_handler: Mutex::new(on_router_close_handler),
}); });
......
...@@ -305,7 +305,7 @@ struct Inner { ...@@ -305,7 +305,7 @@ struct Inner {
transport: Arc<dyn Transport>, transport: Arc<dyn Transport>,
closed: AtomicBool, closed: AtomicBool,
// Drop subscription to producer-specific notifications when producer itself is dropped // Drop subscription to producer-specific notifications when producer itself is dropped
subscription_handler: Mutex<Option<SubscriptionHandler>>, _subscription_handler: Mutex<Option<SubscriptionHandler>>,
_on_transport_close_handler: Mutex<HandlerId>, _on_transport_close_handler: Mutex<HandlerId>,
} }
...@@ -324,8 +324,6 @@ impl Inner { ...@@ -324,8 +324,6 @@ impl Inner {
self.handlers.close.call_simple(); self.handlers.close.call_simple();
let subscription_handler = self.subscription_handler.lock().take();
if close_request { if close_request {
let channel = self.channel.clone(); let channel = self.channel.clone();
let request = ProducerCloseRequest { let request = ProducerCloseRequest {
...@@ -341,18 +339,6 @@ impl Inner { ...@@ -341,18 +339,6 @@ impl Inner {
if let Err(error) = channel.request(request).await { if let Err(error) = channel.request(request).await {
error!("producer closing failed on drop: {}", error); error!("producer closing failed on drop: {}", error);
} }
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handler);
})
.detach();
} else {
self.executor
.spawn(async move {
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handler);
}) })
.detach(); .detach();
} }
...@@ -525,7 +511,7 @@ impl Producer { ...@@ -525,7 +511,7 @@ impl Producer {
app_data, app_data,
transport, transport,
closed: AtomicBool::new(false), closed: AtomicBool::new(false),
subscription_handler: Mutex::new(subscription_handler), _subscription_handler: Mutex::new(subscription_handler),
_on_transport_close_handler: Mutex::new(on_transport_close_handler), _on_transport_close_handler: Mutex::new(on_transport_close_handler),
}); });
......
...@@ -281,7 +281,7 @@ struct Inner { ...@@ -281,7 +281,7 @@ struct Inner {
router: Router, router: Router,
closed: AtomicBool, closed: AtomicBool,
// Drop subscription to transport-specific notifications when transport itself is dropped // Drop subscription to transport-specific notifications when transport itself is dropped
subscription_handler: Mutex<Option<SubscriptionHandler>>, _subscription_handler: Mutex<Option<SubscriptionHandler>>,
_on_router_close_handler: Mutex<HandlerId>, _on_router_close_handler: Mutex<HandlerId>,
} }
...@@ -300,8 +300,6 @@ impl Inner { ...@@ -300,8 +300,6 @@ impl Inner {
self.handlers.close.call_simple(); self.handlers.close.call_simple();
let subscription_handler = self.subscription_handler.lock().take();
if close_request { if close_request {
let channel = self.channel.clone(); let channel = self.channel.clone();
let request = TransportCloseRequest { let request = TransportCloseRequest {
...@@ -316,18 +314,6 @@ impl Inner { ...@@ -316,18 +314,6 @@ impl Inner {
if let Err(error) = channel.request(request).await { if let Err(error) = channel.request(request).await {
error!("transport closing failed on drop: {}", error); error!("transport closing failed on drop: {}", error);
} }
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handler);
})
.detach();
} else {
self.executor
.spawn(async move {
// Drop from a different thread to avoid deadlock with recursive dropping
// from within another subscription drop.
drop(subscription_handler);
}) })
.detach(); .detach();
} }
...@@ -662,7 +648,7 @@ impl WebRtcTransport { ...@@ -662,7 +648,7 @@ impl WebRtcTransport {
app_data, app_data,
router, router,
closed: AtomicBool::new(false), closed: AtomicBool::new(false),
subscription_handler: Mutex::new(subscription_handler), _subscription_handler: Mutex::new(subscription_handler),
_on_router_close_handler: Mutex::new(on_router_close_handler), _on_router_close_handler: Mutex::new(on_router_close_handler),
}); });
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论