slachiewicz opened a new issue, #13827:
URL: https://github.com/apache/maven/issues/13827

   ### Question
   
   Should Maven offer plugins a way to run work on another thread inside the 
current mojo execution scope, or is the expectation that plugins never let 
scoped components be created off the Mojo thread?
   
   ### Case
   
   `maven-site-plugin`'s `site:run` serves pages from the JDK HTTP server's own 
threads, after `execute()` has started the server. A Doxia parser contributed 
by a site dependency, the Asciidoctor one, injects `MavenProject`. Parsers are 
looked up per document, so it is instantiated on a server thread and fails with
   
   ```
   OutOfScopeException: Cannot access Key[type=MavenProject, annotation=[none]] 
outside of a scoping block
     at MojoExecutionScopeModule.configure(MojoExecutionScopeModule.java:47)
   ```
   
   `site:site` works, since it renders on the Mojo thread 
(apache/maven-site-plugin#987, MSITE-849).
   
   `MojoExecutionScope` keeps its state in a `ThreadLocal` (3.9.x 
`org.apache.maven.execution.scope.internal`, 4.x `org.apache.maven.impl.di`). 
`SessionScope` does not; it uses a `CopyOnWriteArrayList`, so session-scoped 
components work from any thread. Plugins see only 
`org.apache.maven.execution.scope` (the annotations and 
`WeakMojoExecutionListener`), so they cannot enter or seed the mojo execution 
scope themselves.
   
   ### What the plugin does now
   
   apache/maven-site-plugin#1350 keeps the server's thread pool for requests 
and hands only document rendering to the Mojo thread, which loops over a queue 
until the server stops. It works with every Maven version, but it serializes 
rendering and any plugin starting a server or a worker pool has to rediscover 
it.
   
   ### Options
   
   1. **Context propagation API.** In the exported 
`org.apache.maven.execution.scope` package (and the Maven 4 API equivalent), 
something that captures the current mojo execution scope on the Mojo thread and 
returns a wrapper (`Runnable`/`Callable`/`Executor`) that enters the captured 
state on whichever thread runs the task and exits it afterwards.
   2. **Guidance only.** Document that mojo-execution-scoped objects are only 
available on the Mojo thread, and that components meant to be created by 
plugins on other threads must not inject them; the producer of the component 
(here Doxia Sitetools) should pass what is needed explicitly. That is being 
discussed for this case in apache/maven-doxia-sitetools#736.
   3. **Something else** that already exists and I missed.
   
   The site plugin needs option 2 or its current workaround anyway while it 
supports Maven 3.9. The question is whether option 1 is worth adding for 3.10 
or 4.x, or whether option 2 is the intended model.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to