net.async icon indicating copy to clipboard operation
net.async copied to clipboard

Passing an unresolved hostname to `connect` causes thread to crash

Open dpetran opened this issue 4 years ago • 2 comments

When trying to connect to a host that isn't resolvable via DNS causes the event-loop thread to crash.

(def host1 (ntcp/event-loop))
(def client (connect host1 {:host "unresolved.local" :port 8885}))

In the logs you'll find:

SEVERE: thread died
java.nio.channels.UnresolvedAddressException
	at java.base/sun.nio.ch.Net.checkAddress(Net.java:130)
	at java.base/sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:673)
	at net.async.tcp$detect_connecting.invokeStatic(tcp.clj:174)
	at net.async.tcp$detect_connecting.invoke(tcp.clj:165)
	at net.async.tcp$event_loop_impl.invokeStatic(tcp.clj:199)
	at net.async.tcp$event_loop_impl.invoke(tcp.clj:195)
	at clojure.lang.AFn.applyToHelper(AFn.java:154)
	at clojure.lang.AFn.applyTo(AFn.java:144)
	at clojure.core$apply.invokeStatic(core.clj:667)
	at clojure.core$apply.invoke(core.clj:662)
	at net.async.tcp$in_thread$fn__13942.invoke(tcp.clj:30)
	at clojure.lang.AFn.run(AFn.java:22)
	at java.base/java.lang.Thread.run(Thread.java:829)

The event loop still exists and the thread is still runnable, but the connection isn't retried and the only sign that something is wrong is the SEVERE line in the logs.

We discovered it when bringing up a service in an environment with dynamic DNS, where the server it was connecting to was not yet resolvable.

dpetran avatar Dec 14 '21 17:12 dpetran

We solved the problem by adding a perpetual loop where we wait for the host name to be resolvable, and I'm happy to contribute that if you'd like:

(defn ^InetSocketAddress inet-addr [{:keys [^String host ^Integer port] :as spec}]
  (let [addr (if host
               (InetSocketAddress. host port)
               (InetSocketAddress. port))]
    (if (.isUnresolved addr)
      (do (logging/warn "addr is unresolved" host port)
          (Thread/sleep 5000)
          (recur spec))
      addr)))

Though if you'd prefer to handle it a different way, that's fine too.

dpetran avatar Dec 14 '21 17:12 dpetran

This is a very old library and I am surprised to find out you are actually using it :) I can transfer the project to you, if you want: it’s hard for me to make the calls because I don’t remember what this was about and I don’t really use it myself. I’d be happy to see it improving based on real-world usage. WDYT?

tonsky avatar Dec 15 '21 12:12 tonsky