From 8f774120c9f256599affd945f177b611d9a114d9 Mon Sep 17 00:00:00 2001 From: Nathaniel Nicandro Date: Thu, 11 Jan 2018 12:06:26 -0600 Subject: [PATCH] Better handling of when to remove requests from a client's request table For each message received in response to a request, record the `last-message-time` of any received messages for a request. When a request's `idle-received-p` field is non-nil and the `last-message-time` is larger than some number of seconds, remove the request from the request table. --- jupyter-base.el | 1 + jupyter-client.el | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/jupyter-base.el b/jupyter-base.el index 95cca62..82887dc 100644 --- a/jupyter-base.el +++ b/jupyter-base.el @@ -132,6 +132,7 @@ from the kernel.") (-id) (time (current-time)) (idle-received-p nil) + (last-message-time nil) (run-handlers-p t) (callbacks)) diff --git a/jupyter-client.el b/jupyter-client.el index 9555a0b..2e77ebc 100644 --- a/jupyter-client.el +++ b/jupyter-client.el @@ -612,6 +612,18 @@ that if no TIMEOUT is given, it defaults to (declare (indent 1)) (jupyter-wait-until req msg-type #'identity timeout)) +(defun jupyter--drop-idle-requests (client) + (cl-loop + with requests = (oref client requests) + with ctime = (current-time) + for req in (hash-table-values requests) + for ltime = (jupyter-request-last-message-time req) + for id = (jupyter-request-id req) + when (and (jupyter-request-idle-received-p req) + (> (float-time (time-subtract ctime ltime)) 60)) + ;; Don't remove the `jupyter-missing-request' + unless (equal id "") do (remhash id requests))) + (cl-defmethod jupyter-handle-message ((client jupyter-kernel-client) channel) "Process a message on CLIENT's CHANNEL. When a message is received from the kernel, the @@ -645,6 +657,7 @@ are taken: ;; TODO: Would we always want this? (when (eq (oref channel type) :iopub) (jupyter-handle-message channel client nil msg)) + (setf (jupyter-request-last-message-time req) (current-time)) (unwind-protect (jupyter--run-callbacks req msg) (unwind-protect @@ -653,10 +666,8 @@ are taken: ;; Checking for pmsg-id prevents the removal of ;; `jupyter-missing-request' for the client (when (and pmsg-id (jupyter-message-status-idle-p msg)) - (setf (jupyter-request-idle-received-p req) t) - ;; TODO: Messages associated with the request might still be - ;; received when the request is removed from the requests table. - (remhash pmsg-id requests)))))))) + (setf (jupyter-request-idle-received-p req) t)) + (jupyter--drop-idle-requests client))))))) ;;; STDIN message requests/handlers