Repository: quarkusio/quarkus
Issue: Quarkus 1.13.x WebSocket Client - wrong ClassLoader?

Since upgrading from 1.12.x to 1.13.x the WebSocket Client callbacks no longer interact with the container correctly.  I am seeing issues such as:

```
ERROR: UT026013: Unhandled error in annotated endpoint org.acme.websockets.ChatTest$Client@54e6d66f
java.lang.RuntimeException: java.util.ServiceConfigurationError: io.smallrye.config.SmallRyeConfigFactory: io.quarkus.runtime.configuration.QuarkusConfigFactory not a subtype
```

I'm guessing a ClassLoader issue?

I observed this initially on a server-side WS Client but it is also reproducible in the ChatTest example (attached).
[Reproducer: websockets-quickstart.zip](https://github.com/quarkusio/quarkus/files/6293295/websockets-quickstart.zip)

Changed files:
extensions/websockets/runtime/src/main/java/io/quarkus/undertow/websockets/runtime/WebsocketRecorder.java

diff --git a/extensions/websockets/runtime/src/main/java/io/quarkus/undertow/websockets/runtime/WebsocketRecorder.java b/extensions/websockets/runtime/src/main/java/io/quarkus/undertow/websockets/runtime/WebsocketRecorder.java
index fc5c834d8ac..f0f16c3f1e4 100644
--- a/extensions/websockets/runtime/src/main/java/io/quarkus/undertow/websockets/runtime/WebsocketRecorder.java
+++ b/extensions/websockets/runtime/src/main/java/io/quarkus/undertow/websockets/runtime/WebsocketRecorder.java
@@ -19,6 +19,7 @@
 import io.quarkus.arc.runtime.BeanContainer;
 import io.quarkus.runtime.ExecutorRecorder;
 import io.quarkus.runtime.annotations.Recorder;
+import io.undertow.websockets.UndertowContainerProvider;
 import io.undertow.websockets.WebSocketDeploymentInfo;
 import io.undertow.websockets.util.ContextSetupHandler;
 import io.undertow.websockets.util.ObjectFactory;
@@ -107,6 +108,7 @@ public WebSocketDeploymentInfo createDeploymentInfo(Set<String> annotatedEndpoin
 
     public Handler<RoutingContext> createHandler(BeanContainer beanContainer, Supplier<EventLoopGroup> eventLoopGroupSupplier,
             WebSocketDeploymentInfo info) throws DeploymentException {
+        ClassLoader cl = Thread.currentThread().getContextClassLoader();
         ManagedContext requestContext = Arc.container().requestContext();
         VertxServerWebSocketContainer container = new VertxServerWebSocketContainer(new ObjectIntrospecter() {
             @Override
@@ -137,11 +139,22 @@ public <T, C> Action<T, C> create(Action<T, C> action) {
                         return new Action<T, C>() {
                             @Override
                             public T call(C context) throws Exception {
-                                requestContext.activate();
+                                ClassLoader old = Thread.currentThread().getContextClassLoader();
+                                Thread.currentThread().setContextClassLoader(cl);
+                                boolean required = !requestContext.isActive();
+                                if (required) {
+                                    requestContext.activate();
+                                }
                                 try {
                                     return action.call(context);
                                 } finally {
-                                    requestContext.terminate();
+                                    try {
+                                        if (required) {
+                                            requestContext.terminate();
+                                        }
+                                    } finally {
+                                        Thread.currentThread().setContextClassLoader(old);
+                                    }
                                 }
                             }
                         };
@@ -159,6 +172,7 @@ public Executor get() {
         for (ServerEndpointConfig i : info.getProgramaticEndpoints()) {
             container.addEndpoint(i);
         }
+        UndertowContainerProvider.setDefaultContainer(container);
         return new VertxWebSocketHandler(container, info);
     }