From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#71223: 30.0.50; stack overflow after very fast opening and closing of frames Date: Wed, 29 May 2024 17:07:34 -0400 Message-ID: References: Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="9091"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Daniel Clemente , 71223@debbugs.gnu.org To: Chong Yidong Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed May 29 23:08:11 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1sCQWo-00024M-TS for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 29 May 2024 23:08:11 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sCQWY-0001OU-L4; Wed, 29 May 2024 17:07:54 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sCQWW-0001Nx-Ji for bug-gnu-emacs@gnu.org; Wed, 29 May 2024 17:07:52 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sCQWW-0004mZ-AX for bug-gnu-emacs@gnu.org; Wed, 29 May 2024 17:07:52 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sCQWf-0006Z2-NB for bug-gnu-emacs@gnu.org; Wed, 29 May 2024 17:08:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 29 May 2024 21:08:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 71223 X-GNU-PR-Package: emacs Original-Received: via spool by 71223-submit@debbugs.gnu.org id=B71223.171701687525217 (code B ref 71223); Wed, 29 May 2024 21:08:01 +0000 Original-Received: (at 71223) by debbugs.gnu.org; 29 May 2024 21:07:55 +0000 Original-Received: from localhost ([127.0.0.1]:54543 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sCQWY-0006Ye-Kt for submit@debbugs.gnu.org; Wed, 29 May 2024 17:07:55 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:46451) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sCQWW-0006YI-F7 for 71223@debbugs.gnu.org; Wed, 29 May 2024 17:07:53 -0400 Original-Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 9F7CF100043; Wed, 29 May 2024 17:07:36 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1717016855; bh=2tK2A0Lx4Q7Jayz2/hd3vSatSE3qD7Lux+gUFFzinUA=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=PIIOrY6pYcF3ec/9RQr7XaOhuH5EV0DRz/MvP1sfvjFPyLoJJeulVCTCH5VyG7Ixf VIP/RECePeD/61Ys+5XX3I9/zNxB158hl6/c5kf9z/wHozj3TaciHdTwbyKQAUP6tb a8EGZH3fygtHSjDPxyZukVwestLlUgbt5V1hBp0DMbwhBMDt52eP0VcLPdhrjVboQR NC9aFsk+U5ewETzvMAmp8YnWYw49cbZxjUJZTAHsoRmtBtmOnFVzpSyh+JAXyOWDEw 6I3uuDXYIcTE9LYFJiUz0rF1FD6VH4O/G3PztZvzikAGfgsxwUnZLRRwRvtXnuMG65 rMsmorw66qeUA== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 5756B10002E; Wed, 29 May 2024 17:07:35 -0400 (EDT) Original-Received: from lechazo (lechon.iro.umontreal.ca [132.204.27.242]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 4331712025D; Wed, 29 May 2024 17:07:35 -0400 (EDT) In-Reply-To: (Stefan Monnier's message of "Tue, 28 May 2024 20:28:37 -0400") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:286183 Archived-At: --=-=-= Content-Type: text/plain [ Hi Chong, see below a question for you. ] > Could you try the patch below which sequentializes the processing of the > process-filters to avoid such recursion? Here's a new version of the patch which fixes various shortcomings I noticed while trying it out. Also it shortens the wait times after displaying a message by applying the `sit-for` principle of not waiting if there's some "pending input" (where I consider `emacsclient` processes to be pending inputs as well). This way, the `sit-for` waiting times don't accumulate so much. Also I noticed a weird thing in commit 2a847524ab57b1b3d6eaa7e12b96be52dbb79509 Author: Chong Yidong Date: Sat Oct 2 20:03:44 2010 -0400 * lisp/server.el (server-process-filter, server-return-error): Give emacsclient time to shut down after receiving an error string. * etc/NEWS: Document tweak to emacsclient exit status. [...] diff --git a/lisp/server.el b/lisp/server.el index 0f1b0219a2c..e661f055e1a 100644 --- a/lisp/server.el +++ b/lisp/server.el @@ -876,6 +876,9 @@ server-process-filter (server-log "Authentication failed" proc) (server-send-string proc (concat "-error " (server-quote-arg "Authentication failed"))) + ;; Before calling `delete-process', give emacsclient time to + ;; receive the error string and shut down on its own. + (sit-for 1) (delete-process proc) ;; We return immediately (return-from server-process-filter))) @@ -1129,6 +1132,9 @@ server-return-error proc (concat "-error " (server-quote-arg (error-message-string err)))) (server-log (error-message-string err) proc) + ;; Before calling `delete-process', give emacsclient time to + ;; receive the error string and shut down on its own. + (sit-for 5) (delete-process proc))) (defun server-goto-line-column (line-col) Why do we wait for 5s in one spot but 1s in another? Stefan --=-=-= Content-Type: text/x-diff; charset=iso-8859-1 Content-Disposition: inline; filename=server.patch Content-Transfer-Encoding: quoted-printable diff --git a/lisp/server.el b/lisp/server.el index b65053267a6..06bba74bfd7 100644 --- a/lisp/server.el +++ b/lisp/server.el @@ -438,7 +438,8 @@ server-sentinel (ignore-errors (delete-file (process-get proc :server-file)))) (server-log (format "Status changed to %s: %s" - (process-status proc) msg) proc) + (process-status proc) msg) + proc) (server-delete-client proc)) =20 (defun server--on-display-p (frame display) @@ -1046,6 +1047,9 @@ server-execute-continuation (process-put proc 'continuation nil) (if continuation (ignore-errors (funcall continuation))))) =20 +(defvar server--pending-filter-calls nil) +(defvar server--processing-pending-filters nil) + (cl-defun server-process-filter (proc string) "Process a request from the server to edit some files. PROC is the server process. STRING consists of a sequence of @@ -1145,6 +1149,37 @@ server-process-filter `-suspend' Suspend this terminal, i.e., stop the client process. Sent when the user presses \\[suspend-frame]." + ;; Push this to the end of the list, so the list is in the order in which + ;; we need to process it. + ;; FIXME: This implies an O(N=B2) worst-case, which is not good: + ;; we should use a "true" O(N) queue. + (setq server--pending-filter-calls + (nconc server--pending-filter-calls (list (cons proc string)))) + (unless server--processing-pending-filters + (server-process-pending-filters))) + +(defun server-process-pending-filters () + (let ((server--processing-pending-filters t)) + (unwind-protect + (while server--pending-filter-calls + (let* ((oldest (pop server--pending-filter-calls))) + (server--process-filter (car oldest) (cdr oldest)))) + ;; In case we're exiting early (e.g. for `server-goto-toplevel'), + ;; make sure we continue running the other pending filters. + (when server--pending-filter-calls + (run-with-timer 0 nil #'server-process-pending-filters))))) + +(defun server--message-timed (time &rest args) + (apply #'message args) + ;; FIXME: Ideally we should not need `sit-for' here and instead use + ;; some `message-timed' call which makes sure the message is visible + ;; without forcing us to wait here. + ;; If there's already another process-filter pending, skip `sit-for', + ;; just as it does when there's pending user input. + (unless (consp server--pending-filter-calls) + (sit-for time))) + +(cl-defun server--process-filter (proc string) (server-log (concat "Received " string) proc) ;; First things first: let's check the authentication (unless (process-get proc :authenticated) @@ -1158,8 +1193,7 @@ server-process-filter ;; Display the error as a message and give the user time to see ;; it, in case the error written by emacsclient to stderr is not ;; visible for some reason. - (message "Authentication failed") - (sit-for 2) + (server--message-timed 2 "Authentication failed") (server-send-string proc (concat "-error " (server-quote-arg "Authentication failed"))) (unless (eq system-type 'windows-nt) @@ -1169,10 +1203,10 @@ server-process-filter (delete-terminal terminal)))) ;; Before calling `delete-process', give emacsclient time to ;; receive the error string and shut down on its own. - (sit-for 1) - (delete-process proc) + ;; FIXME: Why do we wait 1s here but 5s in the other one? + (run-with-timer 1 nil #'delete-process proc) ;; We return immediately. - (cl-return-from server-process-filter))) + (cl-return-from server--process-filter))) (let ((prev (process-get proc 'previous-string))) (when prev (setq string (concat prev string)) @@ -1507,8 +1541,7 @@ server-return-error ;; Display the error as a message and give the user time to see ;; it, in case the error written by emacsclient to stderr is not ;; visible for some reason. - (message (error-message-string err)) - (sit-for 2) + (server--message-timed 2 (error-message-string err)) (server-send-string proc (concat "-error " (server-quote-arg (error-message-string err)))) @@ -1520,8 +1553,8 @@ server-return-error (delete-terminal terminal)))) ;; Before calling `delete-process', give emacsclient time to ;; receive the error string and shut down on its own. - (sit-for 5) - (delete-process proc))) + ;; FIXME: Why do we wait 5s here but 1s in the other one? + (run-with-timer 5 nil #'delete-process proc))) =20 (defun server-goto-line-column (line-col) "Move point to the position indicated in LINE-COL. --=-=-=--