From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Eric Wong Newsgroups: gmane.comp.audio.sox.devel,gmane.comp.audio.sox Subject: [PATCH] use non-blocking stdin for interactive mode Date: Sat, 3 Oct 2015 22:13:18 +0000 Message-ID: <20151003221318.GA32546@dcvr.yhbt.net> References: Reply-To: sox-devel@lists.sourceforge.net NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1443910421 13574 80.91.229.3 (3 Oct 2015 22:13:41 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 3 Oct 2015 22:13:41 +0000 (UTC) Cc: sox-devel@lists.sourceforge.net To: Tarim , sox-users@lists.sourceforge.net Original-X-From: sox-devel-bounces@lists.sourceforge.net Sun Oct 04 00:13:35 2015 Return-path: Envelope-to: gcasd-sox-devel@m.gmane.org X-ACL-Warn: Content-Disposition: inline In-Reply-To: X-Spam-Score: -0.0 (/) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay domain X-Headers-End: 1ZiV3U-00066G-Ly X-BeenThere: sox-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: sox-devel-bounces@lists.sourceforge.net Xref: news.gmane.org gmane.comp.audio.sox.devel:429 gmane.comp.audio.sox:6043 Archived-At: Received: from lists.sourceforge.net ([216.34.181.88]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZiV3d-0001Am-Hq for gcasd-sox-devel@m.gmane.org; Sun, 04 Oct 2015 00:13:33 +0200 Received: from localhost ([127.0.0.1] helo=sfs-ml-2.v29.ch3.sourceforge.com) by sfs-ml-2.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1ZiV3Y-0004OP-DI; Sat, 03 Oct 2015 22:13:28 +0000 Received: from sog-mx-4.v43.ch3.sourceforge.com ([172.29.43.194] helo=mx.sourceforge.net) by sfs-ml-2.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1ZiV3X-0004OG-Fv; Sat, 03 Oct 2015 22:13:27 +0000 Received: from dcvr.yhbt.net ([64.71.152.64]) by sog-mx-4.v43.ch3.sourceforge.com with esmtp (Exim 4.76) id 1ZiV3U-00066G-Ly; Sat, 03 Oct 2015 22:13:27 +0000 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 1F5DF633849; Sat, 3 Oct 2015 22:13:19 +0000 (UTC) Tarim wrote: > I'm now thinking that this is actually a bug in the latest Ubuntu kernel > 3.13.0-65.105-generic where select's FD_ISSET returns true even when > there is no data to read! This is a kernel bug from a performance standpoint, but the select manpage explicitly documents spurious wakeups as a possibility which userspace must be prepared for. The following patch should fix the problem: ------------------8<------------------ Subject: [PATCH] use non-blocking stdin for interactive mode When accepting keyboard input, it is possible for select() to return a false-positive with spurious wakeups from inside the update_status callback. Using the FIONREAD ioctl in place of select is also a possibility, but may be less portable. --- Downloadable patch: http://80x24.org/spew/1443909787-27821-1-git-send-email-e%4080x24.org/raw In case upstream prefers a pull request: The following changes since commit 7e74b254b2a7c963be0bfce751fc5911fe681c12: Remove hepler script. It's mostly unmaintained, I don't know if anyone but me ever used it. In any case, those who want a custom Debian package should be capable of updating the debian/changelog entry on their own. (2015-02-26 22:48:40 -0500) are available in the git repository at: git://bogomips.org/sox nb-stdin for you to fetch changes up to 62a370a2a01cf1dcc0d5f9b9f49d1515706346cd: use non-blocking stdin for interactive mode (2015-10-03 22:04:50 +0000) ---------------------------------------------------------------- Eric Wong (1): use non-blocking stdin for interactive mode src/sox.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/sox.c b/src/sox.c index bab0f45..fdb7616 100644 --- a/src/sox.c +++ b/src/sox.c @@ -1789,6 +1789,18 @@ static int process(void) tcsetattr(fileno(stdin), TCSANOW, &modified_termios); } #endif +#if defined(F_GETFL) && defined(F_SETFL) && defined(O_NONBLOCK) + if (interactive) { + int fd = fileno(stdin); + int flags = fcntl(fd, F_GETFL); + if (flags == -1) { + lsx_warn("error getting flags on stdin descriptor: %s", strerror(errno)); + } else if (!(flags & O_NONBLOCK)) { + if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) + lsx_warn("error setting non-blocking on stdin: %s", strerror(errno)); + } + } +#endif signal(SIGTERM, sigint); /* Stop gracefully, as soon as we possibly can. */ signal(SIGINT , sigint); /* Either skip current input or behave as SIGTERM. */ -- EW ------------------------------------------------------------------------------