public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
From: Takashi Yano <takashi.yano@nifty.ne.jp>
To: cygwin-patches@cygwin.com
Cc: Takashi Yano <takashi.yano@nifty.ne.jp>
Subject: [PATCH] Cygwin: console: Add error handling for thread_sync_event
Date: Sat, 29 Jun 2024 19:06:50 +0900	[thread overview]
Message-ID: <20240629100742.2343-1-takashi.yano@nifty.ne.jp> (raw)

Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
---
 winsup/cygwin/fhandler/console.cc | 51 ++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 17 deletions(-)

diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc
index 1c0d5c815..881445824 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -272,17 +272,23 @@ cons_master_thread (VOID *arg)
   fhandler_console::handle_set_t handle_set;
   fh->get_duplicated_handle_set (&handle_set);
   HANDLE thread_sync_event;
-  DuplicateHandle (GetCurrentProcess (), fh->thread_sync_event,
-		   GetCurrentProcess (), &thread_sync_event,
-		   0, FALSE, DUPLICATE_SAME_ACCESS);
-  SetEvent (thread_sync_event);
-  master_thread_started = true;
-  /* Do not touch class members after here because the class instance
-     may have been destroyed. */
-  fhandler_console::cons_master_thread (&handle_set, ttyp);
-  fhandler_console::close_handle_set (&handle_set);
-  SetEvent (thread_sync_event);
-  CloseHandle (thread_sync_event);
+  if (DuplicateHandle (GetCurrentProcess (), fh->thread_sync_event,
+		       GetCurrentProcess (), &thread_sync_event,
+		       0, FALSE, DUPLICATE_SAME_ACCESS))
+    {
+      SetEvent (thread_sync_event);
+      master_thread_started = true;
+      /* Do not touch class members after here because the class instance
+	 may have been destroyed. */
+      fhandler_console::cons_master_thread (&handle_set, ttyp);
+      fhandler_console::close_handle_set (&handle_set);
+      SetEvent (thread_sync_event);
+      CloseHandle (thread_sync_event);
+      master_thread_started = false;
+    }
+  else
+    debug_printf ("cons_master_thread not started because thread_sync_event "
+		  "could not be duplicated %08x", GetLastError ());
   return 0;
 }
 
@@ -451,6 +457,8 @@ fhandler_console::cons_master_thread (handle_set_t *p, tty *ttyp)
 	case WAIT_CANCELED:
 	  break;
 	default: /* Error */
+	  free (input_rec);
+	  free (input_tmp);
 	  ReleaseMutex (p->input_mutex);
 	  return;
 	}
@@ -1847,9 +1855,12 @@ fhandler_console::open (int flags, mode_t)
       char name[MAX_PATH];
       shared_name (name, CONS_THREAD_SYNC, get_minor ());
       thread_sync_event = CreateEvent(NULL, FALSE, FALSE, name);
-      new cygthread (::cons_master_thread, this, "consm");
-      WaitForSingleObject (thread_sync_event, INFINITE);
-      CloseHandle (thread_sync_event);
+      if (thread_sync_event)
+	{
+	  new cygthread (::cons_master_thread, this, "consm");
+	  WaitForSingleObject (thread_sync_event, INFINITE);
+	  CloseHandle (thread_sync_event);
+	}
     }
   return 1;
 }
@@ -1910,9 +1921,15 @@ fhandler_console::close ()
 	  char name[MAX_PATH];
 	  shared_name (name, CONS_THREAD_SYNC, get_minor ());
 	  thread_sync_event = OpenEvent (MAXIMUM_ALLOWED, FALSE, name);
-	  con.owner = MAX_PID + 1;
-	  WaitForSingleObject (thread_sync_event, INFINITE);
-	  CloseHandle (thread_sync_event);
+	  if (thread_sync_event)
+	    {
+	      con.owner = MAX_PID + 1;
+	      WaitForSingleObject (thread_sync_event, INFINITE);
+	      CloseHandle (thread_sync_event);
+	    }
+	  else
+	    debug_printf ("Failed to open thread_sync_event %08x",
+			  GetLastError ());
 	}
       con.owner = 0;
     }
-- 
2.45.1


                 reply	other threads:[~2024-06-29 10:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20240629100742.2343-1-takashi.yano@nifty.ne.jp \
    --to=takashi.yano@nifty.ne.jp \
    --cc=cygwin-patches@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).