Skip to content

Commit

Permalink
sctp: Hold association lock when sending client init
Browse files Browse the repository at this point in the history
This fixes a race where read_loop may process incoming init and going to
established before send_init() is called.
  • Loading branch information
haaspors committed Sep 18, 2024
1 parent 6566ac7 commit 84d6980
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions sctp/src/association/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,8 @@ impl Association {
init.set_supported_extensions();

let association_internal = Arc::new(Mutex::new(ai));

{
let weak = Arc::downgrade(&association_internal);

let mut ai = association_internal.lock().await;
ai.t1init = Some(RtxTimer::new(
weak.clone(),
Expand Down Expand Up @@ -352,33 +350,32 @@ impl Association {
NO_MAX_RETRANS,
)); // retransmit forever
ai.ack_timer = Some(AckTimer::new(weak, ACK_INTERVAL));
}

tokio::spawn(Association::read_loop(
name.clone(),
Arc::clone(&bytes_received),
Arc::clone(&net_conn),
close_loop_ch_rx1,
Arc::clone(&association_internal),
));

tokio::spawn(Association::write_loop(
name.clone(),
Arc::clone(&bytes_sent),
Arc::clone(&net_conn),
close_loop_ch_rx2,
Arc::clone(&association_internal),
awake_write_loop_ch_rx,
));

if is_client {
let mut ai = association_internal.lock().await;
ai.set_state(AssociationState::CookieWait);
ai.stored_init = Some(init);
ai.send_init()?;
let rto = ai.rto_mgr.get_rto();
if let Some(t1init) = &ai.t1init {
t1init.start(rto).await;
tokio::spawn(Association::read_loop(
name.clone(),
Arc::clone(&bytes_received),
Arc::clone(&net_conn),
close_loop_ch_rx1,
Arc::clone(&association_internal),
));

tokio::spawn(Association::write_loop(
name.clone(),
Arc::clone(&bytes_sent),
Arc::clone(&net_conn),
close_loop_ch_rx2,
Arc::clone(&association_internal),
awake_write_loop_ch_rx,
));

if is_client {
ai.set_state(AssociationState::CookieWait);
ai.stored_init = Some(init);
ai.send_init()?;
let rto = ai.rto_mgr.get_rto();
if let Some(t1init) = &ai.t1init {
t1init.start(rto).await;
}
}
}

Expand Down

0 comments on commit 84d6980

Please sign in to comment.