Skip to content
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

[BUG] Token renewal is not working #34

Closed
jerosa opened this issue Oct 13, 2021 · 3 comments · Fixed by #36
Closed

[BUG] Token renewal is not working #34

jerosa opened this issue Oct 13, 2021 · 3 comments · Fixed by #36
Assignees
Labels
bug Something isn't working
Milestone

Comments

@jerosa
Copy link

jerosa commented Oct 13, 2021

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.

@Colin-b
Copy link
Owner

Colin-b commented Oct 22, 2021

Thanks for the report @jerosa I am unfortunately not available this week-end but I will look into it next week

@Colin-b Colin-b added the bug Something isn't working label Oct 22, 2021
@Colin-b Colin-b self-assigned this Oct 22, 2021
@Colin-b
Copy link
Owner

Colin-b commented Oct 29, 2021

I plan to work on it on monday, hopefully releasing a bugfix on the same day.

@Colin-b Colin-b added this to the 0.11.1 milestone Oct 29, 2021
@Colin-b Colin-b linked a pull request Nov 1, 2021 that will close this issue
@Colin-b
Copy link
Owner

Colin-b commented Nov 1, 2021

Release 0.12.0 is now available on pypi.org and should fix this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants