embabel-agent icon indicating copy to clipboard operation
embabel-agent copied to clipboard

Invasive configuration: Virtual Threads

Open stefanks-px opened this issue 6 months ago • 20 comments

In the agent-application.properties virtual threads are enabled by default.

spring.threads.virtual.enabled=true

This might cause problems for consumers of the framework. The applications themselves should decide whether to use virtual threads or not.

stefanks-px avatar May 29 '26 14:05 stefanks-px

@stefanks-px - thank you.
Per my understanding, you can always override it in:

  • application.properties
  • application.yaml

Like any other popety overrides.

igordayen avatar May 29 '26 16:05 igordayen

@stefanks-px - could you please let us know if you are ok with overrides? thank you

igordayen avatar Jun 04 '26 22:06 igordayen

@stefanks-px - please advise, otherwise planning to close this issue, thank you

igordayen avatar Jun 06 '26 19:06 igordayen

@stefanks-px - I'm closing this issue. Feel free to re-open if you think the issue requires additional attention. thank you

igordayen avatar Jun 08 '26 17:06 igordayen

@igordayen Sorry for the late reply. Couldn't make it last week.

I still think that you should remove this property. Yes, we can override it but it might catch people off-guard. We have experimented with virtual threads in the past and had some issues.

In my opinion, a framework should not decide this by default for the using applications.

stefanks-px avatar Jun 08 '26 17:06 stefanks-px

@stefanks-px - thank you. Could you please elaborate on your experience, and where you think override is problematic?

igordayen avatar Jun 08 '26 17:06 igordayen

@igordayen Yes sure. Override is a solution to the problem, which should not exist in the first place.

Semantically speaking, Embabel assumes that activating virtual threads for Spring is fine, but this assumption might be wrong. So responsibility to make the override is shifted to the consumer of the framework.

Without digging into the Embabel framework, the consumer does not even notice, that Embabel changed a central configuration of the Spring application, which might require a override.

In our case, we had problems with virtual threads in the past and decided to not activate them -> Keep the default -> Property was never set. At the moment, we have some application running with virtual threads active in production as Embabel set them to active. We couldn't guard or validate the change because we didn't even notice until recently, that Embabel activated virtual threads.

stefanks-px avatar Jun 08 '26 17:06 stefanks-px

@stefanks-px - which model do you think would be proper:

  1. if target larger app sets virtual threads use it (inherit from app)
  2. if app does not set explicitly, use "embabel. virtual.threads"

This would guarantee a single model gets used across applications. Theoretically, two-model support is possible, but its getting more complex.

 // App's choice wins, embabel follows by default                                                                                                                                            
  return environment.getProperty("spring.threads.virtual.enabled", Boolean::class.java)                                                                                                       
      ?: properties. embabel. virtual.enabled                                                                                                                                                           
      ?: true                                                                                                                                                                                 

Pros:

  • Simple, clear priority
  • App controls embabel by default (good for compatibility)
  • Embabel can still set its own default

Cons:

  • Less flexible (embabel can't override if app sets spring property)
  • if app sets s_pring.threads.virtual.enabled=false_ for legacy reasons, embabel is stuck with platform threads even if it would benefit from virtual

is your app on Java 8-17?

Looping @alexheifetz , @poutsma, @simeshev

igordayen avatar Jun 08 '26 18:06 igordayen

@igordayen My opinion is that embabel should just follow the integrating application. Does embabel require the use of virtual threads or do you see any problem with this approach?

We are currently on Java 21

stefanks-px avatar Jun 09 '26 11:06 stefanks-px

@stefanks-px thank you. Is your app sharing the same thread executor as Embabel? Do you use any parallelization besides internal to Embabel? In other words, do you require thread pool isolation from Embabel?

igordayen avatar Jun 09 '26 14:06 igordayen

Embabel Threading Configuration: Analysis & Proposal

@stefanks-px My earlier proposal is detailed below.

Problem Statement

Embabel currently ships with spring.threads.virtual.enabled=true in its library properties. This creates an issue:

Developer Feedback:
"Override is a solution to the problem, which should not exist in the first place. Semantically speaking, Embabel assumes that activating virtual threads for Spring is fine, but this
assumption might be wrong. Without digging into the Embabel framework, the consumer does not even notice that Embabel changed a central configuration of the Spring application."

Impact:

  • Embabel forces virtual threads on the entire host application
  • Users who explicitly disabled virtual threads (due to past issues) are unknowingly running with them in production
  • This violates library best practices (libraries shouldn't make application-wide configuration decisions)

The Two-User Camps

Camp 1: Conservative/Traditional

  • Explicitly sets spring.threads.virtual.enabled=false OR relies on Spring's default (false)
  • Had issues with virtual threads, doesn't trust them
  • Wants platform threads
  • Currently unhappy - embabel overrides their decision

Camp 2: Progressive/Modern

  • Wants virtual threads for performance
  • May set spring.threads.virtual.enabled=true OR expects modern frameworks to use optimal settings
  • Currently happy - embabel gives good performance by default
  • Would be unhappy if default performance regresses

Note: Embabel's ConcurrentAgentProcess spawns one thread per parallel action with blocking LLM calls - this workload strongly benefits from virtual threads.


Option 1: CURRENT BEHAVIOR (Status Quo)

Configuration

What embabel ships with:
embabel's agent-application.properties :

  • spring.threads.virtual.enabled=true ← Embabel sets application-wide property

Decision Matrix

  ┌──────────────────────────────────────┬────────────────────┬─────────────────────┬───────────────┬─────────────────────────────────────┐                                                   
  │            What App Sets             │   Embabel Result   │    Camp 1 Happy?    │ Camp 2 Happy? │               Issue?                │                                                   
  ├──────────────────────────────────────┼────────────────────┼─────────────────────┼───────────────┼─────────────────────────────────────┤                                                   
  │ spring.threads.virtual.enabled=false │ Virtual threads ❌ │ ❌ NO - Overridden! │ ✅ YES        │ CONFLICT - This is the complaint    │                                                   
  ├──────────────────────────────────────┼────────────────────┼─────────────────────┼───────────────┼─────────────────────────────────────┤                                                   
  │ spring.threads.virtual.enabled=true  │ Virtual threads ✅ │ ✅ YES              │ ✅ YES        │ No issue                            │                                                   
  ├──────────────────────────────────────┼────────────────────┼─────────────────────┼───────────────┼─────────────────────────────────────┤                                                   
  │ Nothing (rely on Spring default)     │ Virtual threads ✅ │ ❌ NO - Forced      │ ✅ YES        │ Camp 1 doesn't get Spring's default │                                                   
  └──────────────────────────────────────┴────────────────────┴─────────────────────┴───────────────┴─────────────────────────────────────┘                                                   

Analysis

Pros:

  • ✅ Camp 2 gets optimal performance automatically
  • ✅ No configuration needed for best performance

Cons:

  • ❌ Interferes with application-wide Spring configuration
  • ❌ Overrides user's explicit choices
  • ❌ Violates library best practices
  • ❌ Users unaware embabel is changing app settings

Verdict: ❌ Unacceptable - This is the root cause of the complaint


Option 2: EMBABEL FOLLOWS APP ONLY (No embabel property)

Configuration

What embabel ships with:
embabel's agent-application.properties :

  • (spring.threads.virtual.enabled removed - no embabel-specific property)

Code logic:

  val useVirtual = environment.getProperty("spring.threads.virtual.enabled", Boolean::class.java)                                                                                             
      ?: false  // Match Spring's current default      

Decision Matrix

  ┌──────────────────────────────────────┬─────────────────────┬───────────────┬─────────────────────────┬───────────────────────────────────┐                                                
  │            What App Sets             │   Embabel Result    │ Camp 1 Happy? │      Camp 2 Happy?      │              Issue?               │                                                
  ├──────────────────────────────────────┼─────────────────────┼───────────────┼─────────────────────────┼───────────────────────────────────┤                                                
  │ spring.threads.virtual.enabled=false │ Platform threads ✅ │ ✅ YES        │ ✅ YES (if they set it) │ No issue                          │                                                
  ├──────────────────────────────────────┼─────────────────────┼───────────────┼─────────────────────────┼───────────────────────────────────┤                                                
  │ spring.threads.virtual.enabled=true  │ Virtual threads ✅  │ ✅ YES        │ ✅ YES                  │ No issue                          │                                                
  ├──────────────────────────────────────┼─────────────────────┼───────────────┼─────────────────────────┼───────────────────────────────────┤                                                
  │ Nothing (rely on Spring default)     │ Platform threads ✅ │ ✅ YES        │ ❌ NO - Slow by default │ Camp 2 must read docs & configure │                                                
  └──────────────────────────────────────┴─────────────────────┴───────────────┴─────────────────────────┴───────────────────────────────────┘                                                

Analysis

Pros:

  • ✅ Clean, simple implementation
  • ✅ No interference with application configuration
  • ✅ Respects application's threading decision
  • ✅ Camp 1 is satisfied

Cons:

  • ❌ Camp 2 loses "just works fast" benefit
  • ❌ Users must understand virtual threads and configure manually
  • ❌ Embabel appears slower than competitors by default
  • ❌ Falls into "pit of mediocrity" instead of "pit of success"

Verdict: ⚠️ Simple but pessimistic - Fixes complaint but hurts user experience for modern apps


Option 3: THREE-LEVEL PRIORITY (Recommended)

Configuration

What embabel ships with:
embabel's agent-application.properties :
embabel.threads.virtual.enabled=true ← Embabel's preference (different property!)

NOTE: NOT setting spring.threads.virtual.enabled

Code logic:

  val useVirtual = environment.getProperty("spring.threads.virtual.enabled", Boolean::class.java)                                                                                             
      ?: properties.virtual.enabled  // embabel.threads.virtual.enabled                                                                                                                       
      ?: true  // Final fallback      

Priority hierarchy:

  1. spring.threads.virtual.enabled (application's choice - always wins)
  2. embabel.threads.virtual.enabled (library's default preference)
  3. true (hardcoded fallback)

Decision Matrix

  ┌──────────────────────────────────────┬─────────────────────┬───────────────────────────────────────┬──────────────────────────┬───────────────────────────────────────────┐               
  │            What App Sets             │   Embabel Result    │             Camp 1 Happy?             │      Camp 2 Happy?       │                  Issue?                   │               
  ├──────────────────────────────────────┼─────────────────────┼───────────────────────────────────────┼──────────────────────────┼───────────────────────────────────────────┤               
  │ spring.threads.virtual.enabled=false │ Platform threads ✅ │ ✅ YES - App wins                     │ ✅ YES - App respected   │ No issue                                  │               
  ├──────────────────────────────────────┼─────────────────────┼───────────────────────────────────────┼──────────────────────────┼───────────────────────────────────────────┤               
  │ spring.threads.virtual.enabled=true  │ Virtual threads ✅  │ ✅ YES - App wins                     │ ✅ YES - App respected   │ No issue                                  │               
  ├──────────────────────────────────────┼─────────────────────┼───────────────────────────────────────┼──────────────────────────┼───────────────────────────────────────────┤               
  │ Nothing (rely on defaults)           │ Virtual threads ✅  │ ⚠️ Can override with embabel...=false │ ✅ YES - Fast by default │ Camp 1 needs embabel property to override │               
  └──────────────────────────────────────┴─────────────────────┴───────────────────────────────────────┴──────────────────────────┴───────────────────────────────────────────┘               

Analysis

Pros:

  • ✅ No interference with application-wide Spring configuration (uses separate property)
  • ✅ Application's spring... property always wins if set
  • ✅ Camp 2 gets optimal performance by default
  • ✅ Camp 1 can override with either property
  • ✅ Clear, explicit priority hierarchy
  • ✅ Follows "pit of success" principle

Cons:

  • ⚠️ Slightly more complex (introduces embabel.threads.virtual.enabled property)
  • ⚠️ Camp 1 must learn about embabel-specific property to override (if not using spring property)
  • ⚠️ Documentation needed to explain priority

Verdict: ✅ Recommended - Best balance between library best practices and user experience


Configuration Examples (Option 3)

Camp 1: Wants platform threads everywhere

application.properties

spring.threads.virtual.enabled=false ← Forces platform for app AND embabel
Result: App uses platform threads, embabel uses platform threads ✅


Camp 1: Wants platform threads only for embabel

application.properties

spring.threads.virtual.enabled=true ← App uses virtual threads
embabel.threads.virtual.enabled=false ← Embabel uses platform threads
Result: App uses virtual threads, embabel uses platform threads ✅


Camp 2: Wants optimal performance (default)

application.properties:

  • (nothing - embabel's default gives virtual threads)
    Result: App follows Spring default (platform), embabel uses virtual threads ✅

Camp 2: Wants virtual threads everywhere

application.properties:
spring.threads.virtual.enabled=true ← Forces virtual for app AND embabel
Result: App uses virtual threads, embabel uses virtual threads ✅

Threading Configuration

Embabel defaults to virtual threads (embabel.threads.virtual.enabled=true)
for optimal performance with parallel agent operations.

Priority

  1. spring.threads.virtual.enabled (your application's setting - always wins)
  2. embabel.threads.virtual.enabled (embabel's default)
  3. true (if neither is set)

To use platform threads

Option 1: Force platform threads for entire application                                                                                                                                   
spring.threads.virtual.enabled=false                                                                                                                                                        
                                                                                                                                                                                            
Option 2: Platform threads only for embabel                                                                                                                                               
embabel.threads.virtual.enabled=false                                                                                                                                                       
                                                                                                                                                                                            
Why virtual threads?                                                                                                                                                                        
                                                                                                                                                                                            
Embabel's ConcurrentAgentProcess spawns many parallel threads for LLM calls                                                                                                                 
and tool executions. Virtual threads provide better resource efficiency and                                                                                                                 
scalability for this workload.                                                                                                                  
                                                                      

igordayen avatar Jun 11 '26 21:06 igordayen

I concur with @stefanks-px: enabling virtual threads is the responsibility of an application; not that of a framework or library. There is a reason why Spring Boot has not enabled the setting by default, and I don't see a reason why we should know better.

poutsma avatar Jun 15 '26 09:06 poutsma

@poutsma , @stefanks-px - thanks for the feedback.

still inquiring on: should we have embabel's own executor (for isolation from app's thread pool)

igordayen avatar Jun 15 '26 13:06 igordayen

@igordayen, I think we should own our own virtual thread executor, here is why:

1. Every asyncer call site is the same workload

There are exactly seven production call sites to asyncer across the codebase:

Call site What it blocks on
ConcurrentAgentProcess:120 LLM HTTP call per concurrent action
ChatClientLlmOperations:350, 495 LLM HTTP call (structured output, streaming)
AbstractLlmOperations:104 LLM HTTP call with timeout
ParallelToolLoop:127 Parallel tool execution (I/O: web, files, APIs)
AgentPlatform:125 Background agent process (itself a chain of I/O)

Every single one submits an I/O-bound task and then blocks waiting for the result. Executors.newVirtualThreadPerTaskExecutor() is the correct executor for exactly this pattern - one virtual thread per submitted task, unmounted from its carrier the moment it blocks.

2. The current fallback is platform threads

AsyncConfiguration.kt:36–38:

val executor = executorProvider.getIfAvailable {
    Executors.newCachedThreadPool() // default fallback, Consider Executors.newVirtualThreadPerTaskExecutor()
}

If the host application has not configured Spring's applicationTaskExecutor, Embabel silently gets platform threads. Right now, Embabel's concurrency efficiency depends on what the host app happens to configure - that is an invisible dependency that may impact the frameworks operations.

3. Sharing the app executor creates two-way interference

Embabel's workload does not need to compete with application work. These are structurally different: the app's pool is general-purpose, typically CPU-bound or latency-sensitive; Embabel's is exclusively long-blocking I/O.

AsyncConfiguration pulls in Spring's APPLICATION_TASK_EXECUTOR_BEAN_NAME. That executor serves every @Async method and @Scheduled task in the entire application. Sharing it means:

  • Embabel starves the app: A burst of concurrent LLM calls consumes threads that scheduled jobs or async service calls also need.
  • The app starves Embabel: If the host application configures a bounded ThreadPoolTaskExecutor with a queue and rejection policy (common in production), Embabel's tasks can be queued, throttled, or rejected under load.

4. newVirtualThreadPerTaskExecutor needs no tuning

Last but not least, a virtual-thread-per-task executor has no configuration surface. It scales with demand automatically. There is no pool size to get wrong. Embabel owns it and there is nothing for users or operators to misconfigure. A bounded ThreadPoolTaskExecutor requires choosing core size, max size, queue capacity, and rejection policy - all of which are wrong by default for Embabel's pattern (unbounded parallelism, each task blocked for seconds).

5. Our own executor enables named threads and full observability

ExecutorAsyncer already does Embabel-specific work: it captures AgentProcessAccessor from the calling thread and propagates it to worker threads. Embabel already treats this executor as its own in practice. Formalizing that with a named thread factory (Thread.ofVirtual().name("embabel-action-", 0).factory()) means every Embabel thread is immediately identifiable in thread dumps and profilers. When an LLM call stalls, we'll see embabel-action-42 rather than an anonymous pool thread shared with the rest of the application.

6. It decouples the threading decision from Spring configuration entirely

The three-tier proposal in the issue (spring.threads.virtual.enabled -> embabel.threads.virtual.enabled -> true) still routes through Spring's property system to influence the shared executor. If we own Executors.newVirtualThreadPerTaskExecutor() directly, the decision is made once in Embabel's own AsyncConfiguration, requires no Spring property at all, and the problem in issue #1694 - a library modifying application-wide Spring config - disappears completely. The spring.threads.virtual.enabled property controls the application's own threads. Embabel's threads are simply never part of that surface.

simeshev avatar Jun 15 '26 15:06 simeshev

@simeshev - thanks for the feedback.

Summarizing:

  • remove property from the config
  • maintain separate thread pools app vs. embabel

igordayen avatar Jun 15 '26 20:06 igordayen

@igordayen @simeshev Thanks for the work on this and sorry for the late reply

I think you are on the right track for this. For our project, we provide our own Asyncer implementation because we need to ensure that some ThreadLocals are passed.

Whatever you provide as a standard embabel executor, it should be unbounded. We ran into problems when we first used a bounded executor

stefanks-px avatar Jun 16 '26 07:06 stefanks-px

@poutsma @simeshev @alexheifetz @stefanks-px @azanux

I don't think we can always state that agent behavior is I/O-bound; for instance, in a scenario with heavy action parallelism.

Summarizing

Practically, we may need: 1. inheritance override from app settings: if app sets platform policy, change for framework to virtual; if app sets virtual, override to platform; or no overrides - meaning default as full policy inheritance 2. shared pool - only if application sets policy as platform threads, indicate same thread pool as app thread pool or not, default - separate pool

properties names:
embabel.agent. platform.virtual.threads.override = true/false
embabel.agent. platform.threading.shared = true/false
  ┌────────────────────────────────┬───────────────┬───────────────┬──────────┬──────────┬───────────────────────────────────┐                                                                
  │ spring.threads.virtual.enabled │   override    │    shared     │   App    │ Embabel  │         Executor Instance         │                                                                
  ├────────────────────────────────┼───────────────┼───────────────┼──────────┼──────────┼───────────────────────────────────┤                                                                
  │ not set (→ platform)           │ not set/false │ not set/false │ Platform │ Platform │ Isolated (separate pools)         │                                                                
  ├────────────────────────────────┼───────────────┼───────────────┼──────────┼──────────┼───────────────────────────────────┤                                                                
  │ not set (→ platform)           │ not set/false │ true          │ Platform │ Platform │ Shared (same pool)                │                                                                
  ├────────────────────────────────┼───────────────┼───────────────┼──────────┼──────────┼───────────────────────────────────┤                                                                
  │ not set (→ platform)           │ true          │ (ignored)     │ Platform │ Virtual  │ Isolated (N/A - different models) │                                                                
  ├────────────────────────────────┼───────────────┼───────────────┼──────────┼──────────┼───────────────────────────────────┤                                                                
  │ false                          │ not set/false │ not set/false │ Platform │ Platform │ Isolated (separate pools)         │                                                                
  ├────────────────────────────────┼───────────────┼───────────────┼──────────┼──────────┼───────────────────────────────────┤                                                                
  │ false                          │ not set/false │ true          │ Platform │ Platform │ Shared (same pool)                │                                                                
  ├────────────────────────────────┼───────────────┼───────────────┼──────────┼──────────┼───────────────────────────────────┤                                                                
  │ false                          │ true          │ (ignored)     │ Platform │ Virtual  │ Isolated (N/A - different models) │                                                                
  ├────────────────────────────────┼───────────────┼───────────────┼──────────┼──────────┼───────────────────────────────────┤                                                                
  │ true                           │ not set/false │ (ignored)     │ Virtual  │ Virtual  │ Isolated (N/A - both unbounded)   │                                                                
  ├────────────────────────────────┼───────────────┼───────────────┼──────────┼──────────┼───────────────────────────────────┤                                                                
  │ true                           │ true          │ (ignored)     │ Virtual  │ Platform │ Isolated (N/A - different models) │                                                                
  └────────────────────────────────┴───────────────┴───────────────┴──────────┴──────────┴───────────────────────────────────┘ 

igordayen avatar Jun 17 '26 20:06 igordayen

Nice. Though, don't we want by default (nothing is set ) be virtual / isolated?

simeshev avatar Jun 17 '26 22:06 simeshev

Thought about this too. Feel default should be full inheritance, no overrides. Thanks fir feedback.

igordayen avatar Jun 17 '26 22:06 igordayen

@stefanks-px - please try: gh pr checkout 1730 thanks

igordayen avatar Jun 19 '26 15:06 igordayen