Subject: Re: bug#29170: Infinite loop noticed on Emacs in OpenBSD

From: [email-masked] (Grégoire Jadi)
Date: Fri, 17 Nov 2017 15:36:19 +0100

Manuel Giraud <[email-masked]> writes:

> Hi,

Hello Manuel,

Thanks for the reply and sorry for the delay.

> Here is a patch against HEAD to avoid the hang in infinite loop. The
> 'x-selection-timeout' pause will still be there even with this patch.

I've tested your patch on emacs-25-3.1 and it does prevent the infinite
loop. However, if the user sends input to emacs while it is waiting, it
raises the following backtrace :

Debugger entered--Lisp error: (error "Timed out waiting for reply from selection owner")
  x-get-selection-internal(SECONDARY STRING nil nil)
  #[1026 "\300$\207" [x-get-selection-internal] 9 "\n\n(fn SELECTION-SYMBOL TARGET-TYPE &optional TIME-STAMP TERMINAL)"](SECONDARY STRING)
  apply(#[1026 "\300$\207" [x-get-selection-internal] 9 "\n\n(fn SELECTION-SYMBOL TARGET-TYPE &optional TIME-STAMP TERMINAL)"] (SECONDARY STRING))
  gui-backend-get-selection(SECONDARY STRING)
  gui-get-selection(SECONDARY STRING)
  (let ((value (quote SECONDARY)) (x-selection-timeout 2000)) (gui-get-selection value (quote UTF8_STRING)) (gui-get-selection value (quote COMPOUND_TEXT)) (gui-get-selection value (quote STRING)) (gui-get-selection value (quote TEXT)))
  eval((let ((value (quote SECONDARY)) (x-selection-timeout 2000)) (gui-get-selection value (quote UTF8_STRING)) (gui-get-selection value (quote COMPOUND_TEXT)) (gui-get-selection value (quote STRING)) (gui-get-selection value (quote TEXT))) nil)
  elisp--eval-last-sexp(nil)
  eval-last-sexp(nil)
  funcall-interactively(eval-last-sexp nil)
  call-interactively(eval-last-sexp nil nil)
  command-execute(eval-last-sexp)

But it's still better than a complete freeze IMO.

> My understandings:
>
> On OpenBSD, most of the time, the function
> "xselect.c/x_get_foreign_selection" won't get a SelectNotify with
> (SECONDARY, TEXT) as arguments (that I did not understand and it might
> never happen on other oses).
>
> But then, while waiting at most 'x-selection-timeout' into
> "process.c/wait_reading_process_output" the 'now' variable won't have a
> chance of being invalidated or updated and that is what cause the
> infinite loop.
>
> Someone more knowledgeable of "process.c/wait_reading_process_output"
> might have a better solution to this problem.
>
> diff --git a/src/process.c b/src/process.c
> index fc46e74332..25bd28a82b 100644
> --- a/src/process.c
> +++ b/src/process.c
> @@ -5115,8 +5115,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
>        /* Exit if already run out.  */
>        if (wait == TIMEOUT)
>  	{
> -	  if (!timespec_valid_p (now))
> -	    now = current_timespec ();
> +	  now = current_timespec ();
>  	  if (timespec_cmp (end_time, now) <= 0)
>  	    break;
>  	  timeout = timespec_sub (end_time, now);