   to use a one-shot context. Sets the file line accordingly."
   [id nspace file line thunk]
   (let [the-repl (if (not= id -1)
-                   (dosync
-                     (let [the-repls (deref *repls*)
-                           the-repl  (the-repls id)]
-                       (when-not the-repl
-                         (throw (Exception. (str "No Repl of that id: " id))))
-                       (alter *repls* dissoc id)
-                       the-repl))
+                   (locking *repls*
+                     (if-let [the-repl (get @*repls* id)]
+                       (do
+                         (swap! *repls* dissoc id)
+                         the-repl)
+                       (throw (Exception. (str "No Repl of that id: " id)))))
                    (make-repl -1))
         line     (if (= line 0)
                    (the-repl :line)

  #^{:doc
  "A map holding the references to all running repls indexed by their repl id."}
  *repls*
  (ref {}))

(let [id (ref 0)]
  (defn repl-id
    "Get a new Repl id."
    []
    (dosync (alter id inc))))

(def
  #^{:doc

  ; Make sure user namespace exists.
  (let [id       (repl-id)
        the-repl (make-repl id nspace)]
    (swap! *repls* assoc id the-repl)
    id))

(defn stop
  "Stop the Repl with the given id."
  [id]
  (when-not (@*repls* id)
    (throw (Exception. "Not Repl of that id or Repl currently active: " id)))
  (swap! *repls* dissoc id))

(defn root-cause
  "Drill down to the real root cause of the given Exception."
