Uploaded image for project: 'Undertow'
  1. Undertow
  2. UNDERTOW-2615

Undertow does not call `contextDestroyed` from version 2.2.32 (2.2.31 works)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • None
    • 2.2.32.Final
    • Servlet
    • None

      I have this ServletContextListener:

       

      public static class EchoContextListener implements ServletContextListener {
      
          static AtomicBoolean INIT = new AtomicBoolean(false);
          ServletContextEvent sce;
      
          @Override
          public void contextInitialized(ServletContextEvent sce) {
             INIT.set(true);
             this.sce = sce;
          }
      
          @Override
          public void contextDestroyed(ServletContextEvent sce) {
             INIT.set(false);
          }
      
      }

      which I use in my test. This test passes in 2.2.31, but stops working on 2.2.32 and newer 2.2.X versions:

      @Test
      public void echoContextListener() throws IOException, InterruptedException {
          server.addServletListener(EchoContextListener.class.getName());
          server.start();
      
          assertEquals(true, EchoContextListener.INIT.get());
      
          server.stop();
      
          assertEquals(false, EchoContextListener.INIT.get());
      } 

      start() hides the logic to start an embedded undertow server:

       

      @Override
      public void start() {
          if (isStarted()) {
             throw new IllegalStateException("Already started");
          }
          worker = buildXnioWorker();
          buildDeploymentManager();
          buildPathHandler();
          buildAccessLogHandler();
          buildUndertow();
      
          undertow.start();
      } 
      
      private void buildDeploymentManager() {
          deploymentManager = Servlets.newContainer().addDeployment(buildDeploymentInfo());
          deploymentManager.deploy();
      }
      
      private void buildPathHandler() {
          try {
             this.pathHandler = Handlers.path(Handlers.redirect("/")).addPrefixPath("/",
                   requireNonNull(deploymentManager).start());
          } catch (ServletException e) {
             throw new RuntimeException(e);
          }
      }
      
      private void buildAccessLogHandler() {
          accessLogHandler = new LogHttpHandler(requireNonNull(pathHandler), Predicates.truePredicate());
          accessLogHandler.start();
      }
      
      private void buildUndertow() {
          undertow = Undertow.builder().addHttpListener(config.getHttpPort(), config.getHost())
                .setWorker(requireNonNull(worker)).setDirectBuffers(true).setHandler(requireNonNull(accessLogHandler))
                .build();
      }

      same for stop():

      public void stop() {
          if (!isStarted()) {
             return;
          }
          try {
             deploymentManager.undeploy();
             deploymentManager = null;
          } finally {
             try {
                accessLogHandler.stop();
                accessLogHandler = null;
             } finally {
                try {
                   undertow.stop();
                   undertow = null;
                } finally {
                   worker.shutdown();
                   worker = null;
                }
             }
          }
      }
       

              rhn-cservice-bbaranow Bartosz Baranowski
              flozano@gmail.com Francisco Alejandro Lozano López
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated: