Skip to content

Commit 7512b83

Browse files
committed
Merge pull request #811 from vitoshka/startup
[Fix #810] don't allow requests before client initialization has completed
2 parents c975b48 + 8747a7f commit 7512b83

File tree

1 file changed

+41
-49
lines changed

1 file changed

+41
-49
lines changed

nrepl-client.el

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,7 @@ within Emacs. Return the newly created client connection process."
599599
(directory (or directory default-directory))
600600
(host (or host (plist-get endpoint :hostname)))
601601
(port (or port (plist-get endpoint :port)))
602-
(server-buf (and server-proc
603-
(buffer-name (process-buffer server-proc))))
602+
(server-buf (and server-proc (process-buffer server-proc)))
604603
(client-buf (if replp
605604
(cider-repl-create directory host port)
606605
(nrepl-create-connection-buffer directory host port)))
@@ -617,8 +616,7 @@ within Emacs. Return the newly created client connection process."
617616

618617
(with-current-buffer client-buf
619618
(when server-buf
620-
(setq nrepl-project-dir (buffer-local-value 'nrepl-project-dir
621-
(get-buffer server-buf))
619+
(setq nrepl-project-dir (buffer-local-value 'nrepl-project-dir server-buf)
622620
nrepl-server-buffer server-buf))
623621
(setq nrepl-endpoint `(,host ,port)
624622
;; fixme: repl and connection buffers are the same thing
@@ -629,50 +627,40 @@ within Emacs. Return the newly created client connection process."
629627
nrepl-completed-requests (make-hash-table :test 'equal)))
630628

631629
(nrepl-make-connection-default client-buf)
632-
633-
;; Everything is set. We are ready to send requests.
634-
(nrepl-request:clone (nrepl--new-tooling-session-handler client-proc))
635-
(nrepl-request:clone (nrepl--new-session-handler client-proc))
636-
(when replp
637-
(nrepl-request:describe
638-
(nrepl--connection-buffer-init-handler client-buf t)))
639-
client-proc))
640-
641-
(defun nrepl--connection-buffer-init-handler (conn-buffer replp)
642-
"Return a handler to setup CONN-BUFFER as a connection buffer.
643-
If REPLP is non-nil, also initialize it as a REPL buffer."
644-
(lambda (response)
645-
(nrepl-dbind-response response (id ops versions)
646-
(with-current-buffer conn-buffer
647-
(remhash id nrepl-pending-requests)
648-
(setq nrepl-ops ops)
649-
(setq nrepl-versions versions)))
650-
(when replp
651-
(cider-repl-init conn-buffer))
630+
(nrepl--init-client-sessions client-proc)
631+
(nrepl--init-connection-buffer client-buf replp)
652632
(cider--check-required-nrepl-ops)
653-
(cider--check-middleware-compatibility)))
633+
(cider--check-middleware-compatibility)
634+
(run-hooks 'nrepl-connected-hook)
654635

655-
(defun nrepl--new-session-handler (process)
656-
"Create a new session handler for PROCESS."
657-
(lambda (response)
658-
(nrepl-dbind-response response (id new-session err)
636+
client-proc))
637+
638+
(defun nrepl--init-client-sessions (client)
639+
"Initialize CLIENT sessions."
640+
(let ((response-main (nrepl-sync-request:clone))
641+
(response-tooling (nrepl-sync-request:clone)))
642+
(nrepl-dbind-response response-main (new-session err)
659643
(if new-session
660-
(with-current-buffer (process-buffer process)
661-
(remhash id nrepl-pending-requests)
644+
(with-current-buffer (process-buffer client)
662645
(setq nrepl-session new-session))
663-
(error "Could not create new session (%s)" err))
664-
(run-hooks 'nrepl-connected-hook))))
665-
666-
(defun nrepl--new-tooling-session-handler (process)
667-
"Create a new tooling session handler for PROCESS."
668-
(lambda (response)
669-
(nrepl-dbind-response response (id new-session err)
646+
(error "Could not create new session (%s)" err)))
647+
(nrepl-dbind-response response-tooling (new-session err)
670648
(if new-session
671-
(with-current-buffer (process-buffer process)
672-
(remhash id nrepl-pending-requests)
649+
(with-current-buffer (process-buffer client)
673650
(setq nrepl-tooling-session new-session))
674651
(error "Could not create new tooling session (%s)" err)))))
675652

653+
(defun nrepl--init-connection-buffer (conn-buffer replp)
654+
"Initialize CONN-BUFFER as a connection buffer.
655+
If REPLP is non-nil, initialize as a REPL buffer."
656+
(let ((description (nrepl-sync-request:describe)))
657+
(nrepl-dbind-response description (ops versions)
658+
(with-current-buffer conn-buffer
659+
(setq nrepl-ops ops)
660+
(setq nrepl-versions versions)))
661+
(when replp
662+
(cider-repl-init conn-buffer))))
663+
676664
(defun nrepl-close (connection-buffer)
677665
"Close the nrepl connection for CONNECTION-BUFFER."
678666
(interactive (list (nrepl-current-connection-buffer)))
@@ -790,17 +778,13 @@ of the same \"op\" that came along."
790778
(when (> (cadr (time-subtract (current-time) time0))
791779
nrepl-sync-request-timeout)
792780
(error "Sync nREPL request timed out %s" request)))
781+
(-when-let (id (nrepl-dict-get response "id"))
782+
;; fixme: this should go away eventually when we get rid of
783+
;; pending-request hash table
784+
(with-current-buffer (nrepl-current-connection-buffer)
785+
(remhash id nrepl-pending-requests)))
793786
response))
794787

795-
(defun nrepl-request:clone (callback)
796-
"Sent a :clone request to create a new client session.
797-
Response will be handled by CALLBACK."
798-
(nrepl-send-request '("op" "clone") callback))
799-
800-
(defun nrepl-request:describe (callback)
801-
"Peform describe for the given server PROCESS."
802-
(nrepl-send-request (list "op" "describe") callback))
803-
804788
(defun nrepl-request:stdin (input callback)
805789
"Send a :stdin request with INPUT.
806790
Register CALLBACK as the response handler."
@@ -829,6 +813,14 @@ Register CALLBACK as the response handler."
829813
If NS is non-nil, include it in the request. SESSION defaults to current session."
830814
(nrepl-send-request (nrepl--eval-request input ns session) callback))
831815

816+
(defun nrepl-sync-request:clone ()
817+
"Sent a :clone request to create a new client session."
818+
(nrepl-send-sync-request '("op" "clone")))
819+
820+
(defun nrepl-sync-request:describe ()
821+
"Perform :describe request."
822+
(nrepl-send-sync-request (list "op" "describe")))
823+
832824
(defun nrepl-sync-request:eval (input &optional ns session)
833825
"Send the INPUT to the nREPL server synchronously.
834826
If NS is non-nil, include it in the request. SESSION defaults to current

0 commit comments

Comments
 (0)