Bug 57959

Summary: Deadlock in FileHandler.java when log is rotated
Product: Tomcat 7 Reporter: Alex Chachanashvili <achacha>
Component: CatalinaAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: major    
Priority: P2    
Version: 7.0.62   
Target Milestone: ---   
Hardware: PC   
OS: Linux   

Description Alex Chachanashvili 2015-05-27 19:21:22 UTC
Tomcat 7 will deadlock when a log file is rotated.  This issue has been fixed in latest tomcat 6 and tomcat 8 but not in tomcat 7.

The issue is in following code (apache-tomcat-7.0.62-src/java/org/apache/juli/FileHandler.java:188)


                } finally {
                    writerLock.writeLock().unlock();
                    // Down grade to read-lock. This ensures the writer remains valid
                    // until the log message is written
                    writerLock.readLock().lock();
                }
            }


Despite the comment the order is wrong, you have to acquire a read lock then release write lock to make sure the race condition does not occur.

The correct way to do it is:

                } finally {
                    writerLock.readLock().lock();
                    writerLock.writeLock().unlock();
                }
            }

In tomcat 6 (apache-tomcat-6.0.44-src/java/org/apache/juli/FileHandler.java:187) and tomcat 8 (apache-tomcat-8.0.22-src/java/org/apache/juli/FileHandler.java:186) the order is correct.
Comment 1 Violeta Georgieva 2015-05-28 06:59:37 UTC
Thanks for the report.
The fix is provided for Tomcat 7 trunk and will be available with 7.0.63 onwards.

Regards,
Violeta