[PATCH 0/1] Fix deadlocks related to child processes

David McFarland corngood@gmail.com
Sun Oct 28 19:23:00 GMT 2018

For a long time I've been struggling with intermittent deadlocks and
segfaults in emacs, seemingly related to invoking child processes.  I
recently found a reliable way to reproduce one such deadlock:

- install clean cygwin with: emacs-w32, clang
- install flycheck from elpa
- grab some non trivial C header e.g.:
  $ cp /usr/include/stdio.h test.h
- $ emacs -q test.h
- start flycheck:
  (progn (package-initialize)
         (require 'flycheck)
- add a character to the start of the first line
- wait for flygheck to complete
- repeat the last two steps until a deadlock occurs

Breaking in gdb showed the main thread in `cygheap_protect.acquire ()`,
from either _cfree or _cmalloc.  The thread holding the mutex was always
"flasio", and it would either be continually segfaulting or looping in

I added some debug prints to cygheap and determined that it flasio was
double-freeing an atomic_write_buf.  I added some more prints and found
that it was two different fhandler objects freeing the same buffer.

I then found that `fhandler_base_overlapped::copyto` would clear the
buffer pointer after the copy, but none of the derived classes (pipe,
fifo) did.

Attached is a patch which clears the buffer pointers when copying pipes
and fifos.

It would probably be safer to move the buffer clear to a `operator=`,
but I wanted to keep the patch as simple as possible and avoid

David McFarland (1):
  Cygwin: Fix cygheap corruption caused by cloned atomic buffer

 winsup/cygwin/fhandler.h | 2 ++
 1 file changed, 2 insertions(+)


More information about the Cygwin-patches mailing list