         port-files-ref
         (atom nil)
 
+        shutdown-hook
+        (Thread. stop!)
+
         app
         (-> {::started (System/currentTimeMillis)
              :server-pid pid
              :config config
+             :shutdown-hook shutdown-hook
              :ssl-context ssl-context
              :http {:port (or https-port http-port)
                     :http-port http-port

        (or srv (recur (inc port)))
        ))))

(defn start-system
  [app-ref app-config {:keys [cache-root] :as config}]
  (when @app-ref

       (log/warn t# "shutdown failed" ~(pr-str body))
       (discard-println ~(str "shutdown failed: " (pr-str body))))))

(defn shutdown-system [{:keys [shutdown-hook http port-files-ref socket-repl cli-repl nrepl] :as app}]
  (discard-println "shutting down ...")
  (. (Runtime/getRuntime) (removeShutdownHook shutdown-hook))

  (do-shutdown
    (doseq [port-file (vals @port-files-ref)]
      (.delete port-file)))

  (when socket-repl
    (do-shutdown (socket-repl/stop socket-repl)))

