anchor.diff:  (in-package :cl-rabbit-async)
  +
  +(declaim (optimize (safety 3) (debug 3) (speed 0)))
   
   (defclass async-channel ()
     ((connection        :type async-connection
  
positive.code:                 ;; ELSE: Message for unused channel
                  (warn-nonexistent-channel))))))))
  
  (defun handle-command (async-conn in-fd)
    (sb-alien:with-alien ((buf (sb-alien:array sb-alien:int 1)))
      (let* ((size (/ (sb-alien:alien-size sb-alien:int) 8))
             (result (sb-posix:read in-fd (sb-alien:addr buf) size)))
        (unless (= result size)
          (error "Unable to read from pipe"))
        (let ((index (sb-alien:deref buf 0)))
          (let ((cmd (bordeaux-threads:with-lock-held ((async-connection/lock async-conn))
                       (with-accessors ((requests async-connection/requests)) async-conn
                         (let ((cmd (assoc index requests)))
                           (unless cmd
                             (error "Attempt to get missing command index: ~s" index))
                           (setf requests (delete (car cmd) requests :key #'car))
                           (cdr cmd))))))
            (let ((result (funcall (first cmd))))
              (funcall (second cmd) result)))))))
  
  (defun run-sync-loop (async-conn in-fd)
    (let ((socket-fd (cl-rabbit::get-sockfd (async-connection/connection async-conn))))
      (sb-alien:with-alien ((rfds (sb-alien:struct sb-unix:fd-set)))
        (sb-unix:fd-zero rfds)
        (sb-unix:fd-set socket-fd rfds)
        (sb-unix:fd-set in-fd rfds)
        (multiple-value-bind (count err)
            (sb-unix:unix-fast-select (1+ (max socket-fd in-fd)) (sb-alien:addr rfds) nil nil 10 0)
          (if count
              (progn
                (when (sb-unix:fd-isset in-fd rfds)
                  (handle-command async-conn in-fd))
                (when (sb-unix:fd-isset socket-fd rfds)
                  (wait-for-frame async-conn)))
              ;; ELSE: An error occurred in the call
              (unless (= err sb-unix:eintr)
                (error "Error: ~s" err)))))
      (log:info "Sync loop for fd ~a finished" socket-fd)))
  
  (defun run-in-sync-thread (async-conn fn)
    (let* ((h (make-transfer-handler))
  
negative.code:       (trivial-garbage:finalize conn-wrapper
                                  (lambda ()
                                    (log:warn "Reference to RabbitMQ connection object lost. Closing.")
                                    #+nil(cl-rabbit:destroy-connection conn)))
        (multiple-value-bind (in-fd out-fd)
            (sb-posix:pipe)
          (setf (async-connection/cmd-fd conn-wrapper) out-fd)
          (setf (async-connection/cmd-fd-reader conn-wrapper) in-fd)
          (bordeaux-threads:make-thread (lambda () (run-sync-loop conn-wrapper))
                                        :name "RabbitMQ listener")
          conn-wrapper))))
  
  (defun close-async-connection (async-conn)
    (when (async-connection/close-p async-conn)
      (error "Connection is already closed"))
    (setf (async-connection/close-p async-conn) t)
    (sb-posix:close (async-connection/cmd-fd async-conn))
    (sb-posix:close (async-connection/cmd-fd-reader async-conn))
    (with-sync async-conn
      (cl-rabbit:destroy-connection (async-connection/connection async-conn))))
  
  #+nil(defun connect-test ()
    (cl-rabbit:with-connection (conn)
  