websocketclojurealephreitit

Address already in use when sending message to websocket


I’m using the manifold.stream library to send a message through a websocket:

(defn send []
    (manifold.stream/put! mysocket
         (generate-string
          {:type "unsubscribe"
           :product_ids ["ETH-USD"]
           :channels ["heartbeat"]})))

and running (send) in the repl works. However, when I make an http request from a client, and have the send function as the handler for my reitit route, I get the error:

Syntax error macroexpanding at (home.clj:142:3).
    at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:3711)
    at clojure.lang.Compiler$DefExpr.eval(Compiler.java:457)
    at clojure.lang.Compiler.eval(Compiler.java:7186)
    at clojure.lang.Compiler.load(Compiler.java:7640)
    at clojure.lang.RT.loadResourceScript(RT.java:381)
    at clojure.lang.RT.loadResourceScript(RT.java:372)
    at clojure.lang.RT.load(RT.java:459)
    at clojure.lang.RT.load(RT.java:424)
    at clojure.core$load$fn__6856.invoke(core.clj:6115)
    at clojure.core$load.invokeStatic(core.clj:6114)
    at clojure.core$load.doInvoke(core.clj:6098)
    at clojure.lang.RestFn.invoke(RestFn.java:408)
    at clojure.core$load_one.invokeStatic(core.clj:5897)
    at clojure.core$load_one.invoke(core.clj:5892)
    at clojure.core$load_lib$fn__6796.invoke(core.clj:5937)
    at clojure.core$load_lib.invokeStatic(core.clj:5936)
    at clojure.core$load_lib.doInvoke(core.clj:5917)
    at clojure.lang.RestFn.applyTo(RestFn.java:142)
    at clojure.core$apply.invokeStatic(core.clj:669)
    at clojure.core$load_libs.invokeStatic(core.clj:5974)
    at clojure.core$load_libs.doInvoke(core.clj:5958)
    at clojure.lang.RestFn.applyTo(RestFn.java:137)
    at clojure.core$apply.invokeStatic(core.clj:669)
    at clojure.core$require.invokeStatic(core.clj:5996)
    at clojure.core$require.doInvoke(core.clj:5996)
    at clojure.lang.RestFn.invoke(RestFn.java:421)
    at ring.middleware.reload$reloader$fn__4434.invoke(reload.clj:17)
    at ring.middleware.reload$wrap_reload$fn__4446.invoke(reload.clj:38)
    at selmer.middleware$wrap_error_page$fn__4461.invoke(middleware.clj:18)
    at prone.middleware$wrap_exceptions$fn__4702.invoke(middleware.clj:159)
    at ring.middleware.flash$wrap_flash$fn__8513.invoke(flash.clj:39)
    at ring.middleware.session$wrap_session$fn__8872.invoke(session.clj:108)
    at ring.middleware.keyword_params$wrap_keyword_params$fn__8918.invoke(keyword_params.clj:53)
    at ring.middleware.nested_params$wrap_nested_params$fn__8976.invoke(nested_params.clj:89)
    at ring.middleware.multipart_params$wrap_multipart_params$fn__9108.invoke(multipart_params.clj:171)
    at ring.middleware.params$wrap_params$fn__9132.invoke(params.clj:67)
    at ring.middleware.cookies$wrap_cookies$fn__8823.invoke(cookies.clj:214)
    at ring.middleware.absolute_redirects$wrap_absolute_redirects$fn__9320.invoke(absolute_redirects.clj:47)
    at ring.middleware.resource$wrap_resource_prefer_resources$fn__9168.invoke(resource.clj:25)
    at ring.middleware.content_type$wrap_content_type$fn__9268.invoke(content_type.clj:34)
    at ring.middleware.default_charset$wrap_default_charset$fn__9292.invoke(default_charset.clj:31)
    at ring.middleware.not_modified$wrap_not_modified$fn__9234.invoke(not_modified.clj:61)
    at ring.middleware.x_headers$wrap_x_header$fn__8476.invoke(x_headers.clj:22)
    at ring.middleware.x_headers$wrap_x_header$fn__8476.invoke(x_headers.clj:22)
    at ring.middleware.x_headers$wrap_x_header$fn__8476.invoke(x_headers.clj:22)
    at hedgehog_interview.middleware$wrap_internal_error$fn__9394.invoke(middleware.clj:17)
    at aleph.http.server$handle_request$fn__15012$f__9755__auto____15013.invoke(server.clj:158)
    at clojure.lang.AFn.run(AFn.java:22)
    at io.aleph.dirigiste.Executor$Worker$1.run(Executor.java:62)
    at manifold.executor$thread_factory$reify__9637$f__9638.invoke(executor.clj:44)
    at clojure.lang.AFn.run(AFn.java:22)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.BindException: Address already in use
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:433)
    at sun.nio.ch.Net.bind(Net.java:425)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
    at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:128)
    at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:558)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1358)
    at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:501)
    at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:486)
    at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:1019)
    at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:254)
    at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:366)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:465)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    ... 1 more

The line 142 refers to,:

(def server
  (aleph.http/start-server server-handler {:port 8992}))

Why am I getting Address already in use?


Solution

  • Your source file defines a top-level variable whose value is a server. To define that variable, it must start a server bound to the specified port. Your wrap-reload middleware reloads the app on each request, which means it must re-define the variable, which means it attempts to bind a new server to that same port.

    You need to either define your server in a way that is less fragile (e.g. use defonce to avoid redefining on reload), or else more controlled (e.g. start it explicitly in -main instead of implicitly when the namespace is loaded).