timeouts are not set in requests inside a custom authentication flow #3679
Replies: 1 comment
|
You've hit on an important architectural nuance between Why this happensIn HTTPX, timeouts are communicated to the transport layer via the request.extensions["timeout"] = {
"connect": 5.0,
"read": 5.0,
"write": 5.0,
"pool": 5.0
}When you call However, when an Recommended Solutions1. Inherit the parent request's extensions (Recommended)The cleanest way to ensure your auth request respects whatever timeout (and trace hooks) the caller configured on the client is to copy class MyAuth(httpx.Auth):
def auth_flow(self, request):
token_request = httpx.Request(
"POST",
"https://auth.example.com/token",
data={"grant_type": "client_credentials"},
extensions=dict(request.extensions) # <--- Preserves timeout & trace hooks
)
token_response = yield token_request
token = token_response.json()["access_token"]
request.headers["Authorization"] = f"Bearer {token}"
yield request2. Set an explicit
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
the problem is that whenever I have a custom authentication that yields some requests, those requests does not have a timeout set. sometimes the underlying authentication flow stalls the whole request.
see the
timeout=Nonehereciting the documentation on timeouts
I am raising this as suggested by the bug template but I think it's definitely a bug.
I know the underlying problem is caused because the custom auth class is not aware of the underlying client and the request is missing the
request.extensions={'timeout': {...}}but this exposes the bigger problem has been raised in https://github.com/encode/httpx/issues/3383 the custom auth class being too decoupled from the clientcode example reproducing the issue
workaround is to copy original extensions which was build by the client
httpx.Request(..., extensions=request.extensions)full log
All reactions