Re: [Rd] Possible changes to connections

From: Bill Dunlap <bill_at_insightful.com>
Date: Thu, 31 May 2007 17:52:51 -0700 (PDT)

On Thu, 31 May 2007, Seth Falcon wrote:

> > When I ran some tests I found 7 packages on CRAN that in their tests
> > were not closing connections.  Four of those are maintained by R-core
> > members.
> > Even though none were by me, I think this is too easy to forget to
> > do!
>
> I agree that it is easy to forget.  It is especially easy if one
> creates so-called anonymous connection references like
> readLines(file(path)) -- this anonymous idiom seems nature to me when
> coding R and it would be nice to make it work for connections.

I like the idea of the connection being closed when there are no more references to it. I guess that means when the garbage collector notices it has been orphaned, which may take a while.

However, one of my longstanding complaints about connections in Splus and R may have a bearing here also. Currently, if you want to have your file opened in a particular way, say for only reading or for appending or in binary mode then you need to specify open=mode when calling file(). However that also tells it to actually open the file. I would prefer that there was a mode= argument to file that meant that when the file is eventually opened it would be opened with that mode. open= should be restricted to TRUE or FALSE, or IMO, be eliminated. (We have an open() function for that.) With the current system

   readLines(file(path))
does not leave path open but

   readLines(file(path, "r"))
does leave it open. E.g., using readLines(file(path)) as Seth did appears to work fine:
> system(paste("/usr/sbin/lsof -p", Sys.getpid(),"|grep /tmp/twolines.txt"))
> readLines(file("/tmp/twolines.txt"))

  [1] "One,"                 "two, and that is it."

> system(paste("/usr/sbin/lsof -p", Sys.getpid(),"|grep /tmp/twolines.txt"))
> # no lsof output means the file is not open
but asking to have it opened in readonly and binary mode leaks a file descriptor:
> system(paste("/usr/sbin/lsof -p", Sys.getpid(),"|grep /tmp/twolines.txt"))
> readLines(file("/tmp/twolines.txt", open="rb"))
  [1] "One,"                 "two, and that is it."

> system(paste("/usr/sbin/lsof -p", Sys.getpid(),"|grep /tmp/twolines.txt"))
R 16950 bill 3r REG 8,2 26 229597 /tmp/twolines.txt
That difference sinces unnatural to me.

Of course, we could just add the mode= argument and hope people started using it instead of open=.



Bill Dunlap
Insightful Corporation
bill at insightful dot com
360-428-8146

 "All statements in this message represent the opinions of the author and do  not necessarily reflect Insightful Corporation policy or position."



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri 01 Jun 2007 - 00:56:38 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 01 Jun 2007 - 09:34:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.