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: dsp: Fix hang on close() if another thread calls write().
Date: Wed, 25 Jan 2023 19:56:22 +0900	[thread overview]
Message-ID: <20230125105622.473-1-takashi.yano@nifty.ne.jp> (raw)

fhandler_dev_dsp (OSS) has a problem that waitforallsent(), which is
called from close(), falls into infinite loop if another thread calls
write() accidentally after close(). This patch fixes the issue.

Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
---
 winsup/cygwin/fhandler/dsp.cc           | 9 +++++++++
 winsup/cygwin/local_includes/fhandler.h | 1 +
 2 files changed, 10 insertions(+)

diff --git a/winsup/cygwin/fhandler/dsp.cc b/winsup/cygwin/fhandler/dsp.cc
index 8798cf876..cfbf6bec7 100644
--- a/winsup/cygwin/fhandler/dsp.cc
+++ b/winsup/cygwin/fhandler/dsp.cc
@@ -1093,6 +1093,8 @@ fhandler_dev_dsp::open (int flags, mode_t)
 
   debug_printf ("ACCMODE=%y audio_in=%d audio_out=%d, err=%d, ret=%d",
 		flags & O_ACCMODE, num_in, num_out, err, ret);
+  if (ret)
+    being_closed = false;
   return ret;
 }
 
@@ -1106,6 +1108,12 @@ fhandler_dev_dsp::_write (const void *ptr, size_t len)
   int len_s = len;
   const char *ptr_s = static_cast <const char *> (ptr);
 
+  if (being_closed)
+    {
+      set_errno (EBADF);
+      return -1;
+    }
+
   if (audio_out_)
     /* nothing to do */;
   else if (IS_WRITE ())
@@ -1207,6 +1215,7 @@ int
 fhandler_dev_dsp::close ()
 {
   debug_printf ("audio_in=%p audio_out=%p", audio_in_, audio_out_);
+  being_closed = true;
   close_audio_in ();
   close_audio_out ();
   return fhandler_base::close ();
diff --git a/winsup/cygwin/local_includes/fhandler.h b/winsup/cygwin/local_includes/fhandler.h
index 5fe979538..e7315ae16 100644
--- a/winsup/cygwin/local_includes/fhandler.h
+++ b/winsup/cygwin/local_includes/fhandler.h
@@ -2758,6 +2758,7 @@ class fhandler_dev_dsp: public fhandler_base
   int audiochannels_;
   Audio_out *audio_out_;
   Audio_in  *audio_in_;
+  bool being_closed;
  public:
   fhandler_dev_dsp ();
   fhandler_dev_dsp *base () const {return (fhandler_dev_dsp *)archetype;}
-- 
2.39.0


             reply	other threads:[~2023-01-25 10:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-25 10:56 Takashi Yano [this message]
2023-01-25 12:35 ` Corinna Vinschen

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