Skip to content

[RESTEASY-3313] Capture the ThreadContexts in the ResteasyProviderFactory to avoid repeated service loading. - #5124

Merged
jamezp merged 2 commits into
resteasy:mainfrom
jamezp:RESTEASY-3313-thread-context
Nov 3, 2026
Merged

jamezp merged 2 commits into
resteasy:mainfrom
jamezp:RESTEASY-3313-thread-context

Conversation

@jamezp

@jamezp jamezp commented Nov 3, 2026

Copy link
Copy Markdown
Member

Previously the ThreadContext services were located with the
ServiceLoader each time a task was submitted to a contextual executor,
and registered ThreadContext providers were stored in the shared
ResteasyContext data, which leaked them across threads and clients.

  • Keep the ServiceLoader.Provider handles in ThreadContexts, so the
    services are located once and a new instance of each is created per
    capture. Add a copy constructor which reuses the handles. Deprecate
    ThreadContexts.clear() as it is now a no-op.
  • Add ResteasyProviderFactory.getThreadContexts(). The provider factory
    owns its thread contexts and registered ThreadContext providers are
    added to them, not to the ResteasyContext. A child provider factory,
    including a client's, copies its parent's thread contexts.
  • Have the internal executors hold the ThreadContexts resolved when the
    executor is created. Add ContextualExecutors overloads which accept
    the ResteasyProviderFactory or the ThreadContexts. Executors wrapped
    without thread contexts resolve them from
    ResteasyProviderFactory.getInstance() on each submit.
  • Create the deployment's executors with the deployment's thread
    contexts after the provider factory is initialized, which also avoids
    creating the global provider factory with the deployment's class
    loader. The AsynchronousDispatcher uses its provider factory's thread
    contexts.
  • Use the client's thread contexts for the client and SSE executors,
    and the invocation's thread contexts for asynchronous invocations and
    client proxies, so ThreadContext providers registered on a WebTarget
    are captured.
  • Shut down the thread pool MultipartEntityPartWriter.asyncWriteTo()
    creates when it is not managed.
  • Document the lifecycle of ThreadContext implementations.

…actory

Previously the ThreadContext services were located with the
ServiceLoader each time a task was submitted to a contextual executor,
and registered ThreadContext providers were stored in the shared
ResteasyContext data, which leaked them across threads and clients.

- Keep the ServiceLoader.Provider handles in ThreadContexts, so the
services are located once and a new instance of each is created per
capture. Add a copy constructor which reuses the handles. Deprecate
ThreadContexts.clear() as it is now a no-op.
- Add ResteasyProviderFactory.getThreadContexts(). The provider factory
owns its thread contexts and registered ThreadContext providers are
added to them, not to the ResteasyContext. A child provider factory,
including a client's, copies its parent's thread contexts.
- Have the internal executors hold the ThreadContexts resolved when the
executor is created. Add ContextualExecutors overloads which accept
the ResteasyProviderFactory or the ThreadContexts. Executors wrapped
without thread contexts resolve them from
ResteasyProviderFactory.getInstance() on each submit.
- Create the deployment's executors with the deployment's thread
contexts after the provider factory is initialized, which also avoids
creating the global provider factory with the deployment's class
loader. The AsynchronousDispatcher uses its provider factory's thread
contexts.
- Use the client's thread contexts for the client and SSE executors,
and the invocation's thread contexts for asynchronous invocations and
client proxies, so ThreadContext providers registered on a WebTarget
are captured.
- Shut down the thread pool MultipartEntityPartWriter.asyncWriteTo()
creates when it is not managed.
- Document the lifecycle of ThreadContext implementations.

https://redhat.atlassian.net/browse/RESTEASY-3313

Signed-off-by: James R. Perkins <[email protected]>
@jamezp
jamezp force-pushed the RESTEASY-3313-thread-context branch from a3f964e to 9492155 Compare November 3, 2026 17:19
@jamezp
jamezp merged commit 9bcdfe9 into resteasy:main Nov 3, 2026
9 checks passed
@jamezp
jamezp deleted the RESTEASY-3313-thread-context branch November 3, 2026 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant