From csardi.gabor at gmail.com  Thu Dec 14 19:02:18 2017
From: csardi.gabor at gmail.com (=?UTF-8?B?R8OhYm9yIENzw6FyZGk=?=)
Date: Thu, 14 Dec 2017 18:02:18 +0000
Subject: [Rd] cannot destroy connection (?) created by readLines in a
	tryCatch
Message-ID: <CABtg=KnWVVSvq6dogNG9fnYWhTJNNadJcyaMDif0_EV7Gd_VaA@mail.gmail.com>

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

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

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

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)
  description
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")
        on.exit(close(con))
    }
    .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?

Thanks,
Gabor



[Rd] cannot destroy connection (?) created by readLines in a tryCatch
=?UTF-8?B?R8OhYm9yIENzw6FyZGk=?=