title: There is more than one active context for org.glassfish.jersey.process.internal.RequestScoped
body: <p>I'm having difficulties retrieving a <code>RequestScoped</code> object inside a <code>ContainerRequestFilter</code>, which I attempt to do by injecting a <code>Provider</code> into my filter. My application configuration is as follows:</p>
  
  <pre><code>class MyResourceConfig extends ResourceConfig
  {
    public static class MyBinder extends AbstractBinder 
    {
      @Override
      protected void configure()
      {
        bindFactory(PolicyFactory.class)
          .to(Policy.class)
          .in(RequestScoped.class);
      }
    }
  
    public MyResourceConfig()
    {
      register(new MyBinder());
      register(MyDynamicFeature.class);
    }
  }
  
  @Provider
  class MyDynamicFeature implements DynamicFeature
  {
    @Override
    public void configure(ResourceInfo ri, FeatureContext ctx)
    {
      ctx.register(MyRequestFilter.class);
    }
  }
  
  @Priority(Priorities.AUTHENTICATION)
  public class MyRequestFilter implements ContainerRequestFilter
  {
    @Context
    private HttpServletRequest request;
  
    @Context
    private ResourceInfo resourceInfo;
  
    @Inject
    private javax.inject.Provider&lt;Policy&gt; policyProvider;
  
    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException
    {
      policyProvider.get();
    }
  }
  </code></pre>
  
  <p>When the provider is invoked in <code>MyRequestFilter</code> I get the following exception:</p>
  
  <pre><code>A MultiException has 2 exceptions.  They are:
  1. java.lang.IllegalStateException: There is more than one active context for org.glassfish.jersey.process.internal.RequestScoped
  2. java.lang.IllegalStateException: While attempting to create a service for SystemDescriptor(
    implementation=com.*.PolicyFactory
    contracts={com.*.Policy}
    scope=org.glassfish.jersey.process.internal.RequestScoped
    qualifiers={}
    descriptorType=PROVIDE_METHOD
    descriptorVisibility=NORMAL
    metadata=
    rank=0
    loader=org.glassfish.hk2.utilities.binding.AbstractBinder$2@16c49a23
    proxiable=null
    proxyForSameScope=null
    analysisName=null
    id=178
    locatorId=48
    identityHashCode=1250168232
    reified=true) in scope org.glassfish.jersey.process.internal.RequestScoped an error occured while locating the context
  
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2120)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:777)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getUnqualifiedService(ServiceLocatorImpl.java:789)
    at org.jvnet.hk2.internal.IterableProviderImpl.get(IterableProviderImpl.java:111)
    at com.*.MyEndpointFilter.hasResourceAccess(MyEndpointFilter.java:127)
    at com.*.MyEndpointFilter.filter(MyEndpointFilter.java:52)
    at org.glassfish.jersey.server.ContainerFilteringStage.apply(ContainerFilteringStage.java:132)
    at org.glassfish.jersey.server.ContainerFilteringStage.apply(ContainerFilteringStage.java:68)
    at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:269)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:272)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:268)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:268)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:256)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:703)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:416)
    ... 12 more
  Caused by: java.lang.IllegalStateException: There is more than one active context for org.glassfish.jersey.process.internal.RequestScoped
    at org.jvnet.hk2.internal.ServiceLocatorImpl._resolveContext(ServiceLocatorImpl.java:2216)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.access$000(ServiceLocatorImpl.java:128)
    at org.jvnet.hk2.internal.ServiceLocatorImpl$3.compute(ServiceLocatorImpl.java:188)
    at org.jvnet.hk2.internal.ServiceLocatorImpl$3.compute(ServiceLocatorImpl.java:184)
    at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture$1.call(Cache.java:97)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture.run(Cache.java:154)
    at org.glassfish.hk2.utilities.cache.Cache.compute(Cache.java:199)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.resolveContext(ServiceLocatorImpl.java:2230)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2104)
    ... 30 more
  </code></pre>
  
  <p>While debugging I observe that 2 instances of <code>Hk2RequestScope</code> are created, with the following stack traces:</p>
  
  <pre><code>Thread [main] (Suspended (breakpoint at line 61 in Hk2RequestScope))    
    Hk2RequestScope.createContext() line: 61  
    Hk2RequestScope(RequestScope).runInScope(Producer&lt;T&gt;) line: 411   
    JerseyInvocation.invoke() line: 752   
    JerseyInvocation$Builder.method(String, Entity&lt;?&gt;) line: 445  
    JerseyInvocation$Builder.post(Entity&lt;?&gt;) line: 351    
    LoginTest.testBadNewPassword() line: 181  
    NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]    
    NativeMethodAccessorImpl.invoke(Object, Object[]) line: 62    
    DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43    
    Method.invoke(Object, Object...) line: 498    
    ReflectionUtils.invokeMethod(Method, Object, Object...) line: 513 
    ExecutableInvoker.invoke(Method, Object, ExtensionContext, ExtensionRegistry) line: 115   
    TestMethodTestDescriptor.lambda$invokeTestMethod$6(ExtensionContext, JupiterEngineExecutionContext) line: 170 
    22179697.execute() line: not available    
    ThrowableCollector.execute(Executable) line: 40   
    TestMethodTestDescriptor.invokeTestMethod(JupiterEngineExecutionContext, Node$DynamicTestExecutor) line: 166  
    TestMethodTestDescriptor.execute(JupiterEngineExecutionContext, Node$DynamicTestExecutor) line: 113   
    TestMethodTestDescriptor.execute(EngineExecutionContext, Node$DynamicTestExecutor) line: 58   
    HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3(ExecutionTracker) line: 113 
    1803669141.execute() line: not available  
    SingleTestExecutor.executeSafely(SingleTestExecutor$Executable) line: 66  
    HierarchicalTestExecutor$NodeExecutor.executeRecursively(ExecutionTracker) line: 108  
    HierarchicalTestExecutor$NodeExecutor.execute(C, ExecutionTracker) line: 79   
    HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$2(ExecutionTracker, TestDescriptor) line: 121 
    232307208.accept(Object) line: not available  
    ForEachOps$ForEachOp$OfRef&lt;T&gt;.accept(T) line: 184 
    ReferencePipeline$2$1.accept(P_OUT) line: 175 
    LinkedHashMap$LinkedKeyIterator(Iterator&lt;E&gt;).forEachRemaining(Consumer&lt;? super E&gt;) line: 116  
    Spliterators$IteratorSpliterator&lt;T&gt;.forEachRemaining(Consumer&lt;? super T&gt;) line: 1801  
    ReferencePipeline$2(AbstractPipeline&lt;E_IN,E_OUT,S&gt;).copyInto(Sink&lt;P_IN&gt;, Spliterator&lt;P_IN&gt;) line: 481 
    ReferencePipeline$2(AbstractPipeline&lt;E_IN,E_OUT,S&gt;).wrapAndCopyInto(S, Spliterator&lt;P_IN&gt;) line: 471   
    ForEachOps$ForEachOp$OfRef&lt;T&gt;(ForEachOps$ForEachOp&lt;T&gt;).evaluateSequential(PipelineHelper&lt;T&gt;, Spliterator&lt;S&gt;) line: 151    
    ForEachOps$ForEachOp$OfRef&lt;T&gt;.evaluateSequential(PipelineHelper, Spliterator) line: 174   
    ReferencePipeline$2(AbstractPipeline&lt;E_IN,E_OUT,S&gt;).evaluate(TerminalOp&lt;E_OUT,R&gt;) line: 234   
    ReferencePipeline$2(ReferencePipeline&lt;P_IN,P_OUT&gt;).forEach(Consumer&lt;? super P_OUT&gt;) line: 418 
    HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3(ExecutionTracker) line: 121 
    1803669141.execute() line: not available  
    SingleTestExecutor.executeSafely(SingleTestExecutor$Executable) line: 66  
    HierarchicalTestExecutor$NodeExecutor.executeRecursively(ExecutionTracker) line: 108  
    HierarchicalTestExecutor$NodeExecutor.execute(C, ExecutionTracker) line: 79   
    HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$2(ExecutionTracker, TestDescriptor) line: 121 
    232307208.accept(Object) line: not available  
    ForEachOps$ForEachOp$OfRef&lt;T&gt;.accept(T) line: 184 
    ReferencePipeline$2$1.accept(P_OUT) line: 175 
    LinkedHashMap$LinkedKeyIterator(Iterator&lt;E&gt;).forEachRemaining(Consumer&lt;? super E&gt;) line: 116  
    Spliterators$IteratorSpliterator&lt;T&gt;.forEachRemaining(Consumer&lt;? super T&gt;) line: 1801  
    ReferencePipeline$2(AbstractPipeline&lt;E_IN,E_OUT,S&gt;).copyInto(Sink&lt;P_IN&gt;, Spliterator&lt;P_IN&gt;) line: 481 
    ReferencePipeline$2(AbstractPipeline&lt;E_IN,E_OUT,S&gt;).wrapAndCopyInto(S, Spliterator&lt;P_IN&gt;) line: 471   
    ForEachOps$ForEachOp$OfRef&lt;T&gt;(ForEachOps$ForEachOp&lt;T&gt;).evaluateSequential(PipelineHelper&lt;T&gt;, Spliterator&lt;S&gt;) line: 151    
    ForEachOps$ForEachOp$OfRef&lt;T&gt;.evaluateSequential(PipelineHelper, Spliterator) line: 174   
    ReferencePipeline$2(AbstractPipeline&lt;E_IN,E_OUT,S&gt;).evaluate(TerminalOp&lt;E_OUT,R&gt;) line: 234   
    ReferencePipeline$2(ReferencePipeline&lt;P_IN,P_OUT&gt;).forEach(Consumer&lt;? super P_OUT&gt;) line: 418 
    HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3(ExecutionTracker) line: 121 
    1803669141.execute() line: not available  
    SingleTestExecutor.executeSafely(SingleTestExecutor$Executable) line: 66  
    HierarchicalTestExecutor$NodeExecutor.executeRecursively(ExecutionTracker) line: 108  
    HierarchicalTestExecutor$NodeExecutor.execute(C, ExecutionTracker) line: 79   
    HierarchicalTestExecutor&lt;C&gt;.execute() line: 55    
    JupiterTestEngine(HierarchicalTestEngine&lt;C&gt;).execute(ExecutionRequest) line: 43   
    DefaultLauncher.execute(TestEngine, ExecutionRequest) line: 170   
    DefaultLauncher.execute(Root, ConfigurationParameters, TestExecutionListener...) line: 154    
    DefaultLauncher.execute(LauncherDiscoveryRequest, TestExecutionListener...) line: 90  
    JUnit5TestReference.run(TestExecution) line: 86   
    TestExecution.run(ITestReference[]) line: 38  
    RemoteTestRunner.runTests(String[], String, TestExecution) line: 538  
    RemoteTestRunner.runTests(TestExecution) line: 760    
    RemoteTestRunner.run() line: 460  
    RemoteTestRunner.main(String[]) line: 206 
  
  Thread [grizzly-http-server-1] (Suspended (breakpoint at line 61 in Hk2RequestScope))   
    Hk2RequestScope.createContext() line: 61  
    ServerRuntime.process(ContainerRequest) line: 249 
    ApplicationHandler.handle(ContainerRequest) line: 703 
    WebComponent.serviceImpl(URI, URI, HttpServletRequest, HttpServletResponse) line: 416 
    WebComponent.service(URI, URI, HttpServletRequest, HttpServletResponse) line: 370 
    ServletContainer.service(URI, URI, HttpServletRequest, HttpServletResponse) line: 389 
    ServletContainer.service(HttpServletRequest, HttpServletResponse) line: 342   
    ServletContainer.service(ServletRequest, ServletResponse) line: 229   
    FilterChainImpl.doFilter(ServletRequest, ServletResponse) line: 147   
    FilterChainImpl.invokeFilterChain(ServletRequest, ServletResponse) line: 106  
    ServletHandler.doServletService(Request, Response) line: 226  
    ServletHandler.service(Request, Response) line: 173   
    HttpHandler$1.run() line: 224 
    FixedThreadPool$BasicWorker(AbstractThreadPool$Worker).doWork() line: 593 
    FixedThreadPool$BasicWorker(AbstractThreadPool$Worker).run() line: 573    
    Thread.run() line: 748    
  </code></pre>
  
  <p>In this particular case I was doing the invocation from a unit test with Grizzly configured for servlet deployment. The issue also occurs when not using servlet deployment, and also when actually running the application live (Tomcat).</p>
  
  <p>How can I resolve this issue?</p>