From 386961706badced15f329c07ef6d4e558eb26a8e Mon Sep 17 00:00:00 2001 From: kkolinko Date: Thu, 4 Mar 2010 00:46:44 +0000 Subject: [PATCH] Followup to r918594 Be more strict with try/finally when locking git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@918799 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/juli/FileHandler.java | 64 +++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/java/org/apache/juli/FileHandler.java b/java/org/apache/juli/FileHandler.java index b6564dc94..8a1f0bdee 100644 --- a/java/org/apache/juli/FileHandler.java +++ b/java/org/apache/juli/FileHandler.java @@ -138,39 +138,43 @@ public class FileHandler // Update to writeLock before we switch writerLock.readLock().unlock(); writerLock.writeLock().lock(); - // Make sure another thread hasn't already done this - if (!date.equals(tsDate)) { - closeWriter(); - date = tsDate; - openWriter(); + try { + // Make sure another thread hasn't already done this + if (!date.equals(tsDate)) { + closeWriter(); + date = tsDate; + openWriter(); + } + // Down grade to read-lock. This ensures the writer remains valid + // until the log message is written + writerLock.readLock().lock(); + } finally { + writerLock.writeLock().unlock(); } - // Down grade to read-lock. This ensures the writer remains valid - // until the log message is written - writerLock.readLock().lock(); - writerLock.writeLock().unlock(); } - String result = null; try { - result = getFormatter().format(record); - } catch (Exception e) { - reportError(null, e, ErrorManager.FORMAT_FAILURE); - writerLock.readLock().unlock(); - return; - } - - try { - if (writer!=null) { - writer.write(result); - if (bufferSize < 0) { - writer.flush(); + String result = null; + try { + result = getFormatter().format(record); + } catch (Exception e) { + reportError(null, e, ErrorManager.FORMAT_FAILURE); + return; + } + + try { + if (writer!=null) { + writer.write(result); + if (bufferSize < 0) { + writer.flush(); + } + } else { + reportError("FileHandler is closed or not yet initialized, unable to log ["+result+"]", null, ErrorManager.WRITE_FAILURE); } - } else { - reportError("FileHandler is closed or not yet initialized, unable to log ["+result+"]", null, ErrorManager.WRITE_FAILURE); + } catch (Exception e) { + reportError(null, e, ErrorManager.WRITE_FAILURE); + return; } - } catch (Exception e) { - reportError(null, e, ErrorManager.WRITE_FAILURE); - return; } finally { writerLock.readLock().unlock(); } @@ -190,8 +194,8 @@ public class FileHandler protected void closeWriter() { + writerLock.writeLock().lock(); try { - writerLock.writeLock().lock(); if (writer == null) return; writer.write(getFormatter().getTail(this)); @@ -213,8 +217,8 @@ public class FileHandler @Override public void flush() { + writerLock.readLock().lock(); try { - writerLock.readLock().lock(); if (writer == null) return; writer.flush(); @@ -319,13 +323,13 @@ public class FileHandler dir.mkdirs(); // Open the current log file + writerLock.writeLock().lock(); try { String pathname = dir.getAbsolutePath() + File.separator + prefix + date + suffix; String encoding = getEncoding(); FileOutputStream fos = new FileOutputStream(pathname, true); OutputStream os = bufferSize>0?new BufferedOutputStream(fos,bufferSize):fos; - writerLock.writeLock().lock(); writer = new PrintWriter( (encoding != null) ? new OutputStreamWriter(os, encoding) : new OutputStreamWriter(os), false); -- 2.11.0