diff --git a/src/proto/streams/counts.rs b/src/proto/streams/counts.rs index f74aa14a7..2b43565a1 100644 --- a/src/proto/streams/counts.rs +++ b/src/proto/streams/counts.rs @@ -282,19 +282,16 @@ impl Counts { where F: FnOnce(&mut Self, &mut store::Ptr) -> U, { - // TODO: Does this need to be computed before performing the action? - let is_pending_reset = stream.is_pending_reset_expiration(); - // Run the action let ret = f(self, &mut stream); - self.transition_after(stream, is_pending_reset); + self.transition_after(stream); ret } // TODO: move this to macro? - pub fn transition_after(&mut self, mut stream: store::Ptr, is_reset_counted: bool) { + pub fn transition_after(&mut self, mut stream: store::Ptr) { tracing::trace!( "transition_after; stream={:?}; state={:?}; is_closed={:?}; \ pending_send_empty={:?}; buffered_send_data={}; \ @@ -311,9 +308,6 @@ impl Counts { if stream.is_closed() { if !stream.is_pending_reset_expiration() { stream.unlink(); - if is_reset_counted { - self.dec_num_reset_streams(); - } } if !stream.state.is_scheduled_reset() && stream.is_counted { @@ -355,7 +349,7 @@ impl Counts { } } - fn dec_num_reset_streams(&mut self) { + pub fn dec_num_reset_streams(&mut self) { assert!(self.num_local_reset_streams > 0); self.num_local_reset_streams -= 1; } diff --git a/src/proto/streams/prioritize.rs b/src/proto/streams/prioritize.rs index 910b2573d..e860501d3 100644 --- a/src/proto/streams/prioritize.rs +++ b/src/proto/streams/prioritize.rs @@ -685,18 +685,16 @@ impl Prioritize { pub fn clear_pending_send(&mut self, store: &mut Store, counts: &mut Counts) { while let Some(mut stream) = self.pending_send.pop(store) { - let is_pending_reset = stream.is_pending_reset_expiration(); if let Some(reason) = stream.state.get_scheduled_reset() { stream.set_reset(reason, Initiator::Library); } - counts.transition_after(stream, is_pending_reset); + counts.transition_after(stream); } } pub fn clear_pending_open(&mut self, store: &mut Store, counts: &mut Counts) { while let Some(stream) = self.pending_open.pop(store) { - let is_pending_reset = stream.is_pending_reset_expiration(); - counts.transition_after(stream, is_pending_reset); + counts.transition_after(stream); } } @@ -718,14 +716,7 @@ impl Prioritize { let span = tracing::trace_span!("popped", ?stream.id, ?stream.state); let _e = span.enter(); - // It's possible that this stream, besides having data to send, - // is also queued to send a reset, and thus is already in the queue - // to wait for "some time" after a reset. - // - // To be safe, we just always ask the stream. - let is_pending_reset = stream.is_pending_reset_expiration(); - - tracing::trace!(is_pending_reset); + tracing::trace!(is_pending_reset = stream.is_pending_reset_expiration()); let frame = match stream.pending_send.pop_front(buffer) { Some(Frame::Data(mut frame)) => { @@ -873,7 +864,7 @@ impl Prioritize { // Since this should only happen as a consequence of `clear_queue`, // we must be in a closed state of some kind. debug_assert!(stream.state.is_closed()); - counts.transition_after(stream, is_pending_reset); + counts.transition_after(stream); continue; } } @@ -894,7 +885,7 @@ impl Prioritize { self.pending_send.push(&mut stream); } - counts.transition_after(stream, is_pending_reset); + counts.transition_after(stream); return Some(frame); } diff --git a/src/proto/streams/recv.rs b/src/proto/streams/recv.rs index 063f829dd..2e0423ce1 100644 --- a/src/proto/streams/recv.rs +++ b/src/proto/streams/recv.rs @@ -1043,16 +1043,21 @@ impl Recv { return; } - if counts.can_inc_num_reset_streams() { - counts.inc_num_reset_streams(); - tracing::trace!("enqueue_reset_expiration; added {:?}", stream.id); - self.pending_reset_expired.push(stream); - } else { - tracing::trace!( - "enqueue_reset_expiration; dropped {:?}, over max_concurrent_reset_streams", - stream.id - ); + if !counts.can_inc_num_reset_streams() { + let oldest = match self.pending_reset_expired.pop(stream) { + Some(oldest) => oldest, + // Retention is disabled. + None => return, + }; + tracing::trace!("enqueue_reset_expiration; evicted {:?}", oldest.id); + // Free the queue slot even if the stream still has frames to send. + counts.dec_num_reset_streams(); + counts.transition_after(oldest); } + + counts.inc_num_reset_streams(); + tracing::trace!("enqueue_reset_expiration; added {:?}", stream.id); + self.pending_reset_expired.push(stream); } pub fn clear_expired_reset_streams(&mut self, store: &mut Store, counts: &mut Counts) { @@ -1066,7 +1071,8 @@ impl Recv { // monotonic). We use a saturating operation to avoid this panic here. now.saturating_duration_since(reset_at) > reset_duration }) { - counts.transition_after(stream, true); + counts.dec_num_reset_streams(); + counts.transition_after(stream); } } } @@ -1096,13 +1102,14 @@ impl Recv { /// Called on EOF fn clear_all_reset_streams(&mut self, store: &mut Store, counts: &mut Counts) { while let Some(stream) = self.pending_reset_expired.pop(store) { - counts.transition_after(stream, true); + counts.dec_num_reset_streams(); + counts.transition_after(stream); } } fn clear_all_pending_accept(&mut self, store: &mut Store, counts: &mut Counts) { while let Some(stream) = self.pending_accept.pop(store) { - counts.transition_after(stream, false); + counts.transition_after(stream); } } diff --git a/tests/h2-tests/tests/stream_states.rs b/tests/h2-tests/tests/stream_states.rs index 6f9e31de5..68ecd7269 100644 --- a/tests/h2-tests/tests/stream_states.rs +++ b/tests/h2-tests/tests/stream_states.rs @@ -1059,15 +1059,12 @@ async fn rst_stream_max() { srv.send_frame(frames::data(3, vec![0; 16])).await; srv.recv_frame(frames::reset(1).cancel()).await; srv.recv_frame(frames::reset(3).cancel()).await; - // sending frame after canceled! - // olders streams trump newer streams - // 1 is still being ignored - srv.send_frame(frames::data(1, vec![0; 16]).eos()).await; - // ping pong to be sure of no goaway - srv.ping_pong([1; 8]).await; - // 3 has been evicted, will get a reset + // The most recently reset stream still has its grace period. srv.send_frame(frames::data(3, vec![0; 16]).eos()).await; - srv.recv_frame(frames::reset(3).stream_closed()).await; + srv.ping_pong([1; 8]).await; + // The oldest reset stream has lost its grace period. + srv.send_frame(frames::data(1, vec![0; 16]).eos()).await; + srv.recv_frame(frames::reset(1).stream_closed()).await; }; let client = async move {