Skip to content

Commit 7d5ee0a

Browse files
authored
dtls: fix race condition in DTLSConn::close (webrtc-rs#611)
a context switch to a simultaneous close operation between `closed.load()` and `closed.store()` would lead to an additional unwanted close operation on the inner connection
1 parent dd7b283 commit 7d5ee0a

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

dtls/src/conn/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,11 @@ impl DTLSConn {
491491

492492
// Close closes the connection.
493493
pub async fn close(&self) -> Result<()> {
494-
if !self.closed.load(Ordering::SeqCst) {
495-
self.closed.store(true, Ordering::SeqCst);
496-
494+
if self
495+
.closed
496+
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
497+
.is_ok()
498+
{
497499
// Discard error from notify() to return non-error on the first user call of Close()
498500
// even if the underlying connection is already closed.
499501
self.notify(AlertLevel::Warning, AlertDescription::CloseNotify)

0 commit comments

Comments
 (0)