{{ message }}
Repository navigation
fix: defer CDI request context destruction for streaming requests - #904
Merged
Merged
Conversation
Streaming (SSE) requests dispatch agent work to a background thread via ManagedExecutor and return immediately. The existing runInRequestContext() terminates the CDI request context in a finally block, destroying all @RequestScoped beans before the agent thread runs. This causes ContextNotActiveException when the agent accesses request-scoped beans (e.g. OIDC token propagation via @accesstoken). Add runInRequestContextDeferred() which deactivates the context (detaching it from the worker thread) but defers bean destruction to the HTTP response lifecycle via endHandler/closeHandler. This keeps all @RequestScoped beans alive for the duration of the streaming response. - JSONRPC routes: use runInRequestContextDeferred for the main handler (handles both streaming and non-streaming) - REST routes: add authenticatedStreaming() wrapper for message:stream and tasks/:subscribe endpoints - Apply same changes to compat-0.3 JSONRPC and REST routes - Add streaming variant of CDI propagation test
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a deferred CDI request context mechanism (runInRequestContextDeferred) in VertxSecurityHelper to support streaming (SSE) requests where background threads need access to request-scoped beans. This mechanism is integrated into JSON-RPC and REST routing classes via the new authenticatedStreaming handler, and integration tests have been added to verify this behavior. The review feedback suggests catching Throwable instead of Exception in runInRequestContextDeferred to ensure the context is safely cleaned up even if an Error occurs.
ehsavoie
approved these changes
Jun 1, 2026
salaboy
pushed a commit
to salaboy/a2a-java
that referenced
this pull request
Jun 19, 2026
…aproject#904) * fix: defer CDI request context destruction for streaming requests Streaming (SSE) requests dispatch agent work to a background thread via ManagedExecutor and return immediately. The existing runInRequestContext() terminates the CDI request context in a finally block, destroying all @RequestScoped beans before the agent thread runs. This causes ContextNotActiveException when the agent accesses request-scoped beans (e.g. OIDC token propagation via @accesstoken). Add runInRequestContextDeferred() which deactivates the context (detaching it from the worker thread) but defers bean destruction to the HTTP response lifecycle via endHandler/closeHandler. This keeps all @RequestScoped beans alive for the duration of the streaming response. - JSONRPC routes: use runInRequestContextDeferred for the main handler (handles both streaming and non-streaming) - REST routes: add authenticatedStreaming() wrapper for message:stream and tasks/:subscribe endpoints - Apply same changes to compat-0.3 JSONRPC and REST routes - Add streaming variant of CDI propagation test
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.

Streaming (SSE) requests dispatch agent work to a background thread via ManagedExecutor and return immediately. The existing runInRequestContext() terminates the CDI request context in a finally block, destroying all @RequestScoped beans before the agent thread runs. This causes ContextNotActiveException when the agent accesses request-scoped beans (e.g. OIDC token propagation via @accesstoken).
Add runInRequestContextDeferred() which deactivates the context (detaching it from the worker thread) but defers bean destruction to the HTTP response lifecycle via endHandler/closeHandler. This keeps all @RequestScoped beans alive for the duration of the streaming response.
Fixes #893 🦕