Bug 64951 - Websocket connection to unresolvable host causes fd leaks
Summary: Websocket connection to unresolvable host causes fd leaks
Status: RESOLVED FIXED
Alias: None
Product: Tomcat 8
Classification: Unclassified
Component: WebSocket (show other bugs)
Version: 8.5.37
Hardware: PC Linux
: P2 normal (vote)
Target Milestone: ----
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-12-02 12:18 UTC by Maurizio Adami
Modified: 2020-12-02 17:32 UTC (History)
0 users



Attachments
patch for WsWebSocketContainer.java 8.5.37 fd leaks (2.45 KB, patch)
2020-12-02 12:18 UTC, Maurizio Adami
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Maurizio Adami 2020-12-02 12:18:03 UTC
Created attachment 37583 [details]
patch for WsWebSocketContainer.java 8.5.37 fd leaks

An attempt to create a new websocket connection with WebSocketContainer.connectToServer to an unresolvable host raises java.nio.channels.UnresolvedAddressException, but leaves some socket resources allocated. This can be verified by putting the above connect method in a loop and monitoring the open files of the java process.
On linux this can be done with e.g. lsof: the number of socket fd increase on every iteration up to the OS limit, then "Too many open files" IOExceptions are raised.

Browsing the org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive() code
I found that the call

        // Open the connection
        Future<Void> fConnect = socketChannel.connect(sa);

raises the UnresolvedAddressException, leaving the socketChannel object opened.

I think the same problem could arise with a proxy server that return a status code != 200, handled in lines 313-317.

I tried to solve extending the try block to include the above call, changing the corresponding finally block to cover extra cases.
Comment 1 Mark Thomas 2020-12-02 17:32:48 UTC
Thanks for the report and for the patch. The analysis looks good to me. I've applied the patch.

Fixed in:
- 10.0.x for 10.0.0-M11 onwards
- 9.0.x for 9.0.41 onwards
- 8.5.x for 8.5.61 onwards
- 7.0.x for 7.0.108 onwards