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

Gabriel Becker gmbecker at ucdavis.edu
Thu Dec 14 20:56:23 CET 2017


Gabor,

You can grab the connection and destroy it via getConnection and then a
standard close call. (it actually lists that it is "closed" already, but
still in the set of existing connections. I can't speak to that difference).

> tryCatch(

+   readLines(tempfile(), warn = FALSE)[1],

+   error = function(e) NA,

+   warning = function(w) NA

+ )

[1] NA

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

> gc()

         used (Mb) gc trigger (Mb) max used (Mb)

Ncells 257895 13.8     592000 31.7   416371 22.3

Vcells 536411  4.1    8388608 64.0  1795667 13.7

>

> showConnections(all = TRUE)

  description


0 "stdin"


1 "stdout"


2 "stderr"


3
"/var/folders/79/l_n_5qr152d2d9d9xs0591lh0000gn/T//RtmpZRcxmh/file128a13bffc77"

  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"

> con = getConnection(3)

> con

A connection with


description
"/var/folders/79/l_n_5qr152d2d9d9xs0591lh0000gn/T//RtmpZRcxmh/file128a13bffc77"

class       "file"


mode        "r"


text        "text"


opened      "closed"


can read    "yes"


can write   "yes"


> close(con)

> showConnections(all=TRUE)

  description class      mode text   isopen   can read can write

0 "stdin"     "terminal" "r"  "text" "opened" "yes"    "no"

1 "stdout"    "terminal" "w"  "text" "opened" "no"     "yes"

2 "stderr"    "terminal" "w"  "text" "opened" "no"     "yes"


HTH,
~G

On Thu, Dec 14, 2017 at 10:02 AM, Gábor Csárdi <csardi.gabor at gmail.com>
wrote:

> 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
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel




-- 
Gabriel Becker, PhD
Scientist (Bioinformatics)
Genentech Research

	[[alternative HTML version deleted]]



More information about the R-devel mailing list