From: Eric Wong <normalperson@yhbt.net>
To: "Måns Rullgård" <mans@mansr.com>
Cc: sox-devel@lists.sourceforge.net
Subject: Re: [WIP/PATCH] restart writes on EINTR
Date: Sat, 11 Jun 2022 02:17:15 +0000 [thread overview]
Message-ID: <20220611021715.GA16306@dcvr> (raw)
In-Reply-To: <yw1xv8t88hkn.fsf@mansr.com>
Måns Rullgård <mans@mansr.com> wrote:
> The return value and ft->tell_off will be wrong in case the fwrite() was
> restarted.
Right, updated patch (covers fread + fflush) below; but still
somewhere else is hitting EINTR... I'm still alive for now,
but not feeling great about it :<
> What did you do that caused the EINTR error?
SIGWINCH
-------8<-------
Subject: [PATCH] WIP EINTR handling
Still incomplete, SIGWINCH in a complex pipeline
(sox -M "|sox ..." "|sox ..." -p) is still causing problems for me
even with this patch. Not sure how much time I can devote to
tracking more of it down of if I'll just setsid the parent script
and call it a day since I fear this can turn into a long-term
whack-a-mole situation...
---
src/formats.c | 6 ++++-
src/formats_i.c | 59 +++++++++++++++++++++++++++++++++++++++----------
2 files changed, 52 insertions(+), 13 deletions(-)
diff --git a/src/formats.c b/src/formats.c
index 3fcf4382..31ab99f8 100644
--- a/src/formats.c
+++ b/src/formats.c
@@ -370,6 +370,10 @@ static sox_bool is_url(char const * text) /* detects only wget-supported URLs */
static int xfclose(FILE * file, lsx_io_type io_type)
{
+ int ret;
+ do {
+ ret = fflush(file);
+ } while (ret == EOF && errno == EINTR);
return
#ifdef HAVE_POPEN
io_type != lsx_io_file? pclose(file) :
@@ -1064,7 +1068,7 @@ int sox_close(sox_format_t * ft)
if (ft->fp == stdin) {
sox_globals.stdin_in_use_by = NULL;
} else if (ft->fp == stdout) {
- fflush(stdout);
+ lsx_flush(ft);
sox_globals.stdout_in_use_by = NULL;
} else if (ft->fp) {
xfclose(ft->fp, ft->io_type);
diff --git a/src/formats_i.c b/src/formats_i.c
index 7048040d..c4ead348 100644
--- a/src/formats_i.c
+++ b/src/formats_i.c
@@ -95,11 +95,24 @@ int lsx_check_read_params(sox_format_t * ft, unsigned channels,
*/
size_t lsx_readbuf(sox_format_t * ft, void *buf, size_t len)
{
- size_t ret = fread(buf, (size_t) 1, len, (FILE*)ft->fp);
- if (ret != len && ferror((FILE*)ft->fp))
- lsx_fail_errno(ft, errno, "lsx_readbuf");
- ft->tell_off += ret;
- return ret;
+ FILE *fp = (FILE*)ft->fp;
+
+ while (1) {
+ size_t ret = fread(buf, 1, len, fp);
+ ft->tell_off += ret;
+
+ if (ret)
+ return ret;
+
+ if (feof(fp))
+ break;
+ if (ferror(fp) && errno != EINTR) {
+ lsx_fail_errno(ft, errno, "lsx_readbuf");
+ break;
+ }
+ }
+
+ return 0;
}
/* Skip input without seeking. */
@@ -129,13 +142,29 @@ int lsx_padbytes(sox_format_t * ft, size_t n)
*/
size_t lsx_writebuf(sox_format_t * ft, void const * buf, size_t len)
{
- size_t ret = fwrite(buf, (size_t) 1, len, (FILE*)ft->fp);
- if (ret != len) {
- lsx_fail_errno(ft, errno, "error writing output file");
- clearerr((FILE*)ft->fp); /* Allows us to seek back to write header */
+ FILE *fp = (FILE*)ft->fp;
+ const char *c = (const char *)buf;
+ sox_uint64_t orig_off = ft->tell_off;
+
+ while (len) {
+ size_t ret = fwrite(c, 1, len, fp);
+
+ ft->tell_off += ret;
+ c += ret;
+ len -= ret;
+ if (len && ferror(fp)) {
+ if (errno == EINTR) {
+ clearerr(fp);
+ /* retry */
+ } else {
+ lsx_fail_errno(ft, errno, "error writing output file");
+ clearerr(fp); /* Allows us to seek back to write header */
+ break;
+ }
+ }
}
- ft->tell_off += ret;
- return ret;
+
+ return ft->tell_off - orig_off;
}
sox_uint64_t lsx_filelength(sox_format_t * ft)
@@ -148,7 +177,13 @@ sox_uint64_t lsx_filelength(sox_format_t * ft)
int lsx_flush(sox_format_t * ft)
{
- return fflush((FILE*)ft->fp);
+ int ret;
+
+ do {
+ ret = fflush((FILE *)ft->fp);
+ } while (ret == EOF && errno == EINTR);
+
+ return ret;
}
off_t lsx_tell(sox_format_t * ft)
_______________________________________________
SoX-devel mailing list
SoX-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sox-devel
prev parent reply other threads:[~2022-06-11 2:17 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-05 21:07 [WIP/PATCH] restart writes on EINTR Eric Wong
2022-06-10 19:05 ` Måns Rullgård
2022-06-11 2:17 ` Eric Wong [this message]
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-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://lists.sourceforge.net/lists/listinfo/sox-devel
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220611021715.GA16306@dcvr \
--to=sox-devel@lists.sourceforge.net \
--cc=mans@mansr.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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/sox.git
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).