Bug 156163

Summary: Socket file descriptor leak in ktorrent
Product: [Applications] ktorrent Reporter: Rickard Närström <rickard.narstrom>
Component: generalAssignee: Joris Guisson <joris.guisson>
Status: RESOLVED FIXED    
Severity: normal CC: getaceres
Priority: NOR    
Version First Reported In: unspecified   
Target Milestone: ---   
Platform: Gentoo Packages   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Rickard Närström 2008-01-19 09:20:22 UTC
Version:           3.0beta1 (using KDE 4.0.0)
Installed from:    Gentoo Packages
Compiler:          gcc (GCC) 4.2.2 (Gentoo 4.2.2 p1.0) 
OS:                Linux

After a few hours of running ktorrent starts stopping torrents with "Too many open files". I have 13 seedings and 1 download active (about half of them with more then one file).

When torrents are beign stopped a lot of sockets in CLOSE_WAIT state are still opened as this command shows:

$ lsof -n -c ktorrent | grep "CLOSE_WAIT" | wc -l
624

In normal cases you should never keep a file descriptor open to a scoket in CLOSE_WAIT.
Comment 1 Joris Guisson 2008-01-21 21:29:05 UTC
*** Bug 156331 has been marked as a duplicate of this bug. ***
Comment 2 Joris Guisson 2008-01-22 19:25:23 UTC
SVN commit 764849 by guisson:

Fix problem with connections staying in close_wait state

BUG: 156163


 M  +4 -0      socket.cpp  


WebSVN link: http://websvn.kde.org/?view=rev&revision=764849
Comment 3 Rickard Närström 2008-01-27 19:57:56 UTC
This is still a problem in ktorrent 3.0rc1
Comment 4 Joris Guisson 2008-01-27 20:14:27 UTC
I'm not seeing it happening, and I'm absolutely sure then when you stop a torrent, all it's sockets are closed.
Comment 5 Rickard Närström 2008-01-27 20:34:26 UTC
No they are not... I have just confirmed that after stopping all torrents ktorrent still have 369 fd's open to sockets in CLOSE_WAIT and 55 to sockets in ESTABLISHED.

Comment 6 Rickard Närström 2008-01-27 20:58:01 UTC
This only hapens with local connections, remote connections are close properly. So you will not see this if you are behind a firewall.

This is properly unrelated but ktorrent dosn't seem to use SO_REUSEADDR for listening connection, if I close ktorrent and start it again it fails to bind to the listening port and a "address already in use" error is written to the log.
Comment 7 Rickard Närström 2008-01-27 21:02:01 UTC
Sorry, my mistake... it only hapens with remote connections, not with local connections. -- All sockets in CLOSE_WAIT are bound to ktorrents listening port.
Comment 8 Rickard Närström 2008-01-27 21:57:46 UTC
I found it!!

in bt::Server::newConnection(int) (server.cpp line 116) you call the mse::StreamSocket(int) constructor with the fd as only argument, however mse::StreamSocket(int) expect ip-version as only argument not a fd. The fd is after that lost and left open, authentication with that host fails as the socket is not connected.

I have for now work around this by applaing this patch, it works for me as I don't use IPv6. You probebly should check the ip-version of the listening socket and use that instead:
----
diff -Naur ktorrent-3.0rc1/libbtcore/torrent/server.cpp ktorrent-3.0rc1.patched/libbtcore/torrent/server.cpp
--- ktorrent-3.0rc1/libbtcore/torrent/server.cpp        2008-01-27 12:56:51.000000000 +0100
+++ ktorrent-3.0rc1.patched/libbtcore/torrent/server.cpp        2008-01-27 21:32:30.000000000 +0100
@@ -113,7 +113,7 @@

        void Server::newConnection(int socket)
        {
-               mse::StreamSocket* s = new mse::StreamSocket(socket);
+               mse::StreamSocket* s = new mse::StreamSocket(socket, 4);
                if (peer_managers.count() == 0)
                {
                        s->close();
Comment 9 Joris Guisson 2008-01-28 19:20:06 UTC
SVN commit 767757 by guisson:

Fix socket descriptor leak, big thanks to Richard Narstrom for finding the problem and the solution

BUG: 156163



 M  +1 -0      ktorrent/main.cpp  
 M  +1 -1      libbtcore/torrent/server.cpp  


WebSVN link: http://websvn.kde.org/?view=rev&revision=767757