diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java index ca67f3c2b126..7111d1372ce6 100644 --- a/java/org/apache/tomcat/util/net/NioEndpoint.java +++ b/java/org/apache/tomcat/util/net/NioEndpoint.java @@ -552,6 +552,26 @@ public void run() { } + private void close(NioChannel socket, SelectionKey key) { + try { + if (socket.getPoller().cancelledKey(key) != null) { + // SocketWrapper (attachment) was removed from the + // key - recycle the key. This can only happen once + // per attempted closure so it is used to determine + // whether or not to return the key to the cache. + // We do NOT want to do this more than once - see BZ + // 57340 / 57943. + if (running && !paused) { + if (!nioChannels.push(socket)) { + socket.free(); + } + } + } + } catch (Exception x) { + log.error("",x); + } + } + private void closeSocket(SocketChannel socket) { try { socket.socket().close(); @@ -984,7 +1004,7 @@ public SendfileState processSendfile(SelectionKey sk, NioSocketWrapper socketWra if (log.isDebugEnabled()) { log.debug("Send file connection is being closed"); } - cancelledKey(sk); + close(sc, sk); } } return SendfileState.DONE; @@ -1001,11 +1021,19 @@ public SendfileState processSendfile(SelectionKey sk, NioSocketWrapper socketWra } } catch (IOException x) { if (log.isDebugEnabled()) log.debug("Unable to complete sendfile request:", x); - cancelledKey(sk); + if (!calledByProcessor && sc != null) { + close(sc, sk); + } else { + cancelledKey(sk); + } return SendfileState.ERROR; } catch (Throwable t) { log.error("", t); - cancelledKey(sk); + if (!calledByProcessor && sc != null) { + close(sc, sk); + } else { + cancelledKey(sk); + } return SendfileState.ERROR; } finally { if (sc!=null) sc.setSendFile(false); @@ -1547,25 +1575,6 @@ public void run() { } } - private void close(NioChannel socket, SelectionKey key) { - try { - if (socket.getPoller().cancelledKey(key) != null) { - // SocketWrapper (attachment) was removed from the - // key - recycle the key. This can only happen once - // per attempted closure so it is used to determine - // whether or not to return the key to the cache. - // We do NOT want to do this more than once - see BZ - // 57340 / 57943. - if (running && !paused) { - if (!nioChannels.push(socket)) { - socket.free(); - } - } - } - } catch (Exception x) { - log.error("",x); - } - } } // ----------------------------------------------- SendfileData Inner Class