From luke-tierney at uiowa.edu  Thu Dec 14 23:05:40 2017
From: luke-tierney at uiowa.edu (Tierney, Luke)
Date: Thu, 14 Dec 2017 22:05:40 +0000
Subject: [Rd] cannot destroy connection (?) created by readLines in
	a	tryCatch
In-Reply-To: <CADwqtCOm8R4E827krkM5Vg+_AwB190tOXeh3w+mxSxu=aBWcCA@mail.gmail.com>
References: <CABtg=KnWVVSvq6dogNG9fnYWhTJNNadJcyaMDif0_EV7Gd_VaA@mail.gmail.com>
 <CADwqtCPwpURQqLs0NrX7Hg9CtEX4huLj+Z=DTKvzTb9bHX=vbA@mail.gmail.com>
 <CABtg=KnTE+5n-4wQgty0yMVr1_vuUhvYgJ2fJGux143Pz-T0bQ@mail.gmail.com>,
 <CADwqtCOm8R4E827krkM5Vg+_AwB190tOXeh3w+mxSxu=aBWcCA@mail.gmail.com>
Message-ID: <5889CB5D-2F32-425F-A072-7100FC767E90@uiowa.edu>

Your guess is wrong. More when I have a sensible keyboard

Sent from my iPhone

On Dec 15, 2017, at 10:21 AM, Gabriel Becker <gmbecker at ucdavis.edu<mailto:gmbecker at ucdavis.edu>> wrote:

On Thu, Dec 14, 2017 at 12:17 PM, G?bor Cs?rdi <csardi.gabor at gmail.com<mailto:csardi.gabor at gmail.com>>
wrote:

On Thu, Dec 14, 2017 at 7:56 PM, Gabriel Becker <gmbecker at ucdavis.edu<mailto:gmbecker at ucdavis.edu>>
wrote:
Gabor,

You can grab the connection and destroy it via getConnection and then a
standard close call.

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 seems to be the  setting of a warning handler in tryCatch that does it
actually; without that, it works as expected, even when errors are caught.

tryCatch(readLines(tempfile(), warn=FALSE), error=function(x) NA)

[1] NA

*Warning message:*

*In file(con, "r") :*

*  cannot open file
'/var/folders/79/l_n_5qr152d2d9d9xs0591lh0000gn/T//RtmpzIZ6Qh/file1ed2e57f2ea':
No such file or directory*

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"

tryCatch(readLines(tempfile(), warn=FALSE), warning=function(x) NA)

[1] NA

showConnections(all=TRUE)

 description


0 "stdin"


1 "stdout"


2 "stderr"


3
"/var/folders/79/l_n_5qr152d2d9d9xs0591lh0000gn/T//RtmpzIZ6Qh/file1ed2300ce801"

 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"


~G



(it actually lists that it is "closed" already, but
still in the set of existing connections. I can't speak to that
difference).

It is closed but not destroyed.

G.

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




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




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

   [[alternative HTML version deleted]]

______________________________________________
R-devel at r-project.org<mailto:R-devel at r-project.org> mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

	[[alternative HTML version deleted]]



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