提交 72e8c9cf authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi

Fix racy deadlock on recursive subscription handler drop

上级 ff4108bf
...@@ -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: Option<SubscriptionHandler>, subscription_handler: Mutex<Option<SubscriptionHandler>>,
_on_router_close_handler: Mutex<HandlerId>, _on_router_close_handler: Mutex<HandlerId>,
} }
...@@ -113,11 +113,17 @@ impl Inner { ...@@ -113,11 +113,17 @@ impl Inner {
rtp_observer_id: self.id, rtp_observer_id: self.id,
}, },
}; };
let subscription_handler = self.subscription_handler.lock().take();
self.executor self.executor
.spawn(async move { .spawn(async move {
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(); .detach();
} }
...@@ -348,7 +354,7 @@ impl ActiveSpeakerObserver { ...@@ -348,7 +354,7 @@ impl ActiveSpeakerObserver {
app_data, app_data,
router, router,
closed: AtomicBool::new(false), closed: AtomicBool::new(false),
_subscription_handler: 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: Option<SubscriptionHandler>, subscription_handler: Mutex<Option<SubscriptionHandler>>,
_on_router_close_handler: Mutex<HandlerId>, _on_router_close_handler: Mutex<HandlerId>,
} }
...@@ -124,11 +124,17 @@ impl Inner { ...@@ -124,11 +124,17 @@ impl Inner {
rtp_observer_id: self.id, rtp_observer_id: self.id,
}, },
}; };
let subscription_handler = self.subscription_handler.lock().take();
self.executor self.executor
.spawn(async move { .spawn(async move {
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(); .detach();
} }
...@@ -366,7 +372,7 @@ impl AudioLevelObserver { ...@@ -366,7 +372,7 @@ impl AudioLevelObserver {
app_data, app_data,
router, router,
closed: AtomicBool::new(false), closed: AtomicBool::new(false),
_subscription_handler: 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: Vec<Option<SubscriptionHandler>>, subscription_handlers: Mutex<Vec<Option<SubscriptionHandler>>>,
_on_transport_close_handler: Mutex<HandlerId>, _on_transport_close_handler: Mutex<HandlerId>,
} }
...@@ -407,6 +407,8 @@ impl Inner { ...@@ -407,6 +407,8 @@ impl Inner {
producer_id: self.producer_id, producer_id: self.producer_id,
}, },
}; };
let subscription_handlers: Vec<_> =
mem::take(&mut self.subscription_handlers.lock());
let weak_producer = self.weak_producer.clone(); let weak_producer = self.weak_producer.clone();
self.executor self.executor
...@@ -415,6 +417,10 @@ impl Inner { ...@@ -415,6 +417,10 @@ impl Inner {
if let Err(error) = channel.request(request).await { if let Err(error) = channel.request(request).await {
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(); .detach();
...@@ -592,7 +598,10 @@ impl Consumer { ...@@ -592,7 +598,10 @@ impl Consumer {
transport, transport,
weak_producer: producer.downgrade(), weak_producer: producer.downgrade(),
closed, closed,
_subscription_handlers: vec![subscription_handler, payload_subscription_handler], subscription_handlers: Mutex::new(vec![
subscription_handler,
payload_subscription_handler,
]),
_on_transport_close_handler: Mutex::new(on_transport_close_handler), _on_transport_close_handler: Mutex::new(on_transport_close_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.
...@@ -208,7 +208,7 @@ struct Inner { ...@@ -208,7 +208,7 @@ struct Inner {
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 consumer-specific notifications when consumer itself is dropped
_subscription_handlers: Vec<Option<SubscriptionHandler>>, subscription_handlers: Mutex<Vec<Option<SubscriptionHandler>>>,
_on_transport_close_handler: Mutex<HandlerId>, _on_transport_close_handler: Mutex<HandlerId>,
} }
...@@ -237,6 +237,8 @@ impl Inner { ...@@ -237,6 +237,8 @@ impl Inner {
data_producer_id: self.data_producer_id, data_producer_id: self.data_producer_id,
}, },
}; };
let subscription_handlers: Vec<_> =
mem::take(&mut self.subscription_handlers.lock());
let weak_data_producer = self.weak_data_producer.clone(); let weak_data_producer = self.weak_data_producer.clone();
self.executor self.executor
...@@ -246,6 +248,10 @@ impl Inner { ...@@ -246,6 +248,10 @@ 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(); .detach();
} }
...@@ -445,7 +451,10 @@ impl DataConsumer { ...@@ -445,7 +451,10 @@ impl DataConsumer {
transport, transport,
weak_data_producer: data_producer.downgrade(), weak_data_producer: data_producer.downgrade(),
closed, closed,
_subscription_handlers: vec![subscription_handler, payload_subscription_handler], subscription_handlers: Mutex::new(vec![
subscription_handler,
payload_subscription_handler,
]),
_on_transport_close_handler: Mutex::new(on_transport_close_handler), _on_transport_close_handler: Mutex::new(on_transport_close_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: Vec<Option<SubscriptionHandler>>, subscription_handlers: Mutex<Vec<Option<SubscriptionHandler>>>,
_on_router_close_handler: Mutex<HandlerId>, _on_router_close_handler: Mutex<HandlerId>,
} }
...@@ -174,11 +174,18 @@ impl Inner { ...@@ -174,11 +174,18 @@ impl Inner {
transport_id: self.id, transport_id: self.id,
}, },
}; };
let subscription_handlers: Vec<_> =
mem::take(&mut self.subscription_handlers.lock());
self.executor self.executor
.spawn(async move { .spawn(async move {
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();
} }
...@@ -515,7 +522,10 @@ impl DirectTransport { ...@@ -515,7 +522,10 @@ impl DirectTransport {
app_data, app_data,
router, router,
closed: AtomicBool::new(false), closed: AtomicBool::new(false),
_subscription_handlers: vec![subscription_handler, payload_subscription_handler], subscription_handlers: Mutex::new(vec![
subscription_handler,
payload_subscription_handler,
]),
_on_router_close_handler: Mutex::new(on_router_close_handler), _on_router_close_handler: Mutex::new(on_router_close_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: Option<SubscriptionHandler>, subscription_handler: Mutex<Option<SubscriptionHandler>>,
_on_router_close_handler: Mutex<HandlerId>, _on_router_close_handler: Mutex<HandlerId>,
} }
...@@ -225,11 +225,17 @@ impl Inner { ...@@ -225,11 +225,17 @@ impl Inner {
transport_id: self.id, transport_id: self.id,
}, },
}; };
let subscription_handler = self.subscription_handler.lock().take();
self.executor self.executor
.spawn(async move { .spawn(async move {
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(); .detach();
} }
...@@ -540,7 +546,7 @@ impl PipeTransport { ...@@ -540,7 +546,7 @@ impl PipeTransport {
app_data, app_data,
router, router,
closed: AtomicBool::new(false), closed: AtomicBool::new(false),
_subscription_handler: 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: Option<SubscriptionHandler>, subscription_handler: Mutex<Option<SubscriptionHandler>>,
_on_router_close_handler: Mutex<HandlerId>, _on_router_close_handler: Mutex<HandlerId>,
} }
...@@ -262,11 +262,17 @@ impl Inner { ...@@ -262,11 +262,17 @@ impl Inner {
transport_id: self.id, transport_id: self.id,
}, },
}; };
let subscription_handler = self.subscription_handler.lock().take();
self.executor self.executor
.spawn(async move { .spawn(async move {
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(); .detach();
} }
...@@ -580,7 +586,7 @@ impl PlainTransport { ...@@ -580,7 +586,7 @@ impl PlainTransport {
app_data, app_data,
router, router,
closed: AtomicBool::new(false), closed: AtomicBool::new(false),
_subscription_handler: 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: Box<dyn Transport>, transport: Box<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: Option<SubscriptionHandler>, subscription_handler: Mutex<Option<SubscriptionHandler>>,
_on_transport_close_handler: Mutex<HandlerId>, _on_transport_close_handler: Mutex<HandlerId>,
} }
...@@ -333,10 +333,16 @@ impl Inner { ...@@ -333,10 +333,16 @@ impl Inner {
producer_id: self.id, producer_id: self.id,
}, },
}; };
let subscription_handler = self.subscription_handler.lock().take();
self.executor self.executor
.spawn(async move { .spawn(async move {
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(); .detach();
...@@ -509,7 +515,7 @@ impl Producer { ...@@ -509,7 +515,7 @@ impl Producer {
app_data, app_data,
transport, transport,
closed: AtomicBool::new(false), closed: AtomicBool::new(false),
_subscription_handler: 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),
}); });
......
...@@ -282,7 +282,7 @@ struct Inner { ...@@ -282,7 +282,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: Option<SubscriptionHandler>, subscription_handler: Mutex<Option<SubscriptionHandler>>,
_on_router_close_handler: Mutex<HandlerId>, _on_router_close_handler: Mutex<HandlerId>,
} }
...@@ -309,11 +309,17 @@ impl Inner { ...@@ -309,11 +309,17 @@ impl Inner {
transport_id: self.id, transport_id: self.id,
}, },
}; };
let subscription_handler = self.subscription_handler.lock().take();
self.executor self.executor
.spawn(async move { .spawn(async move {
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(); .detach();
} }
...@@ -651,7 +657,7 @@ impl WebRtcTransport { ...@@ -651,7 +657,7 @@ impl WebRtcTransport {
app_data, app_data,
router, router,
closed: AtomicBool::new(false), closed: AtomicBool::new(false),
_subscription_handler: 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 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论