Bug 16481 - Closing a pipe doesn't reliably return exit status
Summary: Closing a pipe doesn't reliably return exit status
Alias: None
Product: R
Classification: Unclassified
Component: I/O (show other bugs)
Version: R 3.2.1
Hardware: x86_64/x64/amd64 (64-bit) Linux
: P5 minor
Assignee: R-core
Depends on:
Reported: 2015-07-21 20:53 UTC by msperlich
Modified: 2015-08-13 06:21 UTC (History)
0 users

See Also:

Possible fix (444 bytes, patch)
2015-07-21 20:53 UTC, msperlich
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description msperlich 2015-07-21 20:53:26 UTC
Created attachment 1856 [details]
Possible fix

Calling close() on a pipe should return the exit status of the underlying program, but returns null when the pipe was explicitly opened in user code. For example:

> con <- pipe("ls /", open="rt")
> data <- readLines(con)
> return_code <- close(con)

gives return_code as NULL. Excluding the open parameter gives the expected behavior in this particular instance, but there are some cases where it seems to be impossible to read the exit status (for exampling writing to a pipe with write.table).

I believe this can be traced back to do_close() in connections.c line 3374, where the status code is being read before the connection has been closed (and thus is not set).

The attached patch makes the example above work as expected for me. All tests passed locally, but I'm not familiar enough with R to know if it could introduce bugs in other instances.
Comment 1 Brian Ripley 2015-08-12 10:41:41 UTC
Fixed in R-devel and will be ported to 3.2.2 patched.

Thanks for the report and analysis.
Comment 2 Brian Ripley 2015-08-13 06:21:29 UTC
For the record, the change in the patch did break some uses of capture.output(), and has been confined to pipe() connections.