Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improve shutdown of NIO receiver so that sockets are closed cleanly.
This is essentially the patch that Filip suggested in the bug report with some additional refactoring by me.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1629776 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Oct 6, 2014
1 parent 1c3d3ba commit 4a760b9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
14 changes: 8 additions & 6 deletions java/org/apache/catalina/tribes/transport/nio/NioReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.catalina.tribes.transport.Constants;
import org.apache.catalina.tribes.transport.ReceiverBase;
import org.apache.catalina.tribes.transport.RxTaskPool;
import org.apache.catalina.tribes.util.ExceptionUtils;
import org.apache.catalina.tribes.util.StringManager;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
Expand Down Expand Up @@ -315,12 +316,7 @@ protected void listen() throws Exception {
} catch (java.nio.channels.CancelledKeyException nx) {
log.warn(sm.getString("NioReceiver.clientDisconnect"));
} catch (Throwable t) {
if (t instanceof ThreadDeath) {
throw (ThreadDeath) t;
}
if (t instanceof VirtualMachineError) {
throw (VirtualMachineError) t;
}
ExceptionUtils.handleThrowable(t);
log.error(sm.getString("NioReceiver.requestError"), t);
}

Expand Down Expand Up @@ -388,6 +384,12 @@ private void closeSelector() throws IOException {
} catch (ClosedSelectorException ignore){
// Ignore
}
try {
selector.selectNow();
} catch (Throwable t){
ExceptionUtils.handleThrowable(t);
// Ignore everything else
}
selector.close();
}

Expand Down
42 changes: 42 additions & 0 deletions java/org/apache/catalina/tribes/util/ExceptionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.tribes.util;

/**
* Utilities for handling Throwables and Exceptions.
*/
public class ExceptionUtils {

/**
* Checks whether the supplied Throwable is one that needs to be
* rethrown and swallows all others.
* @param t the Throwable to check
*/
public static void handleThrowable(Throwable t) {
if (t instanceof ThreadDeath) {
throw (ThreadDeath) t;
}
if (t instanceof StackOverflowError) {
// Swallow silently - it should be recoverable
return;
}
if (t instanceof VirtualMachineError) {
throw (VirtualMachineError) t;
}
// All other instances of Throwable will be silently swallowed
}
}
8 changes: 8 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@
</fix>
</changelog>
</subsection>
<subsection name="Tribes">
<changelog>
<fix>
<bug>45282</bug>: Improve shutdown of NIO receiver so that sockets are
closed cleanly. (fhanik/markt)
</fix>
</changelog>
</subsection>
</section>
<section name="Tomcat 8.0.14 (markt)" rtext="2014-09-29">
<subsection name="Other">
Expand Down

0 comments on commit 4a760b9

Please sign in to comment.