Created attachment 1856 [details]
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.
Fixed in R-devel and will be ported to 3.2.2 patched.
Thanks for the report and analysis.
For the record, the change in the patch did break some uses of capture.output(), and has been confined to pipe() connections.