Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sesman-restart regression issue with SIGHUP handling in server #3363

Merged
merged 2 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [#3341](https://github.com/clojure-emacs/cider/issues/3341): Escape clojure-cli args on MS-Windows on non powershell invocations.
- [#3353](https://github.com/clojure-emacs/cider/issues/3353): Fix regression which caused new connections to prompt for reusing dead REPLs.
- [#3355](https://github.com/clojure-emacs/cider/pull/3355): Fix `cider-mode` disabling itself after a disconnect when `cider-auto-mode` is set to nil.
- [#3362](https://github.com/clojure-emacs/cider/issues/3362): Fix `sesman-restart` regression issue.

### Changes

Expand Down
11 changes: 7 additions & 4 deletions nrepl-client.el
Original file line number Diff line number Diff line change
Expand Up @@ -1191,8 +1191,9 @@ up."
(declare-function cider--close-connection "cider-connection")
(defun nrepl-server-sentinel (process event)
"Handle nREPL server PROCESS EVENT.
On a fatal EVENT, attempt to close any open client connections, and signal
an `error' if the nREPL PROCESS exited because it couldn't start up."
If the nREPL PROCESS failed to initiate and encountered a fatal EVENT
signal, raise an 'error'. Additionally, if the EVENT signal is SIGHUP,
close any existing client connections."
;; only interested on fatal signals.
(when (not (process-live-p process))
(emacs-bug-46284/when-27.1-windows-nt
Expand All @@ -1212,8 +1213,10 @@ an `error' if the nREPL PROCESS exited because it couldn't start up."
(eq (buffer-local-value 'nrepl-server-buffer b)
server-buffer))
(buffer-list))))
;; close any known open client connections
(mapc #'cider--close-connection clients)

;; see https://github.com/clojure-emacs/cider/pull/3333
(when (string-match-p "^hangup" event)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be better to explain in comments this behavior, so people don't have to lookup the github issue, but this will do for now.

(mapc #'cider--close-connection clients))

(if (process-get process :cider--nrepl-server-ready)
(progn
Expand Down
30 changes: 30 additions & 0 deletions test/cider-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,36 @@
;; kill server
(delete-process (get-buffer-process client-buffer))))))))

(describe "sesman"
(it "can restart session"
(with-temp-buffer
(let* ((server-process (nrepl-start-mock-server-process))
(server-buffer (process-buffer server-process)))
;; wait for the connection to be established
(nrepl-tests-poll-until (local-variable-p 'nrepl-endpoint server-buffer) 5)
(let ((client-buffer (cider-connect-sibling-clj
`(:repl-buffer ,(current-buffer))
server-buffer))
(endpoint-bef)
(endpoint-aft))
(expect (buffer-local-value 'cider-repl-type client-buffer)
:to-equal 'clj)

(with-current-buffer (cider-current-repl)
(setq endpoint-bef nrepl-endpoint))

(sesman-restart)
ikappaki marked this conversation as resolved.
Show resolved Hide resolved
;; wait until a new server is brought up, i.e. the port has
;; changed. It will throw if it doesn't.
(nrepl-tests-poll-until (when-let ((repl (cider-current-repl)))
(with-current-buffer repl
(setq endpoint-aft nrepl-endpoint)
;; (message ":endpoints %S %S" endpoint-bef endpoint-aft)
(not (= (plist-get endpoint-bef :port) (plist-get endpoint-aft :port)))))
5)
;; kill server
(delete-process (get-buffer-process client-buffer)))))))

(provide 'cider-tests)

;;; cider-tests.el ends here
16 changes: 14 additions & 2 deletions test/nrepl-server-mock.el
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@
(require 'nrepl-client)
(require 'nrepl-tests-utils "test/utils/nrepl-tests-utils")
(require 'queue)
(require 'cl)

(defun nrepl-server-mock-filter (proc output)
"Handle the nREPL message found in OUTPUT sent by the client
PROC. Minimal implementation, just enough for fulfilling clients' testing
requirements."
(mock/log! ":mock.filter/output %s :msg %s" proc output)
;; (mock/log! ":mock.filter/output %s :msg %s" proc output)

(condition-case error-details
(let* ((msg (queue-dequeue (cdr (nrepl-bdecode output))))
(_ (mock/log! ":mock.filter/msg :in %S" msg))
(response (pcase msg
(`(dict "op" "clone" "id" ,id)
`(dict "id" ,id
Expand All @@ -48,10 +50,20 @@ requirements."
"new-session" "a-new-session"))

(`(dict "op" "describe" "session" ,session "id" ,id)
`(dict "id" ,id "session" ,session "status"
("done")))
;; Eval op can include other fields in addition to the
;; code, we only need the signature and the session and
;; id fields at the end.
(`(dict "op" "eval" "code" ,_code . ,rest)
(cl-destructuring-bind (_ session _ id) (seq-drop rest (- (seq-length rest) 4))
`(dict "id" ,id "session" ,session "status"
("done"))))
(`(dict "op" "close" "session" ,session "id" ,id)
`(dict "id" ,id "session" ,session "status"
("done"))))))

(mock/log! ":mock.filter/msg :in %s :out %s" msg response)
(mock/log! ":mock.filter/msg :out %S" response)
(if (not response)
(progn
(mock/log! ":mock.filter/unsupported-msg :in %s :msg %s"
Expand Down