Bug 45282 - NioReceiver doesn't close cleanly, leaving sockets in CLOSE_WAIT indefinitely.
Summary: NioReceiver doesn't close cleanly, leaving sockets in CLOSE_WAIT indefinitely.
Alias: None
Product: Tomcat 6
Classification: Unclassified
Component: Cluster (show other bugs)
Version: 6.0.16
Hardware: PC Linux
: P2 enhancement with 1 vote (vote)
Target Milestone: default
Assignee: Tomcat Developers Mailing List
Depends on:
Reported: 2008-06-25 14:20 UTC by Robert Newson
Modified: 2014-10-06 22:14 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description Robert Newson 2008-06-25 14:20:20 UTC
We see sockets in CLOSE_WAIT when NioReceiver.stop() is called. 

Apparently, according to;


one must remain registered with the selector, close, then select again (with a timeout), and then the channel is properly drained and a tcp reset occurs.

I'm working on a patch.
Comment 1 Robert Newson 2008-06-25 14:36:16 UTC
Ah, forgive me, linger is enabled with a 3 second timeout. In my tests I see them hanging around but if I wait they do close.

The pattern for draining the queue completely is still a useful enhancement and would allow correct behavior without linger.
Comment 2 Mark Thomas 2008-10-01 08:36:27 UTC
Any progress on your patch?
Comment 3 Filip Hanik 2008-10-01 09:24:59 UTC
I believe a patch could look something like

Index: org/apache/catalina/tribes/transport/nio/NioReceiver.java
--- org/apache/catalina/tribes/transport/nio/NioReceiver.java   (revision 695909)
+++ org/apache/catalina/tribes/transport/nio/NioReceiver.java   (working copy)
@@ -377,6 +377,7 @@
                 log.warn("Unable to cleanup on selector close.",ignore);
         }catch ( ClosedSelectorException ignore){}
+        try {selector.selectNow();}catch (Throwable ignore){}

and all we do, is issue a selectNow statement after we have cancelled the keys


Comment 4 Mark Thomas 2014-10-06 22:14:36 UTC
Filips patch has been applied to 8.0.x for 8.0.15 onwards.

I do not propose to back-port this.