Skip to content

Commit

Permalink
fix(client): improve garbage collection for oauth clients
Browse files Browse the repository at this point in the history
  • Loading branch information
lepture committed Jan 28, 2025
1 parent 9188e21 commit c46e939
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
3 changes: 2 additions & 1 deletion authlib/oauth1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,5 @@ def handle_error(error_type, error_description):
raise ValueError(f'{error_type}: {error_description}')

def __del__(self):
del self.session
if self.session:
del self.session
4 changes: 4 additions & 0 deletions authlib/oauth2/rfc7521/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@ def _refresh_token(self, data):
'POST', self.token_endpoint, data=data, withhold_token=True)

return self.parse_response_token(resp)

def __del__(self):
if self.session:
del self.session
13 changes: 7 additions & 6 deletions tests/clients/test_starlette/test_user_mixin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from httpx import ASGITransport
from starlette.requests import Request
from authlib.integrations.starlette_client import OAuth
from authlib.jose import JsonWebKey
Expand All @@ -16,9 +17,9 @@ async def run_fetch_userinfo(payload):
async def fetch_token(request):
return get_bearer_token()

app = AsyncPathMapDispatch({
transport = ASGITransport(AsyncPathMapDispatch({
'/userinfo': {'body': payload}
})
}))

client = oauth.register(
'dev',
Expand All @@ -27,7 +28,7 @@ async def fetch_token(request):
fetch_token=fetch_token,
userinfo_endpoint='https://i.b/userinfo',
client_kwargs={
'app': app,
'transport': transport,
}
)

Expand Down Expand Up @@ -110,9 +111,9 @@ async def test_force_fetch_jwks_uri():
)
token['id_token'] = id_token

app = AsyncPathMapDispatch({
transport = ASGITransport(AsyncPathMapDispatch({
'/jwks': {'body': read_key_file('jwks_public.json')}
})
}))

oauth = OAuth()
client = oauth.register(
Expand All @@ -123,7 +124,7 @@ async def test_force_fetch_jwks_uri():
jwks_uri='https://i.b/jwks',
issuer='https://i.b',
client_kwargs={
'app': app,
'transport': transport,
}
)
user = await client.parse_id_token(token, nonce='n')
Expand Down

0 comments on commit c46e939

Please sign in to comment.