Repository: quarkusio/quarkus
Issue: Shared lifecycle interceptor instances use wrong CreationalContext

Each interceptor instance should receive a new child `CreationalContext` from the CC of the intercepted bean. This issue follows up on #3530.

Changed files:
independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanGenerator.java
independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/intercepted/InterceptedBeanInjectionTest.java
independent-projects/arc/runtime/src/main/java/io/quarkus/arc/InterceptedBeanMetadataProvider.java

diff --git a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanGenerator.java b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanGenerator.java
index 31f2824a9c8..7a75c1d6662 100644
--- a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanGenerator.java
+++ b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanGenerator.java
@@ -854,9 +854,11 @@ protected void implementCreate(ClassOutput classOutput, ClassCreator beanCreator
                             create.getThis());
                     ResultHandle interceptorProvider = create.invokeInterfaceMethod(
                             MethodDescriptors.SUPPLIER_GET, interceptorProviderSupplier);
+                    ResultHandle childCtxHandle = create.invokeStaticMethod(MethodDescriptors.CREATIONAL_CTX_CHILD,
+                            create.getMethodParam(0));
                     ResultHandle interceptorInstanceHandle = create.invokeInterfaceMethod(
                             MethodDescriptors.INJECTABLE_REF_PROVIDER_GET, interceptorProvider,
-                            create.getMethodParam(0));
+                            childCtxHandle);
                     interceptorToResultHandle.put(interceptor, interceptorInstanceHandle);
                     ResultHandle wrapHandle = create.invokeStaticMethod(
                             MethodDescriptor.ofMethod(InitializedInterceptor.class, "of",
diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/InterceptedBeanMetadataProvider.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/InterceptedBeanMetadataProvider.java
index c8955d6ab59..f1b37b7c3cf 100644
--- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/InterceptedBeanMetadataProvider.java
+++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/InterceptedBeanMetadataProvider.java
@@ -14,10 +14,11 @@ public class InterceptedBeanMetadataProvider implements InjectableReferenceProvi
 
     @Override
     public Contextual<?> get(CreationalContext<Contextual<?>> creationalContext) {
+        // First attempt to obtain the creational context of the interceptor bean and then the creational context of the intercepted bean
         CreationalContextImpl<?> parent = unwrap(creationalContext).getParent();
         if (parent != null) {
-            // Intercepted bean creational context
-            return parent.getContextual();
+            parent = parent.getParent();
+            return parent != null ? parent.getContextual() : null;
         }
         return null;
     }
diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/intercepted/InterceptedBeanInjectionTest.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/intercepted/InterceptedBeanInjectionTest.java
index ea212c46c98..ca5c43a3b18 100644
--- a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/intercepted/InterceptedBeanInjectionTest.java
+++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/intercepted/InterceptedBeanInjectionTest.java
@@ -5,6 +5,7 @@
 import io.quarkus.arc.Arc;
 import io.quarkus.arc.test.ArcTestContainer;
 import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.Dependent;
 import javax.enterprise.inject.spi.Bean;
 import javax.inject.Inject;
 import org.junit.jupiter.api.Test;
@@ -14,7 +15,7 @@ public class InterceptedBeanInjectionTest {
 
     @RegisterExtension
     public ArcTestContainer container = new ArcTestContainer(Simple.class,
-            SimpleInterceptor.class, InterceptedBean.class);
+            SimpleInterceptor.class, InterceptedBean.class, InterceptedDependent.class);
 
     @Test
     public void testInterception() {
@@ -22,6 +23,12 @@ public void testInterception() {
         assertEquals(InterceptedBean.class.getName() + InterceptedBean.class.getName(), bean.ping());
         assertEquals(InterceptedBean.class.getName(), SimpleInterceptor.aroundConstructResult);
         assertEquals(InterceptedBean.class.getName(), SimpleInterceptor.postConstructResult);
+        assertEquals(
+                InterceptedBean.class.getName() + InterceptedDependent.class.getName() + InterceptedDependent.class.getName(),
+                bean.pong());
+        InterceptedDependent dependent = Arc.container().instance(InterceptedDependent.class).get();
+        assertEquals(InterceptedDependent.class.getName() + InterceptedDependent.class.getName(),
+                dependent.pong());
     }
 
     @ApplicationScoped
@@ -31,10 +38,27 @@ static class InterceptedBean {
         @Inject
         Bean<?> bean;
 
-        public String ping() {
+        @Inject
+        InterceptedDependent dependent;
+
+        String ping() {
             return bean.getBeanClass().getName();
         }
 
+        String pong() {
+            return dependent.pong();
+        }
+
+    }
+
+    @Dependent
+    static class InterceptedDependent {
+
+        @Simple
+        String pong() {
+            return InterceptedDependent.class.getName();
+        }
+
     }
 
 }