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 <johan...@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


--
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

Reply via email to