[Bug]: onSystemPrompt middleware executes on a Reactor boundedElastic scheduler thread with an uncontrollable TCCL, breaking TCCL-based resource loading (FreeMarker / hutool SPI) in Spring Boot fat-jar deployments
Describe the bug
When an Agent (ReActAgent) is invoked asynchronously, the onSystemPrompt middleware chain
executes on a Reactor boundedElastic scheduler thread whose Thread Context ClassLoader (TCCL)
is not under our control. With the platform AppClassLoader as TCCL, any middleware that
resolves classpath resources or SPI services via the TCCL fails in a Spring Boot fat-jar
deployment, even though the resources are present on the classpath.
A telling symptom: the first agent call succeeds, but subsequent calls fail. This matches
how Reactor's global Schedulers.boundedElastic() pool works — threads are created lazily and
each thread's TCCL is captured once, at creation, from the parent thread that schedules the
first task onto it, then never changes. If the pool first grows on a webapp-classloader thread
the middleware works; once it grows on an AppClassLoader thread, later calls get routed to the
"wrong-TCCL" threads and fail.
Observed failures (both TCCL-dependent):
- FreeMarker (
SpringTemplateLoader/ClassTemplateLoaderresolve viaClassUtils.getDefaultClassLoader(), which prefers the TCCL) →Template not found for name "sysPrompt/xx.md". - Hutool
PinyinUtil(engine resolved viaServiceLoader/ServiceLoaderUtil, TCCL-based) →No pinyin jar found ! Please add one of it to your project !.
To Reproduce
- Run a Spring Boot fat jar (
java -jar app.jar) with AgentScope, Java 21, WebFlux/Reactor. - Register a custom
MiddlewareBasethat overridesonSystemPromptand reads a classpath resource via the TCCL (e.g. a FreeMarker template, or anyServiceLoader-loaded SPI). - Invoke the agent asynchronously (e.g.
agent.callAsync(...)/ subscribe) in a controller. - Log the thread + TCCL inside
onSystemPrompt:
@Override
public Mono<String> onSystemPrompt(Agent agent, RuntimeContext ctx, String currentPrompt) {
System.out.println(Thread.currentThread().getName() + " TCCL="
+ Thread.currentThread().getContextClassLoader());
return Mono.just(currentPrompt);
}
- Make two or more calls. The first may run on a
boundedElastic-Nthread with the webapp class loader and succeed; a later call runs on aboundedElastic-Nthread withjdk.internal.loader.ClassLoaders$AppClassLoader, and the TCCL-dependent resource load fails.
Expected behavior
The onSystemPrompt middleware should run on the caller thread (preserving the webapp TCCL), or
the middleware chain should capture/restore the caller's TCCL, so classpath resources and SPI
services remain resolvable regardless of which pooled scheduler thread happens to run it.
Error messages
freemarker.template.Configuration: Template not found for name "sysPrompt/xxx.md".
The name was interpreted by this TemplateLoader: MultiTemplateLoader(loader1 = SpringTemplateLoader@..., loader2 = ClassTemplateLoader(...)).
cn.hutool.extra.pinyin.PinyinException: No pinyin jar found ! Please add one of it to your project !
Environment (please complete the following information):
- AgentScope-Java Version: v2.0.1
- Java Version: 21
- OS: linux (SAE) / windows
Additional context
The middleware chain itself is fully reactive (seedSystemMsg returns Mono<Msg> and
applySystemPromptMiddlewares returns Mono<String>), so its execution is deferred to the
reactive subscription point — a Reactor boundedElastic scheduler thread. Reactor's global
Schedulers.boundedElastic() pool threads are created lazily and their TCCL is fixed at
creation from the scheduling parent thread, which is why the outcome is non-deterministic and
depends on which thread happened to grow the pool. Suggested fix: run the middleware chain on
the caller's thread, or capture/restore the caller's TCCL around the chain instead of letting it
execute on an arbitrary scheduler thread.
异步调用时 onSystemPrompt 中间件在 Reactor boundedElastic 调度线程上执行,线程 TCCL 不可控,破坏 Spring Boot fat jar 部署下基于 TCCL 的资源加载(FreeMarker / hutool SPI)
问题描述(Describe the bug)
当以异步方式调用 Agent(ReActAgent)时,onSystemPrompt 中间件链路会在 Reactor boundedElastic
调度线程上执行,而该线程的线程上下文类加载器(TCCL)不受我们的控制。当 TCCL 是平台
AppClassLoader 时,任何通过 TCCL 解析 classpath 资源或 SPI 服务的中间件,在 Spring Boot
fat jar 部署下都会失败——尽管这些资源确实在 classpath 上。
一个典型的附加现象:首次 agent 调用成功,但后续调用失败。这与 Reactor 全局
Schedulers.boundedElastic() 线程池的工作方式一致——线程是懒创建的,每个线程的 TCCL 在建线程
那一刻从「首次往该池调度任务的父线程」捕获一次,之后固定不变。如果池先在 webapp 类加载器的
线程上扩容,中间件就正常;一旦池在 AppClassLoader 线程上扩容,后续调用就会被路由到这些
「错误 TCCL」的线程上而失败。
观察到的两类失败(都依赖 TCCL):
- FreeMarker(
SpringTemplateLoader/ClassTemplateLoader通过ClassUtils.getDefaultClassLoader()解析,优先取 TCCL)→Template not found for name "sysPrompt/xx.md"。 - Hutool
PinyinUtil(引擎通过ServiceLoader/ServiceLoaderUtil解析,基于 TCCL)→No pinyin jar found ! Please add one of it to your project !。
复现步骤(To Reproduce)
- 用 Spring Boot fat jar(
java -jar app.jar)+ AgentScope + Java 21 + WebFlux/Reactor 运行。 - 注册一个覆写了
onSystemPrompt的自定义MiddlewareBase,并通过 TCCL 读取 classpath 资源 (例如 FreeMarker 模板,或任何基于ServiceLoader的 SPI)。 - 在 Controller 中异步调用 agent(例如
agent.callAsync(...)/subscribe)。 - 在
onSystemPrompt里打印线程名与 TCCL:
@Override
public Mono<String> onSystemPrompt(Agent agent, RuntimeContext ctx, String currentPrompt) {
System.out.println(Thread.currentThread().getName() + " TCCL="
+ Thread.currentThread().getContextClassLoader());
return Mono.just(currentPrompt);
}
- 连续发起两次及以上调用。第一次可能跑在 TCCL 为 webapp 类加载器的
boundedElastic-N线程上 并成功;后续调用跑在 TCCL 为jdk.internal.loader.ClassLoaders$AppClassLoader的boundedElastic-N线程上,依赖 TCCL 的资源加载失败。
预期行为(Expected behavior)
onSystemPrompt 中间件应在调用方线程上执行(保留 webapp TCCL),或在中间件链路周围捕获/还原
调用方的 TCCL,使 classpath 资源和 SPI 服务无论由哪个池化调度线程执行都保持可解析。
错误信息(Error messages)
freemarker.template.Configuration: Template not found for name "sysPrompt/xxx.md".
The name was interpreted by this TemplateLoader: MultiTemplateLoader(loader1 = SpringTemplateLoader@..., loader2 = ClassTemplateLoader(...)).
cn.hutool.extra.pinyin.PinyinException: No pinyin jar found ! Please add one of it to your project !
环境(Environment)
- AgentScope-Java 版本:v2.0.1
- Java 版本:21
- 操作系统:linux(SAE)/ windows
补充上下文(Additional context)
中间件链路本身是完全响应式的(seedSystemMsg 返回 Mono<Msg>,applySystemPromptMiddlewares
返回 Mono<String>),因此它的执行被延迟到响应式订阅点——即一个 Reactor boundedElastic 调度
线程。Reactor 全局 Schedulers.boundedElastic() 池线程是懒创建的,其 TCCL 在建线程时从调度父线程
固定捕获,这就是结果不确定、取决于「池恰好由哪个线程扩容」的原因。建议修复:在调用方线程上执行
中间件链路,或在链路周围捕获/还原调用方的 TCCL,而不是让它在任意调度线程上执行。