changeset 46045:ef191ce8f33f

8185092: Data race in FilterOutputStream.close Summary: Change boolean instance variable "closed" to an AtomicBoolean. Reviewed-by: martin, alanb, redestad
author bpb
date Thu, 27 Jul 2017 13:13:19 -0700
parents 55b0a570d3d7
children 54ca7058579d
files jdk/src/java.base/share/classes/java/io/FilterOutputStream.java
diffstat 1 files changed, 12 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/jdk/src/java.base/share/classes/java/io/FilterOutputStream.java	Wed Jul 26 14:09:20 2017 -0700
+++ b/jdk/src/java.base/share/classes/java/io/FilterOutputStream.java	Thu Jul 27 13:13:19 2017 -0700
@@ -50,7 +50,12 @@
     /**
      * Whether the stream is closed; implicitly initialized to false.
      */
-    private boolean closed;
+    private volatile boolean closed;
+
+    /**
+     * Object used to prevent a race on the 'closed' instance variable.
+     */
+    private final Object closeLock = new Object();
 
     /**
      * Creates an output stream filter built on top of the specified
@@ -165,7 +170,12 @@
         if (closed) {
             return;
         }
-        closed = true;
+        synchronized (closeLock) {
+            if (closed) {
+                return;
+            }
+            closed = true;
+        }
 
         Throwable flushException = null;
         try {