Skip to content

Commit 3b8e7b6

Browse files
Fix OAuth auth flow tests to properly close async generators
The retry logic fix in PR-1206 moved the final request yield inside the OAuth flow block, which now correctly retries requests after OAuth completion. However, the existing tests didn't expect this final yield and would exit the generator early, causing a GeneratorExit exception while the async lock was still held. This resulted in RuntimeError: The current task is not holding this lock. Fix by properly closing the generators in both failing tests: - test_oauth_discovery_fallback_conditions - test_auth_flow_with_no_tokens Both tests now send a final success response to properly complete the auth flow generator and prevent the lock release error.
1 parent 5f37158 commit 3b8e7b6

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

tests/client/test_auth.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,19 @@ async def test_oauth_discovery_fallback_conditions(self, oauth_provider: OAuthCl
361361
),
362362
request=token_request,
363363
)
364-
token_request = await auth_flow.asend(token_response)
364+
365+
# After OAuth flow completes, the original request is retried with auth header
366+
final_request = await auth_flow.asend(token_response)
367+
assert final_request.headers["Authorization"] == "Bearer new_access_token"
368+
assert final_request.method == "GET"
369+
assert str(final_request.url) == "https://api.example.com/v1/mcp"
370+
371+
# Send final success response to properly close the generator
372+
final_response = httpx.Response(200, request=final_request)
373+
try:
374+
await auth_flow.asend(final_response)
375+
except StopAsyncIteration:
376+
pass # Expected - generator should complete
365377

366378
@pytest.mark.anyio
367379
async def test_handle_metadata_response_success(self, oauth_provider: OAuthClientProvider):
@@ -694,6 +706,13 @@ async def test_auth_flow_with_no_tokens(self, oauth_provider: OAuthClientProvide
694706
assert final_request.method == "GET"
695707
assert str(final_request.url) == "https://api.example.com/mcp"
696708

709+
# Send final success response to properly close the generator
710+
final_response = httpx.Response(200, request=final_request)
711+
try:
712+
await auth_flow.asend(final_response)
713+
except StopAsyncIteration:
714+
pass # Expected - generator should complete
715+
697716
# Verify tokens were stored
698717
assert oauth_provider.context.current_tokens is not None
699718
assert oauth_provider.context.current_tokens.access_token == "new_access_token"

0 commit comments

Comments
 (0)