Skip to content

Commit

Permalink
Dont log (Stackless)ClosedChannelException since that's likely closed…
Browse files Browse the repository at this point in the history
… because of client

#302
  • Loading branch information
Skidamek committed Nov 23, 2024
1 parent ce1df03 commit 0fb2827
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.ClosedChannelException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.*;
Expand Down Expand Up @@ -133,22 +134,29 @@ private void sendFile(ChannelHandlerContext context, String requestUri) {
context.pipeline().firstContext().writeAndFlush(fileRegion, context.newProgressivePromise())
.addListener(future -> {
try {
if (!future.isSuccess()) {
LOGGER.error("Error writing to channel: {} path: {} {}", future.cause().getMessage(), path, future.cause());
Throwable cause = future.cause();
if (cause != null && !(cause instanceof ClosedChannelException)) {
LOGGER.error("Error writing to channel: {} path: {}", cause, path);
}
raf.close(); // Ensure RandomAccessFile is closed
} catch (IOException e) {
LOGGER.error("Failed to close RandomAccessFile: {} of path: {} {}", e.getMessage(), path, e);
} catch (Exception e) {
LOGGER.error("Unexpected error: {}", e.getMessage(), e);
} finally {
context.pipeline().firstContext().channel().close(); // Close channel if necessary
try {
raf.close(); // Ensure RandomAccessFile is closed
} catch (IOException e) {
LOGGER.error("Failed to close RandomAccessFile: {} of path: {}", e, path);
}

// Finally close channel
context.pipeline().firstContext().channel().close();
}
});
} catch (Exception e) {
LOGGER.error("Error during file: {} handling {}", path, e);
try {
raf.close(); // Ensure RandomAccessFile is closed in case of exceptions
} catch (IOException closeEx) {
LOGGER.error("Failed to close RandomAccessFile: {} of path: {} {}", closeEx.getMessage(), path, closeEx);
LOGGER.error("Failed to close RandomAccessFile: {} of path: {}", closeEx, path);
}
}
}
Expand Down

0 comments on commit 0fb2827

Please sign in to comment.