From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/7822 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: cancellable stdio functions - broken and unfixable? Date: Fri, 29 May 2015 11:08:28 -0400 Message-ID: <20150529150828.GJ17573@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1432912181 24566 80.91.229.3 (29 May 2015 15:09:41 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 29 May 2015 15:09:41 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-7834-gllmg-musl=m.gmane.org@lists.openwall.com Fri May 29 17:09:26 2015 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1YyLuV-0002e6-9V for gllmg-musl@m.gmane.org; Fri, 29 May 2015 17:09:23 +0200 Original-Received: (qmail 7426 invoked by uid 550); 29 May 2015 15:08:47 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 6011 invoked from network); 29 May 2015 15:08:40 -0000 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:7822 Archived-At: 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