Q:

Firefox add-on: hanging TCP connections

I'm creating a Firefox add-on which contains server listening for TCP connections on one port. The problem is that it does not always close the connection: after sending a FIN-ACK, and receiving an ACK, the TCP session is left open if the client does not send in return a FIN-ACK.
Only some connections are not closed completely. But after a while, they are too many TC connections hanging, and Firefox cannot open an new file handle, or receive any new connection.
TCP localhost.localdomain:commtact-https->localhost.localdomain:46951 (CLOSE_WAIT)

I could not find a way, preferably in the add-on (but I also tried on the client side) to make sure all TCP connections are closed correctly. Here is what the server functionality looks like in the add-on:
init_server: function(port) {
server.result = {};
server.listener = {
    onSocketAccepted : function(serverSocket, transport) {
        server.result.sout = transport.openOutputStream(0,0,0);
        var instream = transport.openInputStream(0,0,0);
        server.result.sin = Components.classes["@mozilla.org/binaryinputstream;1"].
            createInstance(Components.interfaces.nsIBinaryInputStream);
        server.result.sin.setInputStream(instream);

        server.debug("New incoming connection");

        var dataListener = {
            onStartRequest: function(request, context) {
                server.debug("onStartRequest");
            },
            onStopRequest: function(request, context, status) {
                server.debug("onStopRequest");
//              instream.close();
//          delete instream;
//              server.result.sout.close();
//          server.result.sin.close();
            },
            onDataAvailable: function(request, context, inputStream, offset, count) {
              var sis = Components.classes ["@mozilla.org/scriptableinputstream;1"].createInstance (Components.interfaces.nsIScriptableInputStream);
              sis.init(inputStream);
              var str = sis.read(count);
              var suc = Components.classes ["@mozilla.org/intlscriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
              suc.charset = "utf-8";
              var line = suc. ConvertToUnicode (str);
              server.debug(line);

              [ ... process data ... ]

//        instream.close();
//        server.result.sout.close();
//        server.result.sin.close();
//        delete instream;
            },
        };

        // Listen to new data
        var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance(Components.interfaces.nsIInputStreamPump);
        pump.init(instream, -1, -1, 0, 0, false); //screenserver.result.sin
        pump.asyncRead(dataListener, null);
    },
    onStopListening : function(serverSocket, status){
        server.debug("Client dead?");
        server.init_server(server.PORT);
    },
};

  server.socket = null;
  server.socket = Components.classes["@mozilla.org/network/server-socket;1"].getService(Components.interfaces.nsIServerSocket); 
  server.socket.init(port, true, -1); 
  server.socket.asyncListen(server.listener);
}

A:

I think you need to save the transport you're passed in call onSocketAccepted and then invoke close() on that.

