jacobobryant / biff

A Clojure web framework for solo developers.
https://biffweb.com
MIT License
829 stars 40 forks source link

Can't start `bb dev` on a fresh install #181

Closed edenworky closed 8 months ago

edenworky commented 8 months ago

On running bb dev in a fresh install, it says localhost:8080 is already in use even though it's not before I run it, and afterwards there's a hanging java process listening on localhost:8080 which I have to kill manually.

This is what it prints to the terminal:

Rebuilding...
Done in 451ms.
[main] INFO com.example - starting: com.biffweb$use_config@1922283e
[main] INFO com.example - starting: com.biffweb$use_secrets@7c2a5a0d
[main] INFO com.example - starting: com.biffweb$use_xt@13e1f4d1
[main] INFO xtdb.tx - Started tx-ingester
[main] INFO com.biffweb.impl.xtdb - Indexed #:xtdb.api{:tx-time #inst "2023-12-17T21:27:57.443-00:00", :tx-id 0}
[main] INFO com.example - starting: com.biffweb$use_queues@4f586b3b
[main] INFO com.example - starting: com.biffweb$use_tx_listener@35188326
[main] INFO com.example - starting: com.biffweb$use_jetty@38e12425
[main] INFO org.eclipse.jetty.server.Server - jetty-10.0.7; built: 2021-10-06T19:34:02.766Z; git: da8a4553af9dd84080931fa0f8c678cd2d60f3d9; jvm 11.0.21+9-post-Ubuntu-0ubuntu122.04
Execution error (BindException) at sun.nio.ch.Net/bind0 (Net.java:-2).
Address already in use

This is the full error log:

{:clojure.main/message
 "Execution error (BindException) at sun.nio.ch.Net/bind0 (Net.java:-2).\nAddress already in use\n",
 :clojure.main/triage
 {:clojure.error/class java.net.BindException,
  :clojure.error/line -2,
  :clojure.error/cause "Address already in use",
  :clojure.error/symbol sun.nio.ch.Net/bind0,
  :clojure.error/source "Net.java",
  :clojure.error/phase :execution},
 :clojure.main/trace
 {:via
  [{:type java.io.IOException,
    :message "Failed to bind to /0.0.0.0:8080",
    :at
    [org.eclipse.jetty.server.ServerConnector
     openAcceptChannel
     "ServerConnector.java"
     345]}
   {:type java.net.BindException,
    :message "Address already in use",
    :at [sun.nio.ch.Net bind0 "Net.java" -2]}],
  :trace
  [[sun.nio.ch.Net bind0 "Net.java" -2]
   [sun.nio.ch.Net bind "Net.java" 459]
   [sun.nio.ch.Net bind "Net.java" 448]
   [sun.nio.ch.ServerSocketChannelImpl
    bind
    "ServerSocketChannelImpl.java"
    227]
   [org.eclipse.jetty.server.ServerConnector
    openAcceptChannel
    "ServerConnector.java"
    340]
   [org.eclipse.jetty.server.ServerConnector
    open
    "ServerConnector.java"
    305]
   [org.eclipse.jetty.server.Server lambda$doStart$0 "Server.java" 401]
   [java.util.stream.ForEachOps$ForEachOp$OfRef
    accept
    "ForEachOps.java"
    183]
   [java.util.stream.ReferencePipeline$3$1
    accept
    "ReferencePipeline.java"
    195]
   [java.util.stream.ReferencePipeline$2$1
    accept
    "ReferencePipeline.java"
    177]
   [java.util.Spliterators$ArraySpliterator
    forEachRemaining
    "Spliterators.java"
    948]
   [java.util.stream.AbstractPipeline
    copyInto
    "AbstractPipeline.java"
    484]
   [java.util.stream.AbstractPipeline
    wrapAndCopyInto
    "AbstractPipeline.java"
    474]
   [java.util.stream.ForEachOps$ForEachOp
    evaluateSequential
    "ForEachOps.java"
    150]
   [java.util.stream.ForEachOps$ForEachOp$OfRef
    evaluateSequential
    "ForEachOps.java"
    173]
   [java.util.stream.AbstractPipeline
    evaluate
    "AbstractPipeline.java"
    234]
   [java.util.stream.ReferencePipeline
    forEach
    "ReferencePipeline.java"
    497]
   [org.eclipse.jetty.server.Server doStart "Server.java" 397]
   [org.eclipse.jetty.util.component.AbstractLifeCycle
    start
    "AbstractLifeCycle.java"
    93]
   [ring.adapter.jetty9$run_jetty invokeStatic "jetty9.clj" 307]
   [ring.adapter.jetty9$run_jetty invoke "jetty9.clj" 247]
   [com.biffweb.impl.misc$use_jetty invokeStatic "misc.clj" 75]
   [com.biffweb.impl.misc$use_jetty invoke "misc.clj" 71]
   [com.biffweb$use_jetty invokeStatic "biffweb.clj" 852]
   [com.biffweb$use_jetty invoke "biffweb.clj" 846]
   [com.example$start$fn__28885 invoke "example.clj" 76]
   [clojure.lang.PersistentVector reduce "PersistentVector.java" 343]
   [clojure.core$reduce invokeStatic "core.clj" 6885]
   [clojure.core$reduce invoke "core.clj" 6868]
   [com.example$start invokeStatic "example.clj" 74]
   [com.example$start invoke "example.clj" 73]
   [com.example$_main invokeStatic "example.clj" 85]
   [com.example$_main doInvoke "example.clj" 84]
   [clojure.lang.RestFn applyTo "RestFn.java" 137]
   [clojure.lang.Var applyTo "Var.java" 705]
   [clojure.core$apply invokeStatic "core.clj" 667]
   [clojure.main$main_opt invokeStatic "main.clj" 514]
   [clojure.main$main_opt invoke "main.clj" 510]
   [clojure.main$main invokeStatic "main.clj" 664]
   [clojure.main$main doInvoke "main.clj" 616]
   [clojure.lang.RestFn applyTo "RestFn.java" 137]
   [clojure.lang.Var applyTo "Var.java" 705]
   [clojure.main main "main.java" 40]],
  :cause "Address already in use"}}
jacobobryant commented 8 months ago

Interesting--I just tried creating a new project on my own machine and it works there, so there must be some environmental difference. If you run sudo netstat -nlp | grep 8080 before running bb dev, does that return anything?

edenworky commented 8 months ago

Before, there's nothing. After I run it and I get the error, I get

tcp6   0      0 :::8080                 :::*                    LISTEN  62893/java

Edit: Wow, I'm never replying on email again

jacobobryant commented 8 months ago

hmmmm quite befuddling. can you try running something else on port 8080 and see if it works? like nc -l -p 8080 I think (not at my computer to verify that's the right command), and then try a curl http://localhost:8080 just to be sure it's working.

jacobobryant commented 8 months ago

if that works, the next step I suppose would be to comment out the use-jetty line in your app's main namespace, then run bb dev, then while it's running try the netstat again.

What operating system are you on by the way?

edenworky commented 8 months ago

I can host lots of stuff on :8080, not sure if I understood your meaning. I can run nc like you said and see an incoming request, works as expected to my understanding.

I use Pop!_OS Linux pop-os 6.5.6-76060506-generic #202310061235~1697396945~22.04~9283e32 SMP PREEMPT_DYNAMIC Sun O x86_64 x86_64 x86_64 GNU/Linux. It's usually equivalent to Ubuntu.

When I comment out use-jetty, bb dev finishes booting up and says it's listening on :8080 (which doesn't show up in netstat) and the REPL is on :7888 (which does show up in netstat).

jacobobryant commented 8 months ago

I can host lots of stuff on :8080, not sure if I understood your meaning.

OK, good--just wanted to run an additional check that there wasn't something else running on 8080.

I still have no clue how Biff could be triggering this error, but we can just keep trying to narrow it down. Let's try from two directions:

First, can you run the following from your Biff project directory without getting errors?

$ clj
Clojure 1.11.1
user=> (require '[ring.adapter.jetty9 :as jetty])
nil
user=> (jetty/run-jetty (constantly {:status 200, :body "hello"}) {:host "0.0.0.0", :port 8080})
[main] INFO org.eclipse.jetty.server.Server - jetty-10.0.7; built: 2021-10-06T19:34:02.766Z; git: da8a4553af9dd84080931fa0f8c678cd2d60f3d9; jvm 17.0.9+9-Ubuntu-120.04
[main] INFO org.eclipse.jetty.server.handler.ContextHandler - Started o.e.j.s.ServletContextHandler@5b4d25e7{/,null,AVAILABLE}
[main] INFO org.eclipse.jetty.server.AbstractConnector - Started ServerConnector@38704ff0{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
[main] INFO org.eclipse.jetty.server.Server - Started Server@267517e4{STARTING}[10.0.7,sto=0] @3472ms

# in another terminal:
$ curl http://localhost:8080
hello

That's ultimately what the use-jetty component runs.

Second, if you uncomment the use-jetty component but comment out the use-xt, use-queues, and use-tx-listener components, and then you run bb dev, does the error still happen? If yes, then what if instead of bb dev you run source secrets.env; BIFF_ENV=dev clj -M -m com.example (replacing com.example with whatever you chose for your project's main namespace--though from the stacktrace earlier it looks like you left it as com.example)?

jacobobryant commented 8 months ago

I'll go ahead and close this, but happy to reopen if you want to continue debugging this together.