public inbox for cygwin-developers@cygwin.com
 help / color / mirror / Atom feed
* Invalid handle error in fhandler_pty_master::close
@ 2021-10-07 14:30 Ken Brown
  2021-10-08  3:16 ` Takashi Yano
  0 siblings, 1 reply; 4+ messages in thread
From: Ken Brown @ 2021-10-07 14:30 UTC (permalink / raw)
  To: cygwin-devel

I'm occasionally seeing what appears to be an Invalid Handle error in 
fhandler_pty_master::close at fhandler_tty.cc:2044.

   CloseHandle (attach_mutex);

This sometimes happens when I exit emacs while a process is running.  (It's 
always been a compilation process when I've seen this, but that may or may not 
be relevant.)  Here's a recent example where I caught this under gdb:

Thread 1 "emacs" received signal SIGSEGV, Segmentation fault.
0x00007ff8ebae1548 in ntdll!RtlVirtualUnwind ()
    from /c/WINDOWS/SYSTEM32/ntdll.dll
(gdb) bt
#0  0x00007ff8ebae1548 in ntdll!RtlVirtualUnwind ()
    from /c/WINDOWS/SYSTEM32/ntdll.dll
#1  0x00007ff8ebae1040 in ntdll!RtlVirtualUnwind ()
    from /c/WINDOWS/SYSTEM32/ntdll.dll
#2  0x00007ff8ebae0e7b in ntdll!RtlVirtualUnwind ()
    from /c/WINDOWS/SYSTEM32/ntdll.dll
#3  0x00007ff8ebb013a8 in ntdll!RtlRaiseException ()
    from /c/WINDOWS/SYSTEM32/ntdll.dll
#4  0x00007ff8ebb50bfe in ntdll!KiUserExceptionDispatcher ()
    from /c/WINDOWS/SYSTEM32/ntdll.dll
#5  0x00007ff8ebb50c7a in ntdll!KiRaiseUserExceptionDispatcher ()
    from /c/WINDOWS/SYSTEM32/ntdll.dll
#6  0x00007ff8e966a3c5 in KERNELBASE!CloseHandle ()
    from /c/WINDOWS/System32/KERNELBASE.dll
#7  0x00000001800bb4d8 in fhandler_pty_master::close (this=0x18036ec90)
     at 
/usr/src/debug/cygwin-3.3.0-0.2.6c1f49f83fde/winsup/cygwin/fhandler_tty.cc:2044
#8  0x000000018006b7e5 in fhandler_base::close_with_arch (this=0x18036f2b0)
     at /usr/src/debug/cygwin-3.3.0-0.2.6c1f49f83fde/winsup/cygwin/fhandler.cc:1185
#9  0x000000018014207d in close (fd=15)
     at /usr/src/debug/cygwin-3.3.0-0.2.6c1f49f83fde/winsup/cygwin/syscalls.cc:1652
#10 0x000000018018f3eb in _sigfe () at sigfe.s:35
#11 0x000000010058b787 in posix_close (fd=15, flag=1)
     at ../../emacs-28/src/sysdep.c:2421
#12 0x000000010058b7c1 in emacs_close (fd=15)
     at ../../emacs-28/src/sysdep.c:2439
#13 0x0000000100688597 in close_process_fd (fd_addr=0x800dcdf9c)
     at ../../emacs-28/src/process.c:2023
#14 0x000000010068d990 in deactivate_process (proc=XIL(0x800dcded5))
     at ../../emacs-28/src/process.c:4683
#15 0x0000000100685ba6 in remove_process (proc=XIL(0x800dcded5))
     at ../../emacs-28/src/process.c:937
[...]
Lisp Backtrace:
"yes-or-no-p" (0xffffb758)
0xff4760 PVEC_COMPILED
"save-buffers-kill-emacs" (0xffffbe88)
"save-buffers-kill-terminal" (0xffffc1a0)
"funcall-interactively" (0xffffc198)

Thread 1 "emacs" received signal SIGSEGV, Segmentation fault.
0x00000000ffff9050 in ?? ()
The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on".
Evaluation of the expression containing the function
(backtrace_function) will be abandoned.
When the function is done executing, GDB will silently stop.

Even though gdb reports SIGSEGV, that seems to be a result of something that 
happened while gdb was evaluating a function defined in emacs's .gdbinit.  On a 
different occasion, I explicitly see an Invalid Handle exception while running 
under gdb.  Here's an example of that:

gdb: unknown target exception 0xc0000008 at 0x7ff8ebb50c7a

Thread 1 "emacs" received signal ?, Unknown signal.
0x00007ff8ebb50c7a in ntdll!KiRaiseUserExceptionDispatcher ()
    from /c/WINDOWS/SYSTEM32/ntdll.dll
(gdb) bt
#0  0x00007ff8ebb50c7a in ntdll!KiRaiseUserExceptionDispatcher ()
    from /c/WINDOWS/SYSTEM32/ntdll.dll
#1  0x00007ff8e966a3c5 in KERNELBASE!CloseHandle ()
    from /c/WINDOWS/System32/KERNELBASE.dll
#2  0x00000001800bb4d8 in fhandler_pty_master::close (this=0x180358370)
     at 
/usr/src/debug/cygwin-3.3.0-0.2.6c1f49f83fde/winsup/cygwin/fhandler_tty.cc:2044
#3  0x000000018006b7e5 in fhandler_base::close_with_arch (this=0x180358060)
     at /usr/src/debug/cygwin-3.3.0-0.2.6c1f49f83fde/winsup/cygwin/fhandler.cc:1185
#4  0x000000018013ea3f in close_all_files (norelease=norelease@entry=false)
     at /usr/src/debug/cygwin-3.3.0-0.2.6c1f49f83fde/winsup/cygwin/syscalls.cc:102
#5  0x0000000180048243 in do_exit (status=0)
     at /usr/src/debug/cygwin-3.3.0-0.2.6c1f49f83fde/winsup/cygwin/dcrt0.cc:1180
#6  0x000000018004841f in _exit (n=<optimized out>)
     at /usr/src/debug/cygwin-3.3.0-0.2.6c1f49f83fde/winsup/cygwin/dcrt0.cc:1297
#7  0x00000001801ad68d in exit (code=0)
     at /usr/src/debug/cygwin-3.3.0-0.2.6c1f49f83fde/newlib/libc/stdlib/exit.c:64
#8  0x0000000180048403 in cygwin_exit (n=0)
     at /usr/src/debug/cygwin-3.3.0-0.2.6c1f49f83fde/winsup/cygwin/dcrt0.cc:1291
#9  0x000000018018f3eb in _sigfe () at sigfe.s:35
#10 0x0000000100520797 in Fkill_emacs (arg=XIL(0))
     at ../../emacs-28/src/emacs.c:2669
[...]
Lisp Backtrace:
"kill-emacs" (0xffff9450)
"save-buffers-kill-emacs" (0xffff99f8)
"save-buffers-kill-terminal" (0xffffa060)
[...]

If it would help, I can give more details about exactly what I was doing in 
emacs when these errors occurred, but I don't have a reliable reproduction 
recipe, and I don't have any way of reproducing it that doesn't involve emacs.

Ken

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Invalid handle error in fhandler_pty_master::close
  2021-10-07 14:30 Invalid handle error in fhandler_pty_master::close Ken Brown
@ 2021-10-08  3:16 ` Takashi Yano
  2021-10-08 16:15   ` Ken Brown
  0 siblings, 1 reply; 4+ messages in thread
From: Takashi Yano @ 2021-10-08  3:16 UTC (permalink / raw)
  To: cygwin-developers

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

Hi Ken,

On Thu, 7 Oct 2021 10:30:32 -0400
Ken Brown wrote:
> I'm occasionally seeing what appears to be an Invalid Handle error in 
> fhandler_pty_master::close at fhandler_tty.cc:2044.
> 
>    CloseHandle (attach_mutex);

Could you please check if the patch attached fixes the issue?

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

[-- Attachment #2: attach_mutex.patch --]
[-- Type: application/octet-stream, Size: 1047 bytes --]

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 05fe5348a..823dabf73 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -57,6 +57,7 @@ struct pipe_reply {
 };
 
 extern HANDLE attach_mutex; /* Defined in fhandler_console.cc */
+static LONG NO_COPY master_cnt = 0;
 
 inline static bool pcon_pid_alive (DWORD pid);
 
@@ -2041,7 +2042,8 @@ fhandler_pty_master::close ()
 	    }
 	  release_output_mutex ();
 	  master_fwd_thread->terminate_thread ();
-	  CloseHandle (attach_mutex);
+	  if (InterlockedDecrement (&master_cnt) == 0)
+	    CloseHandle (attach_mutex);
 	}
     }
 
@@ -2876,7 +2878,8 @@ fhandler_pty_master::setup ()
   if (!(pcon_mutex = CreateMutex (&sa, FALSE, buf)))
     goto err;
 
-  attach_mutex = CreateMutex (&sa, FALSE, NULL);
+  if (InterlockedIncrement (&master_cnt) == 1)
+    attach_mutex = CreateMutex (&sa, FALSE, NULL);
 
   /* Create master control pipe which allows the master to duplicate
      the pty pipe handles to processes which deserve it. */

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Invalid handle error in fhandler_pty_master::close
  2021-10-08  3:16 ` Takashi Yano
@ 2021-10-08 16:15   ` Ken Brown
  2021-10-08 16:31     ` Takashi Yano
  0 siblings, 1 reply; 4+ messages in thread
From: Ken Brown @ 2021-10-08 16:15 UTC (permalink / raw)
  To: cygwin-developers

Hi Takashi,

On 10/7/2021 11:16 PM, Takashi Yano wrote:
> On Thu, 7 Oct 2021 10:30:32 -0400
> Ken Brown wrote:
>> I'm occasionally seeing what appears to be an Invalid Handle error in
>> fhandler_pty_master::close at fhandler_tty.cc:2044.
>>
>>     CloseHandle (attach_mutex);
> 
> Could you please check if the patch attached fixes the issue?

Yes.  If you could send it in git-format-patch form and document it in the 3.3.0 
release notes, I'll push it.

Thanks!

Ken

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Invalid handle error in fhandler_pty_master::close
  2021-10-08 16:15   ` Ken Brown
@ 2021-10-08 16:31     ` Takashi Yano
  0 siblings, 0 replies; 4+ messages in thread
From: Takashi Yano @ 2021-10-08 16:31 UTC (permalink / raw)
  To: cygwin-developers

On Fri, 8 Oct 2021 12:15:45 -0400
Ken Brown wrote:
> Hi Takashi,
> 
> On 10/7/2021 11:16 PM, Takashi Yano wrote:
> > On Thu, 7 Oct 2021 10:30:32 -0400
> > Ken Brown wrote:
> >> I'm occasionally seeing what appears to be an Invalid Handle error in
> >> fhandler_pty_master::close at fhandler_tty.cc:2044.
> >>
> >>     CloseHandle (attach_mutex);
> > 
> > Could you please check if the patch attached fixes the issue?
> 
> Yes.  If you could send it in git-format-patch form and document it in the 3.3.0 
> release notes, I'll push it.

Thanks for testing. I have submitted the patch to cygwin-patches@cygwin.com.

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-10-08 16:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-07 14:30 Invalid handle error in fhandler_pty_master::close Ken Brown
2021-10-08  3:16 ` Takashi Yano
2021-10-08 16:15   ` Ken Brown
2021-10-08 16:31     ` Takashi Yano

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).