Repository navigation
Time out idle connections to the auth code receiver - #971
Open
Yashvardhan Thanvi (Thanatos9404) wants to merge 1 commit into
Open
Yashvardhan Thanvi (Thanatos9404) wants to merge 1 commit into
Yashvardhan Thanvi (Thanatos9404) wants to merge 1 commit into
Conversation
AuthCodeReceiver serves one connection at a time with HTTPServer.handle_request(). _AuthCodeHandler had no read timeout, so a client that connected and never sent a request blocked handle_request() inside _get_auth_response(). The browser's form_post callback then waited in the listen backlog until the overall timeout, or forever when there was none. This was seen with az login behind VS Code port forwarding (Remote SSH and Dev Containers). Give _AuthCodeHandler a timeout of 10 seconds. StreamRequestHandler applies it to the accepted socket, and handle_one_request() closes the connection when a read times out, so handle_request() returns and the loop accepts the next connection. A client delivering the auth response sends it right after connecting, so only idle connections are affected. Each idle connection queued ahead of the callback now delays it by up to 10 seconds instead of blocking it. Fixes AzureAD#950
Yashvardhan Thanvi (Thanatos9404)
requested a review
from a team
as a code owner
October 15, 2026 09:26
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #950
AuthCodeReceiverhandles one connection at a time throughHTTPServer.handle_request(), and_AuthCodeHandlerhad no read timeout. A client that connects and never sends a request therefore blockshandle_request()inside_get_auth_response(), and the browser'sform_postcallback sits in the listen backlog behind it.get_auth_response()then returns None at the overall timeout, or never returns when there is none, which is the default foracquire_token_interactive(). The reporter hit this behind VS Code Remote SSH port forwarding, and Daniel Gil (@DanielGilOrtiz) reports it in a VS Code Dev Container and suggests this fix.This sets
timeout = 10on_AuthCodeHandler.StreamRequestHandler.setup()applies it to each accepted socket, andBaseHTTPRequestHandler.handle_one_request()already treats a read timeout as a closed connection. Sohandle_request()returns and the loop accepts the queued callback. The overalltimeoutofget_auth_response()is unchanged.A client delivering the auth response sends it right after connecting, so in practice only idle connections hit this. Connections a browser opens speculatively and leaves unused are idle too, and are dropped the same way. The limit applies to each read, so a request that is slow to arrive still gets through as long as no single gap reaches 10 seconds.
Idle connections are still handled one after another, so each one queued ahead of the callback adds up to 10 seconds: one idle connection delays the sign-in by about 10 seconds and two by about 20, instead of blocking it. Serving connections in parallel would avoid that, but
_get_auth_response()only checks for the response afterhandle_request()returns, so that is a bigger change and is left out here. The value is a class attribute rather than a new parameter, so the public API does not change. Happy to make it configurable if you prefer.Tests
Two new tests in
tests/test_authcode.py. Each opens raw sockets to 127.0.0.1 that never send anything, then posts the auth response from a thread one second later.test_idle_connection_should_not_block_auth_response: one idle connection, default timeout. Ondevit fails after 30 s becauseget_auth_response()returns None. With this change it passes in 10 to 11 s.test_several_idle_connections_should_not_block_auth_response: three idle connections, with the handler timeout patched to 1 s to keep the test short. It checks that the callback still arrives (about 4 s). Since it sets the timeout itself, it passes ondevtoo.The unit test command from
.Pipelines/template-pipeline-stages.ymlpasses on Linux and Windows (Python 3.12); the two authcode test files also pass on Windows with Python 3.9 and 3.14.Also checked with a small socket script: two and three idle connections (response after about 21 s and 31 s), no overall timeout, a client that waits 9 s before sending, and a request that stops halfway. Not tried with a real browser or
az login.