public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Johannes Khoshnazar-Thoma <johannes@johannesthoma.com>
To: cygwin@cygwin.com
Subject: Re: cygwin application hangs on closing console
Date: Mon, 3 Jun 2024 15:20:32 +0200	[thread overview]
Message-ID: <95442cfe-a87c-4816-b337-b130e481e3f1@johannesthoma.com> (raw)
In-Reply-To: <5ed3b0b8-d4f8-454c-951a-fc7b85a7c302@johannesthoma.com>

Hi List,

We did more testing and it looks like the name of the event
that signals console master thread start and end is shared between
unrelated processes (it uses the console minor which is always (?)
0 when running from a powershell).

So since it is a two-state event (as opposed to a semaphore)
in theory the following can happen:

Process A	Process B
SetEvent(e)
		SetEvent(e)
Waitforevent(e)
		Waitforevent(e)

The second SetEvent does nothing. As a result the
later Waitforevent is stuck (which is what we observe).

So the question is: why should this event be used in
unrelated cygwin processes? Is there a technical reason
we don't understand (yet) for doing that (sharing the event).

We patched cygwin to use pseudo random event names (the
tm_usec field of gettimeofday()) and the stuckness vanished.
So unless there is a reason for sharing the event between
cygwin processes this patch should work:

 From f2e2d125a21487579ecb9173406c6322ee4ecfeb Mon Sep 17 00:00:00 2001
From: Johannes Thoma <johannes@johannesthoma.com>
Date: Wed, 29 May 2024 17:35:35 +0000
Subject: [PATCH] console: use pseudo random thread_sync_event name

---
  winsup/cygwin/fhandler/console.cc       | 9 +++++++--
  winsup/cygwin/local_includes/fhandler.h | 1 +
  2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc
index 1352482e9..d9c88d245 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -1895,7 +1895,12 @@ debug_printf("myself->pid %d con.owner %d\n", myself->pid, con.owner);
        if (GetModuleHandle ("ConEmuHk64.dll"))
  	hook_conemu_cygwin_connector ();
        char name[MAX_PATH];
-      shared_name (name, CONS_THREAD_SYNC, get_minor ());
+
+      struct timeval v;
+      gettimeofday(&v, NULL);
+      a_random_number = v.tv_usec;
+
+      shared_name (name, CONS_THREAD_SYNC, a_random_number);
        thread_sync_event = CreateEvent(NULL, FALSE, FALSE, name);
@@ -1983,7 +1988,7 @@ debug_printf("%s: 1\n", __func__);
  	  char name[MAX_PATH];
-	  shared_name (name, CONS_THREAD_SYNC, get_minor ());
+	  shared_name (name, CONS_THREAD_SYNC, a_random_number);
  	  thread_sync_event = OpenEvent (MAXIMUM_ALLOWED, FALSE, name);
diff --git a/winsup/cygwin/local_includes/fhandler.h b/winsup/cygwin/local_includes/fhandler.h
index 978d3e514..132dc6477 100644
--- a/winsup/cygwin/local_includes/fhandler.h
+++ b/winsup/cygwin/local_includes/fhandler.h
@@ -2215,6 +2215,7 @@ public:
    };
    typedef cons_handle_set_t handle_set_t;
    HANDLE thread_sync_event;
+  int a_random_number;
  private:
    static const unsigned MAX_WRITE_CHARS;
    static console_state *shared_console_info[MAX_CONS_DEV + 1];
--
2.17.1


  reply	other threads:[~2024-06-03 13:20 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-22 12:50 Johannes Khoshnazar-Thoma
2024-04-22 18:51 ` Takashi Yano
2024-04-23  9:20   ` Johannes Khoshnazar-Thoma
2024-04-23 10:26     ` Takashi Yano
2024-05-15 15:37       ` Johannes Khoshnazar-Thoma
2024-05-15 15:48         ` Johannes Khoshnazar-Thoma
2024-05-15 20:24           ` Brian Inglis
2024-05-21 16:23             ` Johannes Khoshnazar-Thoma
2024-06-03 13:20               ` Johannes Khoshnazar-Thoma [this message]
2024-06-28 12:17                 ` Takashi Yano
2024-06-28 15:32                   ` Takashi Yano
2024-07-01 11:47                     ` Johannes Khoshnazar-Thoma
2024-07-01 13:20                       ` Takashi Yano
2024-07-01 13:36                       ` Takashi Yano
2024-05-16  9:14           ` 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=95442cfe-a87c-4816-b337-b130e481e3f1@johannesthoma.com \
    --to=johannes@johannesthoma.com \
    --cc=cygwin@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).