                      (lean-server-async-task-queue-len))
   (while lean-global-async-task-queue
     (accept-process-output (lean-server-get-process) 0 50 t)
-    (let* ((cont   (lean-server-async-task-queue-peek-front))
-           (result (lean-server-check-and-process-buffer-with-cont cont)))
+    (let* ((front-item (lean-server-async-task-queue-peek-front))
+           (cont       (car front-item))
+           (cmd-type   (cdr front-item))
+           (result (lean-server-check-and-process-buffer-with-cont cont
+                                                                   cmd-type)))
       (pcase result
         (`(PROCESSED ,ret)
          (lean-server-async-task-queue-pop-front)
-         (lean-server-debug "lean-server-consume-all-sync-tasks: processed. queue size = %d"
+                  (lean-server-debug "lean-server-consume-all-sync-tasks: processed. queue size = %d"
+                            (lean-server-async-task-queue-len)))
+        (`(NOTREADY)
+         (lean-server-debug "lean-server-consume-all-sync-tasks: not ready. queue size = %d"
+                            (lean-server-async-task-queue-len)))
+        (t
+         (lean-server-async-task-queue-pop-front)
+         (lean-server-debug "lean-server-consume-all-sync-tasks: either error or quit happend. queue size = %d"
                             (lean-server-async-task-queue-len))))))
   (lean-server-debug "lean-server-consume-all-async-tasks: over. queue size = %d"
                      (lean-server-async-task-queue-len)))

Otherwise, set an idle-timer to call the handler again"
  (lean-server-cancel-retry-timer)
  (when (eq major-mode 'lean-mode)
    (let* ((cont   (lean-server-async-task-queue-peek-front))
           (result (lean-server-check-and-process-buffer-with-cont cont)))
      (pcase result
        (`(PROCESSED ,ret)
         (lean-server-async-task-queue-pop-front)

    (process-send-string proc string-to-send)
    (lean-server-after-send-cmd cmd)))

(defun lean-server-process-message-with-cont (body type cont cmd-type)
  "Process the message from lean-server and call continuation"
  (lean-server-debug "process-message-with-cont: type = %S, cmd-type = %S"
                     type cmd-type)
  (lean-server-debug "process-message-with-cont: body\n-----\n%s\n-----\n" body)
  (cl-case type
    (INFO
     (lean-server-debug "Process: INFO")
