-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(session): prevent concurrent commit re-committing old messages #783
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
Open
deepakdevp
wants to merge
4
commits into
volcengine:main
Choose a base branch
from
deepakdevp:fix/session-commit-race
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+98
−8
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
55d7f58
fix(session): add commit lock to prevent concurrent re-commit race
deepakdevp c06b094
test(session): add race condition tests for concurrent commit
deepakdevp 44593c1
refactor(session): use PathLock instead of asyncio.Lock for commit guard
deepakdevp 2f85807
perf(session): add fast pre-check before PathLock for empty sessions
deepakdevp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Tests for session commit race condition fix (#580).""" | ||
|
|
||
| import asyncio | ||
|
|
||
| from openviking import AsyncOpenViking | ||
| from openviking.message import TextPart | ||
|
|
||
|
|
||
| class TestCommitRace: | ||
| """Test concurrent commit safety.""" | ||
|
|
||
| async def test_concurrent_commit_no_duplicate(self, client: AsyncOpenViking): | ||
| """Two concurrent commits on the same session: only one should archive.""" | ||
| session = client.session(session_id="race_test_dedup") | ||
| session.add_message("user", [TextPart("Hello")]) | ||
| session.add_message("assistant", [TextPart("Hi there")]) | ||
|
|
||
| results = await asyncio.gather( | ||
| session.commit_async(), | ||
| session.commit_async(), | ||
| ) | ||
|
|
||
| archived_count = sum(1 for r in results if r.get("archived") is True) | ||
| assert archived_count == 1, f"Expected exactly 1 archived commit, got {archived_count}" | ||
|
|
||
| # Messages should be cleared after commit | ||
| assert len(session.messages) == 0 | ||
|
|
||
| # Compression index should have incremented exactly once | ||
| assert session._compression.compression_index == 1 | ||
|
|
||
| async def test_message_added_during_commit_not_lost(self, client: AsyncOpenViking): | ||
| """Messages added while commit is running should not be lost.""" | ||
| session = client.session(session_id="race_test_msg_safety") | ||
| session.add_message("user", [TextPart("Original message")]) | ||
|
|
||
| # Use an Event for deterministic synchronization instead of sleeps | ||
| phase1_done = asyncio.Event() | ||
| original_generate = session._generate_archive_summary_async | ||
|
|
||
| async def slow_generate(messages): | ||
| # Signal that Phase 1 is complete (lock released, messages cleared) | ||
| phase1_done.set() | ||
| # Yield control so add_message can run before archive completes | ||
| await asyncio.sleep(0) | ||
| return await original_generate(messages) | ||
|
|
||
| session._generate_archive_summary_async = slow_generate | ||
|
|
||
| async def commit_and_add(): | ||
| """Start commit, then add a message after Phase 1 completes.""" | ||
| commit_task = asyncio.create_task(session.commit_async()) | ||
| # Wait until Phase 1 is done (lock released, messages cleared) | ||
| await phase1_done.wait() | ||
| # Add message while commit is in Phase 2 (after lock released) | ||
| session.add_message("user", [TextPart("New message during commit")]) | ||
| return await commit_task | ||
|
|
||
| result = await commit_and_add() | ||
|
|
||
| assert result.get("archived") is True | ||
| # The new message should still be in the session | ||
| assert len(session.messages) == 1 | ||
| assert session.messages[0].content == "New message during commit" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small thing — the old code returned early on empty
_messagesbefore acquiring any lock. Now everycommit_async()call on an empty session takes a filesystemPathLockjust to check the list length and return. Probably negligible in practice, but if something is callingcommit_asyncfrequently (e.g. a keep-alive or periodic flush), the lock contention could add up. Worth moving the empty check back above theasync with, or is there a reason it needs to be inside the lock now?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — applied a double-check locking pattern in 2f85807. A fast pre-check for
not self._messagesnow sits above theasync with LockContext, so empty sessions skip the filesystem lock entirely (common case, zero I/O). The authoritative check inside the lock is still there to handle the race where two concurrent callers both pass the pre-check but only the first should archive. Thanks for the suggestion!