java 2022-04-01

I'm attempting to port to clojure this sshj example to do local port forwarding via ssh: https://github.com/hierynomus/sshj/blob/master/examples/src/main/java/net/schmizz/sshj/examples/LocalPF.java

(defn local-port-forwarder [ssh & {:keys [local-host local-port
                                          remote-host remote-port]
                                   :or   {local-host  "0.0.0.0"
                                          remote-host "0.0.0.0"}}]
  (let [params (Parameters. local-host local-port remote-host remote-port)
        ss     (doto (ServerSocket.) #_(.setReuseAddress true))
        lpf    (.newLocalPortForwarder ssh params ss)]
    (.bind ss (InetSocketAddress. local-host local-port))
    (future
      (try
        (.listen lpf)
        (println "done listening")
        (catch Exception e
          (timbre/error e)
          (throw e))
        (finally
          (println "closing")
          (.close ss)))
      (println "Future done"))
    {:lpf lpf
     :ss ss}))
It works, except that connections that were established seem to keep working even after I close the local port forwarder (and the verify underlying ServerSocket is closed). Is this just be me not understanding something about java networking? Specifically what's happening is I'm running a datalevin server on a remote server https://github.com/juji-io/datalevin/blob/master/doc/server.md. Then I 1. connect to the server via ssh 2. attempt to connect to - this fails because I don't have port forwarding set up yet 3. start a port forwarder with the above function. Now I can successfully establish a connection above url and interact with the database using something like (def client (new-client "")) 4. Now I call .close on the LocalPortFowarder object. I then verify that the ServerSocket is closed. All the print statements in the function above print. I can no longer connect if I attempt to call (def client (new-client "")) again. 5. However, the previously established connection from 3. still works. I can still query the remote database, that connection is still clearly working. Additionally I can still see the connection open for that port in netstat. This is on windows 10. Is this me not understanding how socket connections are cleaned up? Or an issue in sshj/windows/datalevin? Any hints would be appreciated!