[BUG] Token renewal is not working
jerosa opened this issue · comments
Hi!
I have a daemon application that needs to call an API endpoint every X seconds and whenever the token expires the application stop with the following error:
File "/usr/local/lib/python3.8/site-packages/httpx/_client.py", line 787, in request
return self.send(
File "/usr/local/lib/python3.8/site-packages/httpx/_client.py", line 878, in send
response = self._send_handling_auth(
File "/usr/local/lib/python3.8/site-packages/httpx/_client.py", line 905, in _send_handling_auth
request = next(auth_flow)
File "/usr/local/lib/python3.8/site-packages/httpx/_auth.py", line 67, in sync_auth_flow
request = next(flow)
File "/usr/local/lib/python3.8/site-packages/httpx_auth/authentication.py", line 284, in auth_flow
token = OAuth2.token_cache.get_token(
File "/usr/local/lib/python3.8/site-packages/httpx_auth/oauth2_tokens.py", line 132, in get_token
new_token = on_missing_token(**on_missing_token_kwargs)
File "/usr/local/lib/python3.8/site-packages/httpx_auth/authentication.py", line 294, in request_new_token
token, expires_in = request_new_grant_with_post(
File "/usr/local/lib/python3.8/site-packages/httpx_auth/authentication.py", line 65, in request_new_grant_with_post
with client:
File "/usr/local/lib/python3.8/site-packages/httpx/_client.py", line 1239, in __enter__
raise RuntimeError(msg)
RuntimeError: Cannot reopen a client instance, once it has been closed.
Looking at the implementation of your client it seems you are using a context manager for managing it. This works correctly only once, because if you use the same httpx client when it enter this function again, the context manager has already closed the connection and that's why the error occurs.
To reproduce the issue just create an httpx client without a context manager and wait till the renewal needs to be done.
Thanks for the report @jerosa I am unfortunately not available this week-end but I will look into it next week
I plan to work on it on monday, hopefully releasing a bugfix on the same day.
Release 0.12.0 is now available on pypi.org and should fix this issue.