reta commented on code in PR #3541:
URL: https://github.com/apache/cxf/pull/3541#discussion_r4189024090


##########
rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingOutputStream.java:
##########
@@ -71,4 +73,60 @@ public void writeCacheTo(StringBuilder out, String 
charsetName, long limit) thro
         super.writeCacheTo(out, charsetName, limit);
         skipFlushingFlowThroughStream = false;
     }
+
+
+    /**
+     * We override the write() methods in order to catch some error that would 
not
+     * close the CachedOutputStream (ex. IOException "Connection reset by 
peer".
+     * Causing ghost or delayed OUT log and possible memory-leak due to 
DelayedCachedOutputStreamCleaner
+     */
+    @Override
+    public void write(byte[] b) throws IOException {
+        try {
+            super.write(b);
+        } catch (RuntimeException | IOException ex) {
+            handleIoException();
+            throw ex;
+        }
+    }
+
+    @Override
+    public void write(byte[] b, int off, int len) throws IOException {
+        try {
+            super.write(b, off, len);
+        } catch (RuntimeException | IOException ex) {
+            handleIoException();
+            throw ex;
+        }
+    }
+
+    @Override
+    public void write(int b) throws IOException {
+        try {
+            super.write(b);
+        } catch (RuntimeException | IOException ex) {
+            handleIoException();
+            throw ex;
+        }
+    }
+
+    @Override
+    public void close() throws IOException {
+        // Ensure closing only one time
+        if (closed.compareAndSet(false, true)) {
+            super.close();
+        }
+    }
+
+    // See CXF-9251 ... introduced due to IOException "Connection reset by 
peer"
+    private void handleIoException() {
+        try {
+            // Close this CachedOutputStream
+            // Write method maybe called more than one time... but we already 
consume the stream the first time
+            // So additional call to this.close would produce nothing
+            this.close();
+        } catch (Exception ignored) {
+            // ignore

Review Comment:
   Let's just not ignored this one but add it to suppressed exception list



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to