[Rd] Enhancement request: anonymous connections

Duncan Murdoch murdoch at stats.uwo.ca
Sun Jan 1 19:20:31 CET 2006


On 1/1/2006 1:05 PM, Prof Brian Ripley wrote:
> On Sun, 1 Jan 2006, Duncan Murdoch wrote:
> 
> 
>>On 12/28/2005 9:50 AM, Seth Falcon wrote:
>>
>>>On 27 Dec 2005, murdoch at stats.uwo.ca wrote:
>>>
>>>
>>>>This is a bug in load, isn't it?  load() opens the connection but
>>>>doesn't close it.
>>>
>>>
>>>Well, it may be that load needs a small fix, but that doesn't fix
>>>anonymous connections in general, IMO.
>>
>>No it doesn't.  However, I've committed the small fix.
> 
> 
> It was not even a bug in load: close() and open() are not pairs.  (I 
> didn't pick the names!) Your `fix' destroys a connection, which is 
> not the documented behaviour and far more dangerous than leaving it open.
> 
> The lifecycle of a connection is  (see e.g. my R-news article)
> 
>  	create->open->close->destroy
> 
> and close() does both of the last two.  Please revert this change. 
> Ideally we would close and not destroy if the connection was opened, but 
> that needs a better C-level interface in place of this R-level one.

Sorry about that.  I've done the reversion.

Is it worth putting that C-level interface in place to make load() more 
compatible with ?connections which says,

      'open' opens a connection.  In general functions using connections
      will open them if they are not open, but then close them again, so
      to leave a connection open call 'open' explicitly.

?  I suppose a natural way to do it would be to add a "destroy=TRUE"
argument to close(), and then have load() do

  on.exit(close(con, destroy=FALSE))

but maybe it would be better to add a separate function to do this, for 
better green book compatibility.

Duncan Murdoch
> 
> 
>>>The loop could easily have been:
>>>
>>>for (i in 1:50) {
>>>    print(load(url(testUrl, open="r")))
>>>}
>>>
>>>And it doesn't need to be related to url or load:
>>>
>>>cat("a line of text\n", file="another-example.txt")
>>>z <- NULL
>>>for (i in 1:50) {
>>>    z <- c(z, readLines(file("another-example.txt", open="r")))
>>>}
>>>
>>>Also, connections are "in use" even if they are closed:
>>>
>>>for (i in 1:50) {
>>>    if (isOpen(file("another-example.txt")))
>>>        stop("you will not get here")
>>>}
>>
>>I think the general problem is that R doesn't have references (or at
>>least, they aren't in a final, documented state).  If the garbage
>>collector closed a connection, then things would go wrong when there
>>were two copies of it:  the second one would be messed up when the first
>>was destroyed.  If we had references, then opening a connection could
>>create a connection object and a reference to it; the connection object
>>would remain as long as there were any references to it, and could be
>>destroyed (and automatically closed) after the last reference was gone.
> 
> 
> However, that just isn't how connections are documented in the Green Book 
> (referenced on all the relevant help pages, so required reading) and 
> getConnection() allows you to create an R object pointing to a connection 
> that previously had none.  The OP has never told us what `anonymous 
> connections' are, but it is quite possible that his unstated ideas are 
> incompatible with the documentation.
>



More information about the R-devel mailing list