diff --git a/core/src/main/java/io/undertow/conduits/FixedLengthStreamSourceConduit.java b/core/src/main/java/io/undertow/conduits/FixedLengthStreamSourceConduit.java index e77f22305c..4d60f7367d 100644 --- a/core/src/main/java/io/undertow/conduits/FixedLengthStreamSourceConduit.java +++ b/core/src/main/java/io/undertow/conduits/FixedLengthStreamSourceConduit.java @@ -126,7 +126,7 @@ public long transferTo(final long position, final long count, final FileChannel try { return res = next.transferTo(position, min(count, val & MASK_COUNT), target); } catch (IOException | RuntimeException | Error e) { - IoUtils.safeClose(exchange.getConnection()); + closeConnection(); transferError = e; throw e; } finally { @@ -151,7 +151,7 @@ public long transferTo(final long count, final ByteBuffer throughBuffer, final S try { return res = next.transferTo(min(count, val & MASK_COUNT), throughBuffer, target); } catch (IOException | RuntimeException | Error e) { - IoUtils.safeClose(exchange.getConnection()); + closeConnection(); transferError = e; throw e; } finally { @@ -219,7 +219,7 @@ public long read(final ByteBuffer[] dsts, final int offset, final int length) th // the total buffer space is less than the remaining count. return res = next.read(dsts, offset, length); } catch (IOException | RuntimeException | Error e) { - IoUtils.safeClose(exchange.getConnection()); + closeConnection(); readError = e; throw e; } finally { @@ -257,7 +257,7 @@ public int read(final ByteBuffer dst) throws IOException { return res = next.read(dst); } } catch (IOException | RuntimeException | Error e) { - IoUtils.safeClose(exchange.getConnection()); + closeConnection(); readError = e; throw e; } finally { @@ -302,7 +302,7 @@ public void awaitReadable(final long time, final TimeUnit timeUnit) throws IOExc try { next.awaitReadable(time, timeUnit); } catch (IOException | RuntimeException | Error e) { - IoUtils.safeClose(exchange.getConnection()); + closeConnection(); throw e; } } @@ -365,4 +365,11 @@ private void invokeFinishListener() { finishListener.handleEvent(this); } + private void closeConnection() { + HttpServerExchange exchange = this.exchange; + if (exchange != null) { + IoUtils.safeClose(exchange.getConnection()); + } + } + }