   (let [cmd-ch (achan actx)
         state-ch (achan actx)]
     (act-loop kvs-mgr-in actx [tot-ops 0]
-              (when-let [m (atake kvs-mgr-in cmd-ch)]
+              (if-let [m (atake kvs-mgr-in cmd-ch)]
                 (if-let [op-handler (get op-handlers (:op m))]
                   (do (op-handler kvs-mgr-in state-ch m)
                       (recur (inc tot-ops)))
-                  (println :kvs-mgr-in-unknown-op-exit m))))
+                  (println :kvs-mgr-in-unknown-op-exit m))
+                (println :kvs-mgr-in-ch-exit)))
 
     ; Using kvs-mgr-state to avoid atoms, which prevent time-travel.
     (act-loop kvs-mgr-state actx [state { :next-uuid 0 :kvss {} }]
               (if-let [[state-fn res-ch] (atake kvs-mgr-state state-ch)]
                 (if-let [[next-state res] (state-fn state)]
-                  (do (aput-close kvs-mgr-state res-ch res)
+                  (do (when (and res-ch res)
+                        (aput-close kvs-mgr-state res-ch res))
                       (recur next-state))
                   (println :kvs-mgr-state-fn-exit))
                 (println :kvs-mgr-state-ch-exit)))

(defn kvs-entry-by-key [kvs key]
  (kvs-entry-by-key-sq kvs key (kvs-sq-by-key kvs key)))

; TODO: Track age, utilization, compaction for simulated performance.

; TODO: Cannot use atoms because they prevent time travel.

; TODO: Add snapshot ability.

(defn kvs-check [m work-fn]
  (fn [state] ; Meant to be used as work-fn for state-work function.
    (if-let [[name uuid] (:kvs-ident m)]
      (if-let [kvs (get-in state [:kvss name])]
        (if (= uuid (:uuid kvs))
          (work-fn state kvs)

(ns cbfg.kvs
  (:require-macros [cbfg.act :refer [act act-loop achan aput-close aput atake]])
  (:require [cbfg.misc :refer [dissoc-in]]))

; A kvs is an abstraction of ordered-KV store.  Concrete examples
