Skip to content

fix(memory): serialize concurrent writes in AsyncSQLAlchemyMemory#1390

Open
umerkhan95 wants to merge 1 commit intoagentscope-ai:mainfrom
umerkhan95:fix/async-memory-concurrent-writes
Open

fix(memory): serialize concurrent writes in AsyncSQLAlchemyMemory#1390
umerkhan95 wants to merge 1 commit intoagentscope-ai:mainfrom
umerkhan95:fix/async-memory-concurrent-writes

Conversation

@umerkhan95
Copy link
Copy Markdown

AgentScope Version

1.0.18

Description

Fixes #1381AsyncSQLAlchemyMemory raises IntegrityError when ReActAgent runs with parallel_tool_calls=True because multiple _acting() coroutines call memory.add() concurrently on a shared AsyncSession.

This PR adds an asyncio.Lock wrapped in a _write_session() async context manager that serializes all mutating session operations (add, clear, delete, delete_by_mark, update_messages_mark) with automatic commit/rollback. Internal helpers remain lock-free since they're only called from within _write_session(). Read-only methods are left unlocked.

Includes test_concurrent_add that fires 20 concurrent asyncio.gather-based add() calls and verifies message count and index contiguity.

Checklist

  • Code has been formatted with pre-commit run --all-files command
  • All tests are passing
  • Docstrings are in Google style
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

Add asyncio.Lock via a _write_session() context manager to serialize
all mutating operations on the shared AsyncSession. This prevents the
IntegrityError caused by concurrent _acting() coroutines in ReActAgent
when parallel_tool_calls=True (agentscope-ai#1381).
@cla-assistant
Copy link
Copy Markdown

cla-assistant bot commented Mar 31, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Copy Markdown
Member

@qbc2016 qbc2016 left a comment

Choose a reason for hiding this comment

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

Please sign our CLA and merge the latest main branch to pass the unittests.

@umerkhan95
Copy link
Copy Markdown
Author

I am not able to authenticate the license agreement, its really buggy and does not authenticate through my Github account, I tried multiple times but the trigger to authentication is not hitting.

The error: upstream connect error or disconnect/reset before headers. reset reason: connection termination

This error comes after I authenticate to the license agreement.

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.

[Bug]:Duplicate Entry Error in AsyncSQLAlchemyMemory with parallel_tool_calls=True

2 participants