fix(skill): ensure thread-safe file uploads in SkillBox using fine-grained path locks#1109
Open
jujn wants to merge 4 commits intoagentscope-ai:mainfrom
Open
fix(skill): ensure thread-safe file uploads in SkillBox using fine-grained path locks#1109jujn wants to merge 4 commits intoagentscope-ai:mainfrom
jujn wants to merge 4 commits intoagentscope-ai:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a concurrency issue where multiple SkillBox instances can concurrently overwrite the same uploaded skill files, by introducing per-target-path synchronization and adding a regression test that exercises concurrent uploads.
Changes:
- Add a static per-file-path lock map in
SkillBox.uploadSkillFiles()and synchronize writes per target file. - Add a multi-threaded JUnit test to validate concurrent uploads do not throw and do not corrupt/truncate file contents.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| agentscope-core/src/main/java/io/agentscope/core/skill/SkillBox.java | Serializes file writes per resolved target path to prevent concurrent overwrite corruption. |
| agentscope-core/src/test/java/io/agentscope/core/skill/SkillBoxTest.java | Adds a concurrent upload test to reproduce/prevent the reported race. |
Comments suppressed due to low confidence (2)
agentscope-core/src/main/java/io/agentscope/core/skill/SkillBox.java:59
FILE_LOCKSis a static map that only ever grows (one entry per unique uploaded file path) and is never cleaned up. If agents use different work dirs (e.g., default temp dirs), this can cause unbounded memory growth over a long-running process. Consider using lock striping (fixed array of locks) or using aReentrantLockper path with safe cleanup when no threads are queued (e.g., remove after unlock when uncontended).
public SkillBox(Toolkit toolkit) {
this(toolkit, null, null);
agentscope-core/src/main/java/io/agentscope/core/skill/SkillBox.java:781
- The per-file lock is held while doing Base64 decoding, which increases contention for concurrent uploads to the same file. Decode the content outside the synchronized section and only synchronize the actual filesystem write to minimize time under the lock.
fileCount++;
} catch (IOException | IllegalArgumentException e) {
logger.error("Failed to upload file {}: {}", resourcePath, e.getMessage());
}
}
agentscope-core/src/test/java/io/agentscope/core/skill/SkillBoxTest.java
Outdated
Show resolved
Hide resolved
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Close #1096
Checklist
Please check the following items before code is ready to be reviewed.
mvn spotless:applymvn test)