Date: Fri, 29 May 2015 11:08:28 -0400 From: Rich Felker <dalias@...c.org> To: musl@...ts.openwall.com Subject: cancellable stdio functions - broken and unfixable? 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
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.