View | Details | Raw Unified | Return to bug 63022
Collapse All | Expand All

(-)java/org/apache/tomcat/util/net/AbstractEndpoint.java (+6 lines)
Lines 1302-1307 Link Here
1302
1302
1303
    protected abstract boolean setSocketOptions(U socket);
1303
    protected abstract boolean setSocketOptions(U socket);
1304
1304
1305
    /**
1306
     * Close the socket when the connection has to be immediately closed when
1307
     * an error occurs while configuring the accepted socket, allocating
1308
     * a wrapper for the socket, or trying to dispatch it for processing.
1309
     * @param socket The newly accepted socket
1310
     */
1305
    protected abstract void closeSocket(U socket);
1311
    protected abstract void closeSocket(U socket);
1306
1312
1307
    protected void destroySocket(U socket) {
1313
    protected void destroySocket(U socket) {
(-)java/org/apache/tomcat/util/net/Nio2Endpoint.java (-49 / +45 lines)
Lines 226-232 Link Here
226
                    // Then close all active connections if any remain
226
                    // Then close all active connections if any remain
227
                    try {
227
                    try {
228
                        for (Nio2Channel channel : getHandler().getOpenSockets()) {
228
                        for (Nio2Channel channel : getHandler().getOpenSockets()) {
229
                            closeSocket(channel.getSocket());
229
                            channel.getSocket().close();;
230
                        }
230
                        }
231
                    } catch (Throwable t) {
231
                    } catch (Throwable t) {
232
                        ExceptionUtils.handleThrowable(t);
232
                        ExceptionUtils.handleThrowable(t);
Lines 378-424 Link Here
378
    }
378
    }
379
379
380
380
381
    private void closeSocket(SocketWrapperBase<Nio2Channel> socket) {
382
        if (log.isDebugEnabled()) {
383
            log.debug("Calling [" + this + "].closeSocket([" + socket + "],[" + socket.getSocket() + "])",
384
                    new Exception());
385
        }
386
        if (socket == null) {
387
            return;
388
        }
389
        try {
390
            getHandler().release(socket);
391
        } catch (Throwable e) {
392
            ExceptionUtils.handleThrowable(e);
393
            if (log.isDebugEnabled()) log.error("",e);
394
        }
395
        Nio2SocketWrapper nio2Socket = (Nio2SocketWrapper) socket;
396
        try {
397
            synchronized (socket.getSocket()) {
398
                if (!nio2Socket.closed) {
399
                    nio2Socket.closed = true;
400
                    countDownConnection();
401
                }
402
                if (socket.getSocket().isOpen()) {
403
                    socket.getSocket().close(true);
404
                }
405
            }
406
        } catch (Throwable e) {
407
            ExceptionUtils.handleThrowable(e);
408
            if (log.isDebugEnabled()) log.error("",e);
409
        }
410
        try {
411
            if (nio2Socket.getSendfileData() != null
412
                    && nio2Socket.getSendfileData().fchannel != null
413
                    && nio2Socket.getSendfileData().fchannel.isOpen()) {
414
                nio2Socket.getSendfileData().fchannel.close();
415
            }
416
        } catch (Throwable e) {
417
            ExceptionUtils.handleThrowable(e);
418
            if (log.isDebugEnabled()) log.error("",e);
419
        }
420
    }
421
422
    protected class Nio2Acceptor extends Acceptor<AsynchronousSocketChannel>
381
    protected class Nio2Acceptor extends Acceptor<AsynchronousSocketChannel>
423
        implements CompletionHandler<AsynchronousSocketChannel, Void> {
382
        implements CompletionHandler<AsynchronousSocketChannel, Void> {
424
383
Lines 928-941 Link Here
928
887
929
888
930
        @Override
889
        @Override
931
        public void close() throws IOException {
890
        public void close() {
932
            getSocket().close();
891
            if (log.isDebugEnabled()) {
892
                log.debug("Calling [" + getEndpoint() + "].closeSocket([" + this + "],[" + getSocket() + "])",
893
                        new Exception());
894
            }
895
            try {
896
                getEndpoint().getHandler().release(this);
897
            } catch (Throwable e) {
898
                ExceptionUtils.handleThrowable(e);
899
                if (log.isDebugEnabled()) {
900
                    log.error("", e);
901
                }
902
            }
903
            try {
904
                synchronized (getSocket()) {
905
                    if (!closed) {
906
                        closed = true;
907
                        getEndpoint().countDownConnection();
908
                    }
909
                    if (getSocket().isOpen()) {
910
                        getSocket().close(true);
911
                    }
912
                }
913
            } catch (Throwable e) {
914
                ExceptionUtils.handleThrowable(e);
915
                if (log.isDebugEnabled()) {
916
                    log.error("", e);
917
                }
918
            }
919
            try {
920
                SendfileData data = getSendfileData();
921
                if (data != null && data.fchannel != null && data.fchannel.isOpen()) {
922
                    data.fchannel.close();
923
                }
924
            } catch (Throwable e) {
925
                ExceptionUtils.handleThrowable(e);
926
                if (log.isDebugEnabled()) {
927
                    log.error("", e);
928
                }
929
            }
933
        }
930
        }
934
931
935
936
        @Override
932
        @Override
937
        public boolean isClosed() {
933
        public boolean isClosed() {
938
            return closed || !getSocket().isOpen();
934
            return closed;
939
        }
935
        }
940
936
941
937
Lines 1775-1781 Link Here
1775
                    }
1771
                    }
1776
                    if (state == SocketState.CLOSED) {
1772
                    if (state == SocketState.CLOSED) {
1777
                        // Close socket and pool
1773
                        // Close socket and pool
1778
                        closeSocket(socketWrapper);
1774
                        socketWrapper.close();;
1779
                        if (running && !paused) {
1775
                        if (running && !paused) {
1780
                            if (!nioChannels.push(socketWrapper.getSocket())) {
1776
                            if (!nioChannels.push(socketWrapper.getSocket())) {
1781
                                socketWrapper.getSocket().free();
1777
                                socketWrapper.getSocket().free();
Lines 1785-1791 Link Here
1785
                        launch = true;
1781
                        launch = true;
1786
                    }
1782
                    }
1787
                } else if (handshake == -1 ) {
1783
                } else if (handshake == -1 ) {
1788
                    closeSocket(socketWrapper);
1784
                    socketWrapper.close();;
1789
                    if (running && !paused) {
1785
                    if (running && !paused) {
1790
                        if (!nioChannels.push(socketWrapper.getSocket())) {
1786
                        if (!nioChannels.push(socketWrapper.getSocket())) {
1791
                            socketWrapper.getSocket().free();
1787
                            socketWrapper.getSocket().free();
Lines 1797-1803 Link Here
1797
            } catch (Throwable t) {
1793
            } catch (Throwable t) {
1798
                log.error(sm.getString("endpoint.processing.fail"), t);
1794
                log.error(sm.getString("endpoint.processing.fail"), t);
1799
                if (socketWrapper != null) {
1795
                if (socketWrapper != null) {
1800
                    closeSocket(socketWrapper);
1796
                    ((Nio2SocketWrapper) socketWrapper).close();
1801
                }
1797
                }
1802
            } finally {
1798
            } finally {
1803
                if (launch) {
1799
                if (launch) {

Return to bug 63022