mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: cancellable stdio functions - broken and unfixable?
Date: Fri, 29 May 2015 11:08:28 -0400	[thread overview]
Message-ID: <20150529150828.GJ17573@brightrain.aerifal.cx> (raw)

I made certain stdio operations cancellation points back in 2012 as
commit 58165923890865a6ac042fafce13f440ee986fd9, on the basis that
POSIX allows them (as optional ones) and that it's a useful feature to
have. But on further examination, it seems to be broken and unfixable
-- the requirements simply don't admit a valid implementation.

The issue is this: on cancellation, the side effects of the
cancellable function must be as if the function failed with EINTR.
That means only the following cases are possible:

1. No data is read/written and the function acts on cancellation.

2. Data is read/written and it returns success without acting on
   cancellation.

3. An error is returned without acting on cancellation.

For the plain fd-based read/write operations, satisfying this contract
is simple because they're allowed to perform short reads/writes and
the caller has to retry. If cancellation is received during an
operation that has already transferred some data, the operation
returns success immediately and cancellation will be acted on at the
next cancellation point, after the application gets to see how much
data was transferred already.

With stdio, however, short reads/writes are not valid. All stdio
functions behave as if by repeat calls to fgetc/fputc, and must repeat
until their termination condition (number of bytes, reaching a newline
or null terminator, or whatever) is met or an error occurs. This makes
a major problem for cancellation -- it cancellation is received after
some data is transferred, the function cannot act on cancellation, but
it also cannot immediately return. Instead it has to keep looping but
with cancellation suppressed for the remainder of the operation, which
could still block indefinitely.

We can implement the above, but I'm not sure if it's useful. There
would not be many opportunities for cancellation; to use it reliably,
you'd need to stick to pure fgetc/fputc, no higher-level calls. The
alternative is just removing the feature entirely (and thereby getting
moderately faster stdio performance on x86 by virtue of being able to
use sysenter/syscall rather than int $128).

Oh, and in case anyone's wondering, the current behavior just acts on
cancellation even when it's wrong to do so and causes the caller to
lose information about how much data was transferred... :(

Rich


                 reply	other threads:[~2015-05-29 15: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=20150529150828.GJ17573@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.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://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).