unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Kangas <stefankangas@gmail.com>
To: Lars Ingebrigtsen <larsi@gnus.org>, Eli Zaretskii <eliz@gnu.org>
Cc: Visuwesh <visuweshm@gmail.com>, bugs@gnu.support, 50849@debbugs.gnu.org
Subject: bug#50849: 28.0.50; Proposal for Emacs daemon to signal when being busy
Date: Mon, 5 Sep 2022 09:56:58 -0400	[thread overview]
Message-ID: <CADwFkmmBp6WksbQgfY0MstPT8yS-aEmgk9RiX+jXfDTADbc+7A@mail.gmail.com> (raw)
In-Reply-To: <87edwsiy9p.fsf@gnus.org>

[-- Attachment #1: Type: text/plain, Size: 1224 bytes --]

tags 50849 + patch
thanks

Lars Ingebrigtsen <larsi@gnus.org> writes:

> I don't think a --timeout parameter will be useful here, though --
> nobody is going to type "emacsclient --timeout 10 -nw".
>
> So I think an informational message (like previously mentioned) is the
> best we can do here.

How about the attached patch?  It adds an informational message after 30
seconds by default.  With a "--timeout N" flag, we instead display a
message and exit (after N seconds).

Note that:

1. I didn't yet add documentation.
2. I've only tested the patch on GNU/Linux (*not* on MS-Windows).
3. To simulate a timeout for testing, you could try this:

diff --git a/lisp/server.el b/lisp/server.el
index 3caa335c4e..5dc220d0b3 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -1113,8 +1113,8 @@ server-process-filter
       (progn
 	(server-add-client proc)
 	;; Send our pid
-	(server-send-string proc (concat "-emacs-pid "
-					 (number-to-string (emacs-pid)) "\n"))
+        ;; (server-send-string proc (concat "-emacs-pid "
+        ;; 				 (number-to-string (emacs-pid)) "\n"))
 	(if (not (string-match "\n" string))
             ;; Save for later any partial line that remains.
             (when (> (length string) 0)

[-- Attachment #2: 0001-Add-new-timeout-flag-to-emacsclient.patch --]
[-- Type: text/x-diff, Size: 4930 bytes --]

From 7e418860ad2a7cd5d99737a4a7d187cf0f3d686c Mon Sep 17 00:00:00 2001
From: Stefan Kangas <stefankangas@gmail.com>
Date: Mon, 5 Sep 2022 15:50:20 +0200
Subject: [PATCH] Add new --timeout flag to emacsclient

* lib-src/emacsclient.c (DEFAULT_TIMEOUT): New constant.
(timeout): New static variable.
(longopts, shortopts, decode_options, print_help_and_exit): Add new
flag --timeout.
(set_socket_timeout, check_socket_timeout): New helper functions.
(main): Display a status message or exit after Emacs has not responded
for a while, depending on above new --timeout flag.  (Bug#50849)
---
 lib-src/emacsclient.c | 70 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 67 insertions(+), 3 deletions(-)

diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index 73c8e45a86..7bc8fc3d4d 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -64,6 +64,8 @@ Copyright (C) 1986-1987, 1994, 1999-2022 Free Software Foundation, Inc.
 
 # define egetenv(VAR) getenv (VAR)
 
+# define DEFAULT_TIMEOUT (30)
+
 #endif /* !WINDOWSNT */
 
 #include <ctype.h>
@@ -144,6 +146,9 @@ Copyright (C) 1986-1987, 1994, 1999-2022 Free Software Foundation, Inc.
 /* If non-NULL, the filename of the authentication file.  */
 static char const *server_file;
 
+/* If non-NULL, seconds to wait before timing out.  */
+static uintmax_t timeout;
+
 /* If non-NULL, the tramp prefix emacs must use to find the files.  */
 static char const *tramp_prefix;
 
@@ -178,6 +183,7 @@ Copyright (C) 1986-1987, 1994, 1999-2022 Free Software Foundation, Inc.
   { "server-file",	required_argument, NULL, 'f' },
   { "display",	required_argument, NULL, 'd' },
   { "parent-id", required_argument, NULL, 'p' },
+  { "timeout",	required_argument, NULL, 'w' },
   { "tramp",	required_argument, NULL, 'T' },
   { 0, 0, 0, 0 }
 };
@@ -185,7 +191,7 @@ Copyright (C) 1986-1987, 1994, 1999-2022 Free Software Foundation, Inc.
 /* Short options, in the same order as the corresponding long options.
    There is no '-p' short option.  */
 static char const shortopts[] =
-  "nqueHVtca:F:"
+  "nqueHVtcaw:F:"
 #ifdef SOCKETS_IN_FILE_SYSTEM
   "s:"
 #endif
@@ -496,7 +502,7 @@ decode_options (int argc, char **argv)
       int opt = getopt_long_only (argc, argv, shortopts, longopts, NULL);
       if (opt < 0)
 	break;
-
+      char* endptr;
       switch (opt)
 	{
 	case 0:
@@ -530,6 +536,17 @@ decode_options (int argc, char **argv)
 	  nowait = true;
 	  break;
 
+	case 'w':
+	  timeout = strtoumax (optarg, &endptr, 10);
+	  if (timeout <= 0 ||
+	      ((timeout == INTMAX_MAX || timeout == INTMAX_MIN)
+	       && errno == ERANGE))
+	    {
+	      fprintf (stderr, "Invalid timeout: \"%s\"\n", optarg);
+	      exit (EXIT_FAILURE);
+	    }
+	  break;
+
 	case 'e':
 	  eval = true;
 	  break;
@@ -671,6 +688,7 @@ print_help_and_exit (void)
 			Set the parameters of a new frame\n\
 -e, --eval    		Evaluate the FILE arguments as ELisp expressions\n\
 -n, --no-wait		Don't wait for the server to return\n\
+-w, --timeout		Seconds to wait before timing out\n\
 -q, --quiet		Don't display messages on success\n\
 -u, --suppress-output   Don't display return values from the server\n\
 -d DISPLAY, --display=DISPLAY\n\
@@ -1870,6 +1888,33 @@ start_daemon_and_retry_set_socket (void)
   return emacs_socket;
 }
 
+static void
+set_socket_timeout (HSOCKET socket, int seconds)
+{
+#ifndef WINDOWSNT
+  struct timeval timeout;
+  timeout.tv_sec = seconds;
+  timeout.tv_usec = 0;
+  setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
+#else
+  DWORD timeout = seconds * 1000;
+  setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char*) &timeout, sizeof timeout);
+#endif
+}
+
+static bool
+check_socket_timeout (int rl)
+{
+#ifndef WINDOWSNT
+  return (rl == -1)
+    && (errno == EAGAIN)
+    && (errno == EWOULDBLOCK);
+#else
+  return (rl == SOCKET_ERROR)
+    && (WSAGetLastError() != WSAETIMEDOUT);
+#endif
+}
+
 int
 main (int argc, char **argv)
 {
@@ -2086,15 +2131,34 @@ main (int argc, char **argv)
     }
   fflush (stdout);
 
+  set_socket_timeout (emacs_socket, timeout > 0 ? timeout : DEFAULT_TIMEOUT);
   /* Now, wait for an answer and print any messages.  */
   while (exit_status == EXIT_SUCCESS)
     {
+      bool retry = true;
+      bool msg_showed = false;
       do
 	{
 	  act_on_signals (emacs_socket);
 	  rl = recv (emacs_socket, string, BUFSIZ, 0);
+	  retry = check_socket_timeout (rl);
+	  if (retry)
+	    {
+	      if (timeout > 0)
+		{
+		  /* Don't retry if we were given a --timeout flag.  */
+		  fprintf (stderr, "\nServer not responding; timed out after %lu seconds",
+			   timeout);
+		  retry = false;
+		}
+	      else if (!msg_showed)
+		{
+		  msg_showed = true;
+		  fprintf (stderr, "\nServer not responding; use Ctrl+C to break");
+		}
+	    }
 	}
-      while (rl < 0 && errno == EINTR);
+      while ((rl < 0 && errno == EINTR) || retry);
 
       if (rl <= 0)
         break;
-- 
2.30.2


  parent reply	other threads:[~2022-09-05 13:56 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-27 14:23 bug#50849: 28.0.50; Proposal for Emacs daemon to signal when being busy Jean Louis
2021-09-27 15:27 ` Eli Zaretskii
2021-09-28  5:56   ` Lars Ingebrigtsen
2021-09-28  7:08     ` Eli Zaretskii
2022-09-02 11:09       ` Lars Ingebrigtsen
2022-09-02 11:21         ` Eli Zaretskii
2022-09-02 12:28           ` Lars Ingebrigtsen
2022-09-02 12:39             ` Eli Zaretskii
2022-09-02 12:44               ` Robert Pluim
2022-09-02 13:02               ` Lars Ingebrigtsen
2022-09-02 13:54                 ` Visuwesh
2022-09-02 14:02                   ` Eli Zaretskii
2022-09-03  9:48                     ` Lars Ingebrigtsen
2022-09-03 15:40                       ` Stefan Kangas
2022-09-03 15:57                         ` Eli Zaretskii
2022-09-04 10:59                         ` Lars Ingebrigtsen
2022-09-05 13:56                       ` Stefan Kangas [this message]
2022-09-05 19:07                         ` Lars Ingebrigtsen
2022-09-06  0:19                           ` Stefan Kangas
2022-09-06  2:31                             ` Eli Zaretskii
2022-09-06  3:33                               ` Visuwesh
2022-09-06 12:22                                 ` Eli Zaretskii
2022-09-06 14:02                                   ` Robert Pluim
2022-09-06 14:12                                     ` Eli Zaretskii
2022-09-06 14:20                                       ` Robert Pluim
2022-09-07  1:05                                   ` Stefan Kangas
2022-09-07  2:39                                     ` Eli Zaretskii
2022-09-07  8:18                                       ` Stefan Kangas
2022-09-07 10:34                                         ` Robert Pluim
2022-09-07 11:19                                           ` Eli Zaretskii
2022-09-08  1:47                                             ` Stefan Kangas
2022-09-08  5:59                                               ` Eli Zaretskii
2022-09-08 12:07                                                 ` Lars Ingebrigtsen
2022-09-08 13:42                                                   ` Eli Zaretskii
2022-09-08 13:46                                                     ` Lars Ingebrigtsen
2022-09-08 14:05                                                       ` Eli Zaretskii
2022-09-08 14:11                                                         ` Lars Ingebrigtsen
2022-09-08 14:15                                                           ` Eli Zaretskii
2022-09-08 14:19                                                             ` Robert Pluim
2022-09-08 16:02                                                               ` Eli Zaretskii
2022-09-09  8:47                                                                 ` Robert Pluim
2022-09-09  9:29                                                                   ` Stefan Kangas
2022-09-09  9:35                                                                     ` Robert Pluim
2022-09-09  9:38                                                                       ` Stefan Kangas
2022-09-09 11:31                                                                   ` Eli Zaretskii
2022-09-06  8:20                             ` Robert Pluim
2022-09-06  8:52                               ` Lars Ingebrigtsen
2022-09-03  1:07                 ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-03  9:49                   ` Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CADwFkmmBp6WksbQgfY0MstPT8yS-aEmgk9RiX+jXfDTADbc+7A@mail.gmail.com \
    --to=stefankangas@gmail.com \
    --cc=50849@debbugs.gnu.org \
    --cc=bugs@gnu.support \
    --cc=eliz@gnu.org \
    --cc=larsi@gnus.org \
    --cc=visuweshm@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).