[Rd] cannot destroy connection (?) created by readLines in a tryCatch

Gábor Csárdi csardi.gabor at gmail.com
Thu Dec 14 19:02:18 CET 2017

Consider this code. This is R 3.4.2, but based on a quick look at the
NEWS, this has not been fixed.

  readLines(tempfile(), warn = FALSE)[1],
  error = function(e) NA,
  warning = function(w) NA

rm(list=ls(all.names = TRUE))

showConnections(all = TRUE)

If you run it, you'll get a connection you cannot close(), i.e. the
last showConnections() call prints:

❯ showConnections(all = TRUE)
0 "stdin"
1 "stdout"
2 "stderr"
3 "/var/folders/59/0gkmw1yj2w7bf2dfc3jznv5w0000gn/T//Rtmpc7JqVS/filecc2044b2ccec"
  class      mode text   isopen   can read can write
0 "terminal" "r"  "text" "opened" "yes"    "no"
1 "terminal" "w"  "text" "opened" "no"     "yes"
2 "terminal" "w"  "text" "opened" "no"     "yes"
3 "file"     "r"  "text" "closed" "yes"    "yes"

AFAICT, readLines should close the connection:

❯ readLines
function (con = stdin(), n = -1L, ok = TRUE, warn = TRUE, encoding = "unknown",
    skipNul = FALSE)
    if (is.character(con)) {
        con <- file(con, "r")
    .Internal(readLines(con, n, ok, warn, encoding, skipNul))
<environment: namespace:base>

so maybe this just a symptom of an on.exit() issue?

Or am I missing something and it is possible to close the connection?


More information about the R-devel mailing list