public inbox for cygwin-developers@cygwin.com
 help / color / mirror / Atom feed
From: Takashi Yano <takashi.yano@nifty.ne.jp>
To: cygwin-developers@cygwin.com
Subject: Re: Invalid handle error in fhandler_pty_master::close
Date: Fri, 8 Oct 2021 12:16:36 +0900	[thread overview]
Message-ID: <20211008121636.27eeb53d34a75db1b70ba3ec@nifty.ne.jp> (raw)
In-Reply-To: <ed4f4e25-3bea-7eac-a38b-67e8fd312847@cornell.edu>

[-- 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. */

  reply	other threads:[~2021-10-08  3:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-07 14:30 Ken Brown
2021-10-08  3:16 ` Takashi Yano [this message]
2021-10-08 16:15   ` Ken Brown
2021-10-08 16:31     ` Takashi Yano

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

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

  git send-email \
    --in-reply-to=20211008121636.27eeb53d34a75db1b70ba3ec@nifty.ne.jp \
    --to=takashi.yano@nifty.ne.jp \
    --cc=cygwin-developers@cygwin.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.
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).