Subject: 23.1; accept-process-output switching current-buffer

From: Uday S Reddy <[email-masked]>
Date: Sat, 10 Apr 2010 22:23:57 +0100

Reading the elisp manual doesn't indicate anywhere that a call such as 

   (accept-process-output process)

should change the current-buffer.  But it is happening.  It led to
some hairy asynchronous errors that took me an entire week to track
down.  Finally, I found an instance that was sort of reproducible, and
tested it with a code fragment such as this:

   (let ((wait nil) (buffer-x (current-buffer)))
     ...
     (if (not (equal (current-buffer) buffer-x))
        (debug nil wait))
     (setq wait t)
     (accept-process-output process)
     (if (not (equal (current-buffer) buffer-x))
        (debug nil wait))
     (setq wait nil)
     ...
   )

If the debugger is entered with the argument 't' that means that the
call to accept-process-output changed the 'current-buffer'.  The
following backtrace was obtained:

Debugger entered: (t)
  vm-imap-read-object(#<process IMAP<1>> t)
  vm-imap-read-object(#<process IMAP<1>>)
  vm-imap-read-response(#<process IMAP<1>>)
  vm-imap-read-response-and-verify(#<process IMAP<1>> "FLAGS FETCH")
  vm-imap-get-message-data-list(#<process IMAP<1>> 1 3672)
  vm-imap-retrieve-uid-and-flags-data()
  vm-imap-get-synchronization-data(t)
  vm-imap-synchronize-folder(t nil t t t t)
  vm-get-spooled-mail(t)
  vm-get-new-mail(nil)
  call-interactively(vm-get-new-mail nil nil)

The full file containing the code, VM's IMAP client, is attached.
The misbehaving accept-process-output call is in the
vm-imap-read-object function.

My theory of what happened here is as follows: VM ended an existing
IMAP session (#<process IMAP>) by sending a LOGOUT command, and
created a new one (#<process IMAP<1>>).  While it was working with the
second session, in the process-buffer, the server must have sent back
some response to the first session, which would have been accepted
during a call to accept-process-output.  This caused the
current-buffer to change to the process-buffer of the original
session. 

I found the problem orignally in Emacs 22.2, but checking it with 23.1
shows that the problem is still present in the current version.

Cheers,
Uday

-----

In GNU Emacs 23.1.1 (i386-mingw-nt5.1.2600)
 of 2009-07-30 on SOFT-MJASON
Windowing system distributor `Microsoft Corp.', version 5.1.2600
configured using `configure --with-gcc (4.4)'

Important settings:
  value of $LC_ALL: nil
  value of $LC_COLLATE: nil
  value of $LC_CTYPE: nil
  value of $LC_MESSAGES: nil
  value of $LC_MONETARY: nil
  value of $LC_NUMERIC: nil
  value of $LC_TIME: nil
  value of $LANG: C.UTF-8
  value of $XMODIFIERS: nil
  locale-coding-system: cp1252
  default-enable-multibyte-characters: t

Major mode: VM Summary

Minor modes in effect:
  savehist-mode: t
  tooltip-mode: t
  mouse-wheel-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  global-auto-composition-mode: t
  auto-encryption-mode: t
  line-number-mode: t
  transient-mark-mode: t

Recent input:
<return> g C-p C-p C-SPC C-n C-n C-n C-n C-n C-n C-n 
C-n C-n C-n C-n C-n C-x C-k C-x C-f d : / g n u SPC 
v m SPC t r u SPC / SPC e m SPC <backspace> <backspace> 
b u SPC SPC SPC SPC <backspace> - e m a c s 2 3 . t 
x t <return> C-y C-x , C-x C-s <help-echo> <down-mouse-1> 
<mouse-1> C-h i m e l SPC <return> SPC C-s p r o c 
e s s C-a m <return> SPC m o u t p u t SPC SPC <return> 
<down-mouse-1> <mouse-1> <wheel-down> <wheel-down> 
<double-wheel-down> <help-echo> <down-mouse-1> <mouse-1> 
<down-mouse-2> <mouse-2> <down-mouse-1> <mouse-1> <wheel-down> 
<wheel-down> <wheel-down> <down-mouse-1> <mouse-1> 
<wheel-up> <double-wheel-up> <wheel-down> <wheel-up> 
<wheel-up> <wheel-down> <wheel-down> <wheel-down> <wheel-up> 
<double-wheel-up> <triple-wheel-up> <triple-wheel-up> 
<triple-wheel-up> <down-mouse-1> <mouse-1> <wheel-down> 
<double-wheel-down> <wheel-up> <double-wheel-up> <triple-wheel-up> 
<wheel-up> <double-wheel-up> <triple-wheel-up> <down-mouse-1> 
<mouse-1> C-x b <return> q C-x , q C-x u C-n C-n C-n 
C-n C-n C-n C-n C-n C-n C-n C-n C-n C-k C-x C-s C-x 
b * B SPC <return> q C-x m C-x k <return> M-x v m <backspace> 
<backspace> r e p o r t - e m SPC SPC <return>

Recent messages:
Composing main Info directory...done
Mark saved where search started
Mark set
Undo!
Saving file d:/gnu/vm/trunk/bug-accept-process-output-emacs23.txt...
Wrote d:/gnu/vm/trunk/bug-accept-process-output-emacs23.txt
Back to top level.
Parsing d:/Home/udr/.mailrc...
Parsing y:/dotfiles/.mailrc... done
Parsing d:/Home/udr/.mailrc... done

------

[ATTACHMENT d:/gnu/vm/trunk23/lisp/vm-imap.el, text/plain]