Skip to content

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

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

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

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.

Upstream #5124

@jamezp
jamezp requested a review from a team as a code owner November 3, 2026 15:48
@jamezp

jamezp commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

@stevenschlansker what do you think of an approach like this? It's a bigger change, but the goal is to create the ThreadContexts once instead of for each thread.

…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-7.0 branch from 90f798a to e0b2b4a Compare November 3, 2026 17:15
@jamezp
jamezp merged commit 3565d21 into resteasy:7.0 Nov 3, 2026
9 of 12 checks passed
@jamezp
jamezp deleted the RESTEASY-3313-thread-context-7.0 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