asyncio.Lock Initialization Outside Event Loop in AgentWithMemory #10576

Open
opened 2026-04-18 17:32:39 +00:00 by HAL9000 · 0 comments
Owner

Metadata

Commit Message: Fix asyncio.Lock initialization in AgentWithMemory to support non-async contexts

Branch Name: fix/asyncio-lock-initialization

Background and Context

The AgentWithMemory class (lines 84-88 in src/cleveragents/agents/base.py) creates an asyncio.Lock in its __init__() method, which is called outside of an async context. In Python 3.10+, creating an asyncio.Lock outside an active event loop raises a RuntimeError. This breaks the AgentWithMemory class when instantiated in non-async contexts, making it impossible to use agents that inherit from this class in synchronous code.

Code Evidence

  1. Line 84-88: AgentWithMemory.__init__() calls asyncio.Lock()
  2. asyncio.Lock() requirement: Requires an active event loop to be created
  3. Error condition: If __init__() is called outside async context, RuntimeError is raised
  4. Impact scope: Affects all code that instantiates AgentWithMemory or its subclasses

Reproduction Steps

from cleveragents.agents.base import AgentWithMemory

class TestAgent(AgentWithMemory):
    async def process_message(self, message, context=None):
        return f"Processed: {message}"
    
    def get_capabilities(self):
        return ["test"]

# This will raise RuntimeError in Python 3.10+
agent = TestAgent("test-agent")
# RuntimeError: no running event loop

Expected Behavior

  • AgentWithMemory can be instantiated in both synchronous and asynchronous contexts
  • The lock mechanism works correctly when async methods are called
  • No RuntimeError is raised during initialization
  • Backward compatibility is maintained with existing code

Acceptance Criteria

  • AgentWithMemory can be instantiated outside an event loop without raising RuntimeError
  • The lock mechanism functions correctly when async methods are invoked
  • Unit tests verify initialization works in both sync and async contexts
  • No breaking changes to the public API
  • All existing tests pass
  • Code coverage remains >= 97%

Subtasks

  • Implement lazy initialization pattern for asyncio.Lock (defer creation to first async use)
  • Add unit tests for sync context initialization
  • Add unit tests for async context initialization
  • Add integration tests with agents that inherit from AgentWithMemory
  • Update docstring to clarify initialization behavior
  • Verify fix works with Python 3.10, 3.11, 3.12+

Definition of Done

This issue is complete when:

  1. AgentWithMemory can be instantiated in non-async contexts without raising RuntimeError
  2. All async lock operations work correctly when called from async methods
  3. Unit tests cover both sync and async initialization paths
  4. All existing tests pass with coverage >= 97%
  5. Code review approved
  6. Changes merged to main branch

Automated by CleverAgents Bot
Agent: new-issue-creator

## Metadata **Commit Message:** Fix asyncio.Lock initialization in AgentWithMemory to support non-async contexts **Branch Name:** fix/asyncio-lock-initialization ## Background and Context The `AgentWithMemory` class (lines 84-88 in `src/cleveragents/agents/base.py`) creates an `asyncio.Lock` in its `__init__()` method, which is called outside of an async context. In Python 3.10+, creating an `asyncio.Lock` outside an active event loop raises a `RuntimeError`. This breaks the `AgentWithMemory` class when instantiated in non-async contexts, making it impossible to use agents that inherit from this class in synchronous code. ### Code Evidence 1. **Line 84-88**: `AgentWithMemory.__init__()` calls `asyncio.Lock()` 2. **asyncio.Lock() requirement**: Requires an active event loop to be created 3. **Error condition**: If `__init__()` is called outside async context, `RuntimeError` is raised 4. **Impact scope**: Affects all code that instantiates `AgentWithMemory` or its subclasses ### Reproduction Steps ```python from cleveragents.agents.base import AgentWithMemory class TestAgent(AgentWithMemory): async def process_message(self, message, context=None): return f"Processed: {message}" def get_capabilities(self): return ["test"] # This will raise RuntimeError in Python 3.10+ agent = TestAgent("test-agent") # RuntimeError: no running event loop ``` ## Expected Behavior - `AgentWithMemory` can be instantiated in both synchronous and asynchronous contexts - The lock mechanism works correctly when async methods are called - No `RuntimeError` is raised during initialization - Backward compatibility is maintained with existing code ## Acceptance Criteria - [ ] `AgentWithMemory` can be instantiated outside an event loop without raising `RuntimeError` - [ ] The lock mechanism functions correctly when async methods are invoked - [ ] Unit tests verify initialization works in both sync and async contexts - [ ] No breaking changes to the public API - [ ] All existing tests pass - [ ] Code coverage remains >= 97% ## Subtasks - [ ] Implement lazy initialization pattern for `asyncio.Lock` (defer creation to first async use) - [ ] Add unit tests for sync context initialization - [ ] Add unit tests for async context initialization - [ ] Add integration tests with agents that inherit from `AgentWithMemory` - [ ] Update docstring to clarify initialization behavior - [ ] Verify fix works with Python 3.10, 3.11, 3.12+ ## Definition of Done This issue is complete when: 1. `AgentWithMemory` can be instantiated in non-async contexts without raising `RuntimeError` 2. All async lock operations work correctly when called from async methods 3. Unit tests cover both sync and async initialization paths 4. All existing tests pass with coverage >= 97% 5. Code review approved 6. Changes merged to main branch --- **Automated by CleverAgents Bot** Agent: new-issue-creator
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#10576
No description provided.