Analyze and Fix One Java System.exit Hang Problem

My Solr application is designed to be running at user's laptop, it will fetch content from remote Solr server via proxy application. To reduce the impact to user, we limit the max memory size to 512mb: -Xmx512.
We take several approaches to reduce memory usage: for example use JSON(GSon) Streaming to read Solr document one by one from response.
Please see more details at:
Solr: Use JSON(GSon) Streaming to Reduce Memory Usage
Solr: Use STAX Parser to Read XML Response to Reduce Memory Usage
Solr: Use SAX Parser to Read XML Response to Reduce Memory Usage

But the Solr application may still throw OutOfMemoryError, when this happens, I have to kill the embedded jetty server, another application will detect the jetty server is down (by sending http request to /solr/admin/ping) and will restart it.

But I found that at some point, there are 2 embedded jetty application running: the first one hit OutOfMemoryError, and tried to kill itself, but hang, another was starting but failed with followingerror:
org.apache.solr.common.SolrException: 
Index locked for write for core collection1.
EVERE: Unable to create core: collection1
org.apache.solr.common.SolrException: Index locked for write for core collection1
 at org.apache.solr.core.SolrCore.$lt;init$gt;(SolrCore.java:806)
 at org.apache.solr.core.SolrCore.$lt;init$gt;(SolrCore.java:619)
Caused by: org.apache.lucene.store.LockObtainFailedException: Index locked for write for core collection1
 at org.apache.solr.core.SolrCore.initIndex(SolrCore.java:484)
 at org.apache.solr.core.SolrCore.$lt;init$gt;(SolrCore.java:730)
This is because the first jetty application didn't actually be killed, so it's still holding the lock to write.lock file. This problem can be fixed by fixing the first issue: why the first jetty application didn't kill itself.
Why System.exit hang?
I reproduced the problem, and get the heap dump and use IBM Java Thread and Dump Analyzer to analyze it.
Quick Conclusion
Don't call System.ext in ServletContextListener.contextDestroyed.

MyTaskThread
This happens first: when the application code detects OutOfMemoryError, it will do some clean up jobs and then call System.exit to kill itself. System.exit will call Shutdown.runHooks, which will run all ShutdownHook threads - added by Runtime.addShutdownHook(Thread hook).
Now it owns the lock of Shutdown instance and wait for jetty ShutdownThread finish.
MyTaskThread State: in Object.wait()
Monitor: Owns Monitor Lock on 0x00000000f0051870
at java.lang.Object.wait(Native Method) 
- waiting on [0x00000000f00a7000] (a org.eclipse.jetty.util.thread.ShutdownThread) 
at java.lang.Thread.join(Unknown Source) 
- locked [0x00000000f00a7000] (a org.eclipse.jetty.util.thread.ShutdownThread) 
at java.lang.Thread.join(Unknown Source) 
at java.lang.ApplicationShutdownHooks.runHooks(Unknown Source) 
at java.lang.ApplicationShutdownHooks$1.run(Unknown Source) 
at java.lang.Shutdown.runHooks(Unknown Source) 
at java.lang.Shutdown.sequence(Unknown Source) 
at java.lang.Shutdown.exit(Unknown Source) 
- locked [0x00000000f0051870] (a java.lang.Class for java.lang.Shutdown) 
at java.lang.Runtime.exit(Unknown Source) 
at java.lang.System.exit(Unknown Source) 
at MyAppUtilUtil.hardCommitLocalCache(MyAppUtilUtil.java:152) 
at MyAppUtilUtil.synchronizeP1Index(MyAppUtilUtil.java:372) 
at MyAppUtil$MyAppUtilUtilTask$MyTaskThread.doRun(MyAppUtilUtil.java:352) 
at MyAppUtil$MyAppUtilUtilTask$MyTaskThread.run(MyAppUtilUtil.java:324
Look at jetty ShutdownThread, it's a ShutdownHook added by jetty.server.Server. It calls MyServletContextListener.contextDestroyed, which will call same method MyAppUtil.shutdown to do clean up which will call System.exit.
Now the cause of the hang is obvious.
ShutdownThread tries to call Shutdown.exit, and tries to get the lock of Shutdown instance to enter synchronized (lock) block. But it can't as the lock is already owned by MyTaskThread thread, so ShutdownThread will wait for ever.

MyTaskThread thread owns the lock of Shutdown instance and wait for jetty ShutdownThread finish. It will also wait for ever. This cause the System.ext hang.
ShutdownThread
Look at jetty ShutdownThread, it's a ShutdownHook added by jetty.server.Server. It calls MyServletContextListener.contextDestroyed, which will call same method MyAppUtil.shutdown to do clean up which will call System.exit.
Now the cause of the hang is obvious.
ShutdownThread tries to call Shutdown.exit, and tries to get the lock of Shutdown instance to enter synchronized (lock) block. But it can't as the lock is already owned by MyTaskThread thread, so ShutdownThread will wait for ever.

MyTaskThread thread owns the lock of Shutdown instance and wait for jetty ShutdownThread finish. It will also wait for ever. This cause the System.ext hang.

State: Waiting on monitor
Monitor: Waiting for Monitor Lock on 0x00000000f0051870
at java.lang.Shutdown.exit(Unknown Source) 
- waiting to lock [0x00000000f0051870] (a java.lang.Class for java.lang.Shutdown) 
at java.lang.Runtime.exit(Unknown Source) 
at java.lang.System.exit(Unknown Source) 
at MyAppUtilUtil.hardCommitLocalCache(MyAppUtilUtil.java:152) 
at MyAppUtil$MyAppUtilUtilTask.shutdown(MyAppUtilUtil.java:126) 
at MyAppUtil.shutdown(MyAppUtilUtil.java:64) 
- locked [0x00000000f04cbfc0] (a MyAppUtil) 
at MyServletContextListener.contextDestroyed(MyServletContextListener.java:18) 
at org.eclipse.jetty.server.handler.ContextHandler.doStop(ContextHandler.java:813) 
at org.eclipse.jetty.servlet.ServletContextHandler.doStop(ServletContextHandler.java:158) 
at org.eclipse.jetty.webapp.WebAppContext.doStop(WebAppContext.java:504) 
at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89) 
- locked [0x00000000f010ce40] (a java.lang.Object) 
at org.eclipse.jetty.server.handler.HandlerCollection.doStop(HandlerCollection.java:250) 
at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89) 
- locked [0x00000000f00430c8] (a java.lang.Object) 
at org.eclipse.jetty.server.handler.HandlerWrapper.doStop(HandlerWrapper.java:107) 
at org.eclipse.jetty.server.Server.doStop(Server.java:338) 
at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89) 
- locked [0x00000000f00404d8] (a java.lang.Object) 
at org.eclipse.jetty.util.thread.ShutdownThread.run(ShutdownThread.java:131)
To fix the problem, In MyServletContextListener.contextDestroyed, I do all nedded cleanup but don't call System.exit.

Resource
Post a Comment

Labels

Java (159) Lucene-Solr (110) All (60) Interview (59) J2SE (53) Algorithm (37) Eclipse (35) Soft Skills (35) Code Example (31) Linux (26) JavaScript (23) Spring (22) Windows (22) Web Development (20) Tools (19) Nutch2 (18) Bugs (17) Debug (15) Defects (14) Text Mining (14) J2EE (13) Network (13) PowerShell (11) Chrome (9) Continuous Integration (9) How to (9) Learning code (9) Performance (9) UIMA (9) html (9) Design (8) Dynamic Languages (8) Http Client (8) Maven (8) Security (8) Trouble Shooting (8) bat (8) blogger (8) Big Data (7) Google (7) Guava (7) JSON (7) Problem Solving (7) ANT (6) Coding Skills (6) Database (6) Scala (6) Shell (6) css (6) Algorithm Series (5) Cache (5) IDE (5) Lesson Learned (5) Miscs (5) Programmer Skills (5) System Design (5) Tips (5) adsense (5) xml (5) AIX (4) Code Quality (4) GAE (4) Git (4) Good Programming Practices (4) Jackson (4) Memory Usage (4) OpenNLP (4) Project Managment (4) Python (4) Spark (4) Testing (4) ads (4) regular-expression (4) Android (3) Apache Spark (3) Become a Better You (3) Concurrency (3) Eclipse RCP (3) English (3) Firefox (3) Happy Hacking (3) IBM (3) J2SE Knowledge Series (3) JAX-RS (3) Jetty (3) Restful Web Service (3) Script (3) regex (3) seo (3) .Net (2) Android Studio (2) Apache (2) Apache Procrun (2) Architecture (2) Batch (2) Build (2) Building Scalable Web Sites (2) C# (2) C/C++ (2) CSV (2) Career (2) Cassandra (2) Distributed (2) Fiddler (2) Google Drive (2) Gson (2) Html Parser (2) Http (2) Image Tools (2) JQuery (2) Jersey (2) LDAP (2) Life (2) Logging (2) Software Issues (2) Storage (2) Text Search (2) xml parser (2) AOP (1) Application Design (1) AspectJ (1) Bit Operation (1) Chrome DevTools (1) Cloud (1) Codility (1) Data Mining (1) Data Structure (1) ExceptionUtils (1) Exif (1) Feature Request (1) FindBugs (1) Greasemonkey (1) HTML5 (1) Httpd (1) I18N (1) IBM Java Thread Dump Analyzer (1) JDK Source Code (1) JDK8 (1) JMX (1) Lazy Developer (1) Mac (1) Machine Learning (1) Mobile (1) My Plan for 2010 (1) Netbeans (1) Notes (1) Operating System (1) Perl (1) Problems (1) Product Architecture (1) Programming Life (1) Quality (1) Redhat (1) Redis (1) Review (1) RxJava (1) Solutions logs (1) Team Management (1) Thread Dump Analyzer (1) Troubleshooting (1) Visualization (1) boilerpipe (1) htm (1) ongoing (1) procrun (1) rss (1)

Popular Posts