Buggy code:
final Loader loader = new WebCtxLoader ( classLoader ) ; webContext . setLoader ( loader ) ; List < ValveMetaData > valves = metaData . getValves ( ) ; if ( valves == null ) { metaData . setValves ( ( valves = new ArrayList < ValveMetaData > ( ) ) ) ; } for ( ValveMetaData valve : valves ) { Valve valveInstance = ( ( Valve ) ( getInstance ( module , valve . getModule ( ) , valve . getValveClass ( ) , valve . getParams ( ) ) ) ) ; webContext . getPipeline ( ) . addValve ( valveInstance ) ; } List < ContainerListenerMetaData > listeners = metaData . getContainerListeners ( ) ; if ( listeners != null ) { for ( ContainerListenerMetaData listener : listeners ) { switch ( listener . getListenerType ( ) ) { case CONTAINER : ContainerListener containerListener = ( ( ContainerListener ) ( getInstance ( module , listener . getModule ( ) , listener . getListenerClass ( ) , listener . getParams ( ) ) ) ) ; webContext . addContainerListener ( containerListener ) ; break ; case LIFECYCLE : LifecycleListener lifecycleListener = ( ( LifecycleListener ) ( getInstance ( module , listener . getModule ( ) , listener . getListenerClass ( ) , listener . getParams ( ) ) ) ) ; if ( webContext instanceof Lifecycle ) { ( ( Lifecycle ) ( webContext ) ) . addLifecycleListener ( lifecycleListener ) ; } break ; case SERVLET_INSTANCE : webContext . addInstanceListener ( listener . getListenerClass ( ) ) ; break ; case SERVLET_CONTAINER : webContext . addWrapperListener ( listener . getListenerClass ( ) ) ; break ; case SERVLET_LIFECYCLE : webContext . addWrapperLifecycle ( listener . getListenerClass ( ) ) ; break ; } } } switch ( metaData . getSessionCookies ( ) ) { case JBossWebMetaData . SESSION_COOKIES_ENABLED : webContext . setCookies ( true ) ; break ; case JBossWebMetaData . SESSION_COOKIES_DISABLED : webContext . setCookies ( false ) ; break ; } String metaDataSecurityDomain = metaData . getSecurityDomain ( ) ; if ( metaDataSecurityDomain == null ) { metaDataSecurityDomain = getJBossAppSecurityDomain ( deploymentUnit ) ; } if ( metaDataSecurityDomain != null ) { metaDataSecurityDomain = metaDataSecurityDomain . trim ( ) ; } String securityDomain = ( metaDataSecurityDomain == null ) ? SecurityConstants . DEFAULT_APPLICATION_POLICY : SecurityUtil . unprefixSecurityDomain ( metaDataSecurityDomain ) ; final List < ServletContextAttribute > attributes = deploymentUnit . getAttachment ( ATTACHMENT_KEY ) ; try { final ServiceName deploymentServiceName = WebSubsystemServices . deploymentServiceName ( hostName , pathName ) ; final ServiceName realmServiceName = deploymentServiceName . append ( "realm" ) ; final JBossWebRealmService realmService = new JBossWebRealmService ( deploymentUnit ) ; ServiceBuilder < ? > builder = serviceTarget . addService ( realmServiceName , realmService ) ; builder . addDependency ( REQUIRED , SERVICE_NAME . append ( securityDomain ) , SecurityDomainContext . class , realmService . getSecurityDomainContextInjector ( ) ) . setInitialMode ( ACTIVE ) . install ( ) ; final WebDeploymentService webDeploymentService = new WebDeploymentService ( webContext , injectionContainer , setupActions , attributes ) ; builder = serviceTarget . addService ( deploymentServiceName , webDeploymentService ) . addDependency ( JBOSS_WEB_HOST . append ( hostName ) , VirtualHost . class , new WebContextInjector ( webContext ) ) . addDependencies ( injectionContainer . getServiceNames ( ) ) . addDependency ( realmServiceName , Realm . class , webDeploymentService . getRealm ( ) ) . addDependencies ( deploymentUnit . getAttachmentList ( WEB_DEPENDENCIES ) ) . addDependency ( JndiNamingDependencyProcessor . serviceName ( deploymentUnit ) ) ; for ( final SetupAction action : setupActions ) { builder . addDependencies ( action . dependencies ( ) ) ; } if ( ( metaData . getDistributable ( ) ) != null ) { DistributedCacheManagerFactoryService factoryService = new DistributedCacheManagerFactoryService ( ) ; DistributedCacheManagerFactory factory = factoryService . getValue ( ) ; if ( factory != null ) { ServiceName factoryServiceName = deploymentServiceName . append ( "session" ) ; builder . addDependency ( OPTIONAL , factoryServiceName , DistributedCacheManagerFactory . class , config . getDistributedCacheManagerFactoryInjector ( ) ) ; ServiceBuilder < DistributedCacheManagerFactory > factoryBuilder = serviceTarget . addService ( factoryServiceName , factoryService ) ; <START_BUG> boolean enabled = factory . addDeploymentDependencies ( deploymentUnit . getServiceRegistry ( ) , factoryBuilder , metaData ) ; <END_BUG> factoryBuilder . setInitialMode ( ( enabled ? Mode . ON_DEMAND : Mode . NEVER ) ) . install ( ) ; } } builder . install ( ) ; AbstractSecurityDeployer < ? > deployer = new WarSecurityDeployer ( ) ; JaccService < ? > service = deployer . deploy ( deploymentUnit ) ; if ( service != null ) { ( ( WarJaccService ) ( service ) ) . setContext ( webContext ) ; final ServiceName jaccServiceName = deploymentUnit . getServiceName ( ) . append ( JaccService . SERVICE_NAME ) ; builder = serviceTarget . addService ( jaccServiceName , service ) ; if ( ( deploymentUnit . getParent ( ) ) != null ) { final DeploymentUnit parentDU = deploymentUnit . getParent ( ) ; builder . addDependency ( parentDU . getServiceName ( ) . append ( JaccService . SERVICE_NAME ) , PolicyConfiguration . class , service . getParentPolicyInjector ( ) ) ; } builder . addDependency ( deploymentServiceName ) ; builder . setInitialMode ( ACTIVE ) . install ( ) ; } } catch ( ServiceRegistryException e ) { throw new DeploymentUnitProcessingException ( WebMessages . MESSAGES . failedToAddWebDeployment ( ) , e ) ; } final ModelNode node = deploymentUnit . getDeploymentSubsystemModel ( "web" ) ; node . get ( CONTEXT_ROOT . getName ( ) ) . set ( ( "" . equals ( pathName ) ? "//" : pathName ) ) ; node . get ( VIRTUAL_HOST . getName ( ) ) . set ( hostName ) ; processManagement ( deploymentUnit , metaData ) ; } public static String pathNameOfDeployment ( final DeploymentUnit deploymentUnit , final JBossWebMetaData metaData ) { } void processManagement ( final DeploymentUnit unit , JBossWebMetaData metaData ) { }

Fix:
boolean enabled = factory . addDeploymentDependencies ( deploymentServiceName , deploymentUnit . getServiceRegistry ( ) , serviceTarget , factoryBuilder , metaData ) ;