Skip to content
Prev 54447 / 63424 Next

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

On Thu, Dec 14, 2017 at 7:56 PM, Gabriel Becker <gmbecker at ucdavis.edu> wrote:
Yeah, that's often a possible workaround, but since this connection
was opened by
readLines() internally, I don't necessarily know which one it is. E.g.
I might open multiple
connections to the same file, so I can't choose based on the file name.

Btw. this workaround seems to work for me:

read_lines <- function(con, ...) {
  if (is.character(con)) {
    con <- file(con)
    on.exit(close(con))
  }
  readLines(con, ...)
}

This is basically the same as readLines(), but on.exit() does its job here.
That's another clue that it might be an on.exit() issue. Wild guess:
on.exit() does not run if an internal function errors.
It is closed but not destroyed.

G.