Skip to content

Commit 05cf4c2

Browse files
authored
Promote create_agent lock to top of function, to avoid race condition (webrtc-rs#290)
* Promote lock to top of function, to avoid race condition
1 parent 7170965 commit 05cf4c2

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

ice/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# webrtc-ice changelog
22

33
## Unreleased
4+
* Promote agent lock in ice_gather.rs create_agent() to top level of the function to avoid a race condition. [#290 Promote create_agent lock to top of function, to avoid race condition](https://github.com/webrtc-rs/webrtc/pull/290) contributed by [efer-ms](https://github.com/efer-ms)
45

56
### v0.8.0
67

webrtc/src/ice_transport/ice_gatherer.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,14 @@ impl RTCIceGatherer {
7979
}
8080

8181
pub(crate) async fn create_agent(&self) -> Result<()> {
82-
{
83-
let agent = self.agent.lock().await;
84-
if agent.is_some() || self.state() != RTCIceGathererState::New {
85-
return Ok(());
86-
}
82+
// NOTE: A lock is held for the duration of this function in order to
83+
// avoid potential double-agent creations. Care should be taken to
84+
// ensure we do not do anything expensive other than the actual agent
85+
// creation in this function.
86+
87+
let mut agent = self.agent.lock().await;
88+
if agent.is_some() || self.state() != RTCIceGathererState::New {
89+
return Ok(());
8790
}
8891

8992
let mut candidate_types = vec![];
@@ -145,10 +148,7 @@ impl RTCIceGatherer {
145148

146149
config.network_types.extend(requested_network_types);
147150

148-
{
149-
let mut agent = self.agent.lock().await;
150-
*agent = Some(Arc::new(ice::agent::Agent::new(config).await?));
151-
}
151+
*agent = Some(Arc::new(ice::agent::Agent::new(config).await?));
152152

153153
Ok(())
154154
}

0 commit comments

Comments
 (0)