Project: Tomcat
Bug 45591 NullPointerException during shutdown in JKMain.pause()
Reported: 2008-08-07 12:35:38 | Status: resolved fixed

On startup, if Tomcat discovers that the main port is already bound to another process, it fails and tries to execute the shutdown code. In some situations (I'm not sure which), the org.apache.jk.server.JKMain instance variable wEnv is null at this point, which causes a NullPointerException when JKMain.pause() is called (see stacktrace below). Since this is occurring within a shutdown hook thread, it can result in a dirty shutdown of the JVM. I've observed this with Tomcat 5.5.20, but I looked over the code in 5.5.26 and it looks like the same problem is still present. Fortunately, the solution is very simple. Just add the following if test at the beginning of JKMain.pause(): if (wEnv == null) { return; } Stacktrace: SEVERE: StandardServer.await: create[8006]: java.net.BindException: Address already in use: JVM_Bind at java.net.PlainSocketImpl.socketBind(Native Method) at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359) at java.net.ServerSocket.bind(ServerSocket.java:319) at java.net.ServerSocket.<init>(ServerSocket.java:185) at org.apache.catalina.core.StandardServer.await(StandardServer.java:372) at org.apache.catalina.startup.Catalina.await(Catalina.java:615) at org.apache.catalina.startup.Catalina.start(Catalina.java:575) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432) Aug 7, 2008 3:25:34 PM org.apache.coyote.http11.Http11BaseProtocol pause INFO: Pausing Coyote HTTP/1.1 on http-8000 Aug 7, 2008 3:25:34 PM org.apache.catalina.connector.Connector pause SEVERE: Protocol handler pause failed java.lang.NullPointerException at org.apache.jk.server.JkMain.pause(JkMain.java:678) at org.apache.jk.server.JkCoyoteHandler.pause(JkCoyoteHandler.java:162) at org.apache.catalina.connector.Connector.pause(Connector.java:1031) at org.apache.catalina.core.StandardService.stop(StandardService.java:491) at org.apache.catalina.core.StandardServer.stop(StandardServer.java:743) at org.apache.catalina.startup.Catalina.stop(Catalina.java:601) at org.apache.catalina.startup.Catalina$CatalinaShutdownHook.run(Catalina.java:644)

Files changed in fix:
java/org/apache/jk/server/JkMain.java