 (defn- handle-err
   [state err current-state]
   (swap! state #(assoc % :aborting? true))
+  ;; If there is an upstream, try sending an abort event up
   (when (.upstream current-state)
     (try ((.upstream current-state) :abort err)
          (catch Exception _ nil))
     (swap! state #(assoc % :next-up-fn nil :upstream nil)))
+
+  ;; Clear out any timeouts
+  (clear-timeout state current-state)
+
+  ;; Finally, close the connection
   (if (.last-write current-state)
     (.addListener (.last-write current-state)
                   netty/close-channel-future-listener)

    (netty/cancel-timeout old-timeout)))

(defn- bump-timeout
  [state current-state]
  (when-let [old-timeout (.timeout current-state)]
    (netty/cancel-timeout old-timeout))
  (let [new-timeout (netty/on-timeout
                     global-timer
                     (* (:timeout (.options current-state)) 1000)
                     #(handle-err state (Exception. "timeout! facepalm!") @state))]
    (swap! state #(assoc % :timeout new-timeout))))

(defn- exchange-finished?
  [current-state]

    (let [current-state (maybe-write-chunk-trailer current-state)]
      (clear-timeout state current-state)
      (if (.keepalive? current-state)
        (let [new-state (fresh-state current-state)]
          (reset! state new-state)
          (start-keepalive-timer state current-state))
        (do (swap! state #(assoc % :upstream nil))
            (finalize-channel current-state))))))

