1

My application java, is running in server, openning port and made connection with devices by socket. Everythings went run till a certain point, lot of connection stay in CLOSE_WAIT, even if my application finish the process on the packet I receive.

What I remark is that CPU began to use double ressource, open file are increasing, and number of CLOSE_WAIT status are increasing also.

In the wireshark, the packet sended which leave a CLOSE_WAIT status, we see that server didn't send FIN to the client.

PS: I'm on ubuntu 14.04 trusty server, I'm using Netty 3.10.1

Here is the code where I Made Pipeline :

@Override
public ChannelPipeline getPipeline() {
    ChannelPipeline pipeline = Channels.pipeline();
    if (resetDelay != null) {
        pipeline.addLast("idleHandler", new IdleStateHandler(GlobalTimer.getTimer(), resetDelay, 0, 0));
    }
    pipeline.addLast("openHandler", new OpenChannelHandler(server));
    if (loggerEnabled) {
        pipeline.addLast("logger", new StandardLoggingHandler());
    }
    addSpecificHandlers(pipeline);
    if (filterHandler != null) {
        pipeline.addLast("filter", filterHandler);
    }
    if (reinitializeHandler != null) {
        pipeline.addLast("reinitialize", reinitializeHandler);
    }
    if (refineHandler != null) {
        pipeline.addLast("refine", refineHandler);
    }
    if (noFilterHandler != null) {
        pipeline.addLast("nofilter", noFilterHandler);
    }
    if (specificFilterHandler != null) {
        pipeline.addLast("specificfilter", specificFilterHandler);
    }
    if (reverseGeocoder != null) {
        pipeline.addLast("geocoder", new ReverseGeocoderHandler(reverseGeocoder, processInvalidPositions));
    }
    pipeline.addLast("handler", new TrackerEventHandler(dataManager));
    return pipeline;
}
EspritBat
  • 21
  • 3

2 Answers2

0

CLOSE_WAIT means your program is still running, and hasn't closed the socket (and the kernel is waiting for it to do so). Add -p to netstat to get the pid, and then kill it more forcefully (with SIGKILL if needed). That should get rid of your CLOSE_WAIT sockets. You can also use ps to find the pid.

SO_REUSEADDR is for servers and TIME_WAIT sockets, so doesn't apply here.

Refer to this thread for other responses.

Check your application code to check if you are explicitly calling close on all the sockets created once you are done handling the client session.

Community
  • 1
  • 1
The Roy
  • 2,178
  • 1
  • 17
  • 33
  • The application are communicating with lot of device, as I said every thing went correctly till a certain point, which mean that connection is closed by my app. Also I cannot do a kill because my app is communicating with other device that server are closing connection with them correctly. – EspritBat Mar 03 '16 at 17:41
  • 1
    Can you share snippets of your code which is responsible for client handling? – The Roy Mar 04 '16 at 00:55
  • 1
    We don't have client code, because it is handled by a device (GPS) that have a program made by our provider. But we know that problem come from our server who SOMETIMES didn't close the connection (by Netty) – EspritBat Mar 04 '16 at 08:51
  • @EspritBat can you add your code where you build the netty pipeline? – Moh-Aw Mar 04 '16 at 08:52
  • @EspritBat As the others have already pointed out, there seems to be a way in your program (for example an exception) where the channel does not get closed. – Moh-Aw Mar 04 '16 at 10:48
  • @EJP : I override tree function that close connection `channelIdle(ChannelHandlerContext ctx, IdleStateEvent e)` & `exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)` & `channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e)` when an IOException is made, connection are closed. @Moh-Aw : In the case I'm talking about, all the pipeline is working well, then when I finish correctly the last step `TrackerEventHandler`, then the connection are not closed. so there is no exception in my part, and if netty generate one, then `exceptionCaught` handle it. – EspritBat Mar 04 '16 at 16:24
  • I don't think you can know this way that the client has closed the connection. Essentially if will need to find out a way to read/write from the socket at regular intervals. When you do that and if the client socket is not available, you will get TCP status 0 or connection is reset by peer. – The Roy Mar 04 '16 at 16:35
  • I know that client send close from wireshark, and I know that server don't. for the reste of your comment I'm afraid that I don't get it. excuse my low knowledge. – EspritBat Mar 04 '16 at 17:01
  • 1
    Even though TCP/IP is "connection oriented" protocol, normally no data is sent over an idle connection. You can have a socket open for a year without a single bit sent over it by the IP stack. In order to notice that a connection is lost, you have to send some data on the application level.(*) Try with an example to implement some kind of application level keepalive mechanism. – The Roy Mar 04 '16 at 17:15
0

Your application is leaking sockets by failing to close them. So close them. All of them. In finally blocks. When you read end of stream or get any IOException operatng on the socket.

user207421
  • 305,947
  • 44
  • 307
  • 483