Skip to content

Commit

Permalink
dtls: fix race condition in DTLSConn::close (#611)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
feschber authored Sep 7, 2024
1 parent dd7b283 commit 7d5ee0a
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions dtls/src/conn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,11 @@ impl DTLSConn {

// Close closes the connection.
pub async fn close(&self) -> Result<()> {
if !self.closed.load(Ordering::SeqCst) {
self.closed.store(true, Ordering::SeqCst);

if self
.closed
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
.is_ok()
{
// Discard error from notify() to return non-error on the first user call of Close()
// even if the underlying connection is already closed.
self.notify(AlertLevel::Warning, AlertDescription::CloseNotify)
Expand Down

0 comments on commit 7d5ee0a

Please sign in to comment.