Skip to content

Fix-453: Create new connections when stream ids expire - #647

Closed
gdhameeja wants to merge 1 commit into
encode:masterfrom
gdhameeja:Fix-453
Closed

gdhameeja wants to merge 1 commit into
encode:masterfrom
gdhameeja:Fix-453

Conversation

@gdhameeja

Copy link
Copy Markdown
Contributor

Fixes #453
Creating new connections when stream ids expire by catching NoAvailableStreamIDError in send method of HTTP2Connection.

Comment thread httpx/dispatch/http2.py Outdated
except NoAvailableStreamIDError:
self.state = h2.connection.H2Connection(config=self.CONFIG)
self.init_stated = False
self.send(request, timeout)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's recursively calling send. Maybe limit it/change behaviour in case NoAvailableStreamIDError is reoccurring?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Also, shouldn't you await ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's recursively calling send. Maybe limit it/change behaviour in case NoAvailableStreamIDError is reoccurring?

NoAvailableStreamIDError shouldn't be raised again since we're opening a new connection. I suppose the exception should only be raised when the identifier for stream ids expire, which is 2^31 on a particular connection I guess. If it does get raised, that would be a bug in the hyper lib?

Also, shouldn't you await ?

yes, I missed that.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'm not very experienced with HTTP2, but I can assume that the exception can also come from the serverside? If so, a 'bad' server could lead to such scenario.

Also I'd add tests to cover the new scenario.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not very experienced with HTTP2, but I can assume that the exception can also come from the serverside? If so, a 'bad' server could lead to such scenario.

Also I'd add tests to cover the new scenario.

Yes, so I believe according to the HTTP2 spec, even numbers are reserved for the server to identify it's streams with and odd numbers are for the client to label it's streams with, so it is quite possible that the stream id's expire on the server side, but I'm not sure if NoAvailableStreamIDError would be raised in that case. Also, I was hoping maybe we could discuss on how to test this in the review, because I guess we'd need to spin up a MockHTTP2Server and configure it to accept only one stream id, so it gets exhausted. But I couldn't find any other tests doing this thing so I was hoping I could get some advice on how to test it here.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The test you suggested is blackbox, why not just patch H2 Connection to raise the exception?
I don't see the a lot of value in doing blackbox, especially when the exception is coming from another library (h2).

Comment thread httpx/dispatch/http2.py
await self.init_complete.wait()
stream_id = self.state.get_next_available_stream_id()
except NoAvailableStreamIDError:
self.state = h2.connection.H2Connection(config=self.CONFIG)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can't do this, since it'll break existing streams.

Getting this right is a little bit complex, and I think we should probably pause on it, pending some more comprehensive work around connection state management along the lines in #624

@lovelydinosaur

Copy link
Copy Markdown
Member

@gdhameeja Really appreciate your time on having taken a crack at this.

Given #647 (comment) I'm going to close this PR off.

Kludex referenced this pull request in pydantic/httpx2 May 11, 2026
Increase the default allowable http header size to 100kB similar to curl.

Raised in discussion encode/httpx#2520

Co-authored-by: Huy Au <hau@invast.com.au>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants