Evaluation in browser REPL occasionally hangs. It seems that repl environment and browser sometimes miss each other and their "randezvous" fails. Browser is waiting for POST reply and repl is trying to send a command, but they do not meet each other.
I found the issue when we switched our tests from nodejs to browser environment. Luckily I was able to find very small example which hangs during execution. It seems that (simulated) heavy load increases the chance of "hanging".
Minimal setup:
(ns race.condition
(:require [cljs.repl.browser :as browser]
[cljs.repl :as repl]
[cljs.env :as env]
[cljs.build.api :as api]))
(api/build '[(ns race.repl
(:require [clojure.browser.repl]))
(clojure.browser.repl/connect "http://localhost:9000/repl")]
{:output-to "target/cljs-race/main.js"
:output-dir "target/cljs-race"
:main 'race.repl})
(spit "target/cljs-race/index.html"
(str "<html>" "<body>"
"<script type=\"text/javascript\" src=\"main.js\">"
"</script>" "</body>" "</html>"))
Now start the environment:
(def env (browser/repl-env :static-dir ["target/cljs-race" "."] :port 9000 :src nil))
(env/with-compiler-env (env/default-compiler-env)
(repl/-setup env {}))
cross your fingers and start this endless loop:
(loop [i 0]
(println (java.util.Date.) i)
(dotimes [j 100]
(let [result (repl/-evaluate env "<exec>" "1" "true")]
(when-not (= :success (:status result))
(println i j result))))
(recur (inc i)))
To simulate heavy load run heavy-load.sh from attachment.
After some iterations (eg 55 big loop i) execution stops. If you investigate stacks (see race-condition.jstack), you can see in one thread:
at clojure.core$promise$reify__6779.deref(core.clj:6816)
at clojure.core$deref.invoke(core.clj:2206)
at cljs.repl.browser$send_for_eval.invoke(browser.clj:65)
at cljs.repl.browser$browser_eval.invoke(browser.clj:193)
at cljs.repl.browser.BrowserEnv._evaluate(browser.clj:262)
The code is waiting for a promise with a connection (which already did arive).
My guess is suspicious code in cljs.repl.server functions connection and set-connection. Both functions access an atom in non-standard way. They deref a valua and make a swap! in two steps.
Can somebody with better understanding of REPL internals investigate? Thank you.