Skip to content

Fix: Limit the number in the slab #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/proto/streams/counts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ impl Counts {
self.num_send_streams != 0 || self.num_recv_streams != 0
}

/// Returns the maximum number of streams.
pub(crate) fn max_streams(&self) -> usize {
self.max_recv_streams + self.max_reset_streams
}

/// Returns true if the receive stream concurrency can be incremented
pub fn can_inc_num_recv_streams(&self) -> bool {
self.max_recv_streams > self.num_recv_streams
Expand Down
2 changes: 1 addition & 1 deletion src/proto/streams/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl Store {
self.ids.len()
}

#[cfg(feature = "unstable")]
/// Returns the number of streams that are held in slab.
pub fn num_wired_streams(&self) -> usize {
self.slab.len()
}
Expand Down
23 changes: 23 additions & 0 deletions src/proto/streams/streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,17 @@ impl<B> DynStreams<'_, B> {
pub fn recv_headers(&mut self, frame: frame::Headers) -> Result<(), Error> {
let mut me = self.inner.lock().unwrap();

if me.store.num_wired_streams() > me.counts.max_streams() {
tracing::error!(
"HEADERS: number of streams exceeds the upper limit ({})",
me.counts.max_streams()
);
return Err(Error::remote_go_away(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not clear to me that this is the correct response as stated above. Would be good to get some reference from the spec or any other implementations for what they do here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find anything more useful
https://www.rfc-editor.org/rfc/rfc7540#section-6.5.2

Bytes::from("out of stream limit"),
Reason::PROTOCOL_ERROR,
));
}

me.recv_headers(self.peer, self.send_buffer, frame)
}

Expand Down Expand Up @@ -348,6 +359,18 @@ impl<B> DynStreams<'_, B> {

pub fn recv_push_promise(&mut self, frame: frame::PushPromise) -> Result<(), Error> {
let mut me = self.inner.lock().unwrap();

if me.store.num_wired_streams() > me.counts.max_streams() {
tracing::error!(
"PUSH_PROMISE: number of streams exceeds the upper limit ({})",
me.counts.max_streams()
);
return Err(Error::remote_go_away(
Bytes::from("out of stream limit"),
Reason::PROTOCOL_ERROR,
));
}

me.recv_push_promise(self.send_buffer, frame)
}

Expand Down