Skip to content

Fix authentication event loop corruption by converting get_current_user to async #40

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

Merged
merged 3 commits into from
Jul 26, 2025

Conversation

abrookins
Copy link
Collaborator

The get_current_user() function was using asyncio.run() within FastAPI's async context, which creates a new event loop and causes "Event loop is closed" errors. This led to intermittent authentication failures where requests would alternate between success (200) and failure (500).

Changes:

  • Convert get_current_user() from sync to async function
  • Replace asyncio.run(verify_token()) with await verify_token()
  • Update require_scope() and require_role() dependency functions to be async
  • Fix all related test cases to use await when calling these functions

This resolves the issue where tool calls to the memory server would intermittently fail with 500 errors during token verification.

🤖 Generated with Claude Code

…er to async

The get_current_user() function was using asyncio.run() within FastAPI's async context,
which creates a new event loop and causes "Event loop is closed" errors. This led to
intermittent authentication failures where requests would alternate between success (200)
and failure (500).

Changes:
- Convert get_current_user() from sync to async function
- Replace asyncio.run(verify_token()) with await verify_token()
- Update require_scope() and require_role() dependency functions to be async
- Fix all related test cases to use await when calling these functions

This resolves the issue where tool calls to the memory server would intermittently fail
with 500 errors during token verification.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@Copilot Copilot AI review requested due to automatic review settings July 26, 2025 00:49
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an authentication event loop corruption issue by converting get_current_user() from a synchronous to an asynchronous function. The core problem was that the function was using asyncio.run() within FastAPI's existing async context, causing "Event loop is closed" errors and intermittent authentication failures.

  • Convert get_current_user() to async and replace asyncio.run() with await
  • Update require_scope() and require_role() dependency functions to be async
  • Fix all test cases to properly await the converted async functions

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
agent_memory_server/auth.py Converts authentication functions to async and removes problematic asyncio.run() call
tests/test_token_auth.py Updates test cases to use await with converted async functions and adds proper async decorators
tests/test_auth.py Updates existing test cases to use await with the now-async authentication functions

abrookins and others added 2 commits July 25, 2025 17:50
Version bump to reflect the authentication bug fix that resolves
intermittent 500 errors during token verification.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@abrookins abrookins merged commit 10029b0 into main Jul 26, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant