     :recur))
 
 (defn ago-return-chan [state value]
-  (let [^not-native c (aget state ioc-helpers/USER-START-IDX)]
-    (when-not (nil? value)
-      (protocols/put! c value (ioc-helpers/fn-handler (fn [] nil))))
-    (protocols/close! c)
-    (ago-dereg-state-machine (chan-to-ago-world c) (.-buf c))
+  (let [^not-native c (aget state ioc-helpers/USER-START-IDX)
+        ago-world (chan-ago-world c)
+        active? #(<= (compare-segvs (chan-segv c) (:segv @ago-world)) 0)]
+    (when (active?)
+      (when-not (nil? value)
+        (protocols/put! c value (fn-handler active? (fn [] nil))))
+      (protocols/close! c))
+    (ago-dereg-state-machine ago-world (.-buf c))
     c))
 
 ; --------------------------------------------------------


; --------------------------------------------------------

(defn fn-handler [f]
  (reify
    protocols/Handler
    (active? [_] true)
    (commit [_] f)))

(defn ago-take [state blk ^not-native c]
  (if-let [cb (protocols/take!
               c (fn-handler
                  (fn [x]
                    (ioc-macros/aset-all! state ioc-helpers/VALUE-IDX
                                          x ioc-helpers/STATE-IDX blk)
                    (ioc-helpers/run-state-machine-wrapped state))))]
    (do (ioc-macros/aset-all! state ioc-helpers/VALUE-IDX
                              @cb ioc-helpers/STATE-IDX blk)
        :recur)
    nil))

(defn ago-put [state blk ^not-native c val]
  (if-let [cb (protocols/put!
               c val (fn-handler
                      (fn [ret-val]
                        (ioc-macros/aset-all! state ioc-helpers/VALUE-IDX
                                              ret-val ioc-helpers/STATE-IDX blk)
                        (ioc-helpers/run-state-machine-wrapped state))))]
    (do (ioc-macros/aset-all! state ioc-helpers/VALUE-IDX
                              @cb ioc-helpers/STATE-IDX blk)
        :recur)
    nil))

(defn ago-alts [state cont-block ports & {:as opts}]
  (ioc-macros/aset-all! state ioc-helpers/STATE-IDX cont-block)
  (when-let [cb (cljs.core.async/do-alts
                 (fn [val]
                   (ioc-macros/aset-all! state ioc-helpers/VALUE-IDX val)
                   (ioc-helpers/run-state-machine-wrapped state))
                 ports

                                    buf-or-n)
                       buf-or-n)))))

(defn chan-segv [ch]
  (.-segv (.-takes ch)))

(defn chan-ago-world [ch]
  (.-ago-world (.-takes ch)))

; --------------------------------------------------------
