fix(core): make JsonSchemaUtils schema generation thread-safe - #2796
fix(core): make JsonSchemaUtils schema generation thread-safe#2796jnduan wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
88c2470 to
8882daa
Compare
|
The failing check is unrelated to this PR. I dug into the job log:
Looks like a flaky test. Could someone with permissions re-run the failed jobs? Thanks! |
8882daa to
15859a9
Compare
|
I ran JAIPilot Cloud against this exact PR head. It found one follow-up: cache generated JSON schemas by Class/Type so repeated structured-output and tool schemas do not serialize on SCHEMA_LOCK and rerun reflective generation. In the managed sandbox, the identical 16-thread, 80,000-call workload over two repeated classes moved from a 20,323 ms median to 899 ms across five observations per side. Four behavior locks preserve schema equality, independent returned-map mutation, and null handling; downstream structured-output tests and mvn -pl agentscope-core -am verify passed. PR directly onto this source branch: jnduan#1 The cache retains one entry per encountered Class/Type, so this is optional and the lifecycle assumption should be reviewed for dynamic class-loading environments. No absolute production-latency claim is intended. |
15859a9 to
16a0623
Compare
|
Thanks for the analysis! Schema caching is indeed the planned follow-up — this PR was deliberately kept to the minimal synchronization fix (single concern), and caching will be submitted as a separate enhancement once this lands. The per-class lifecycle note is a good point to factor into that work. |
合并 PR agentscope-ai#2796,为共享 SchemaGenerator 的两个生成入口增加同步保护,并补充并发回归测试。
7761e0c to
186f878
Compare
The shared static victools SchemaGenerator is not thread-safe: its JacksonModule keeps an unsynchronized introspection cache, so concurrent structured calls can fail with ConcurrentModificationException. Guard both schemaGenerator.generateSchema(...) call sites with a dedicated lock.
186f878 to
35080eb
Compare
AgentScope-Java Version
2.0.3-SNAPSHOT (latest
main, commit0de9ed43)Description
Fixes #2795
JsonSchemaUtilsshares a single static victoolsSchemaGeneratoracross theJVM without synchronization. victools'
SchemaGeneratoris not thread-safe(its
JacksonModulekeeps an unsynchronizedMap<Class, BeanDescription>introspection cache), so concurrent structured calls — e.g. multiple agents
running in parallel with the same target class — race on the shared generator
and can fail with a
ConcurrentModificationException. The linked issue has thefull root-cause analysis, including the identical bug fixed in Spring AI
(spring-ai#6207, resolved with the same synchronized-guard approach).
Changes:
SCHEMA_LOCKand guarded the twoschemaGenerator.generateSchema(...)call sites(
generateSchemaFromClass/generateSchemaFromType). Public API isunchanged.
240 tasks released simultaneously via
CountDownLatch, alternating betweenmultiple POJOs so the victools introspection cache starts cold. Any exception
propagates through
Future#getand fails the test; all results arevalidated.
Note: the pre-fix race window is narrow and cannot be reproduced
deterministically, so the tests are regression protection rather than a
reproducer.
mvn -pl agentscope-core test: 2277 tests, 0 failures.Checklist
Please check the following items before code is ready to be reviewed.
mvn spotless:applymvn test)