[Rd] Enhancement request: anonymous connections
Prof Brian Ripley
ripley at stats.ox.ac.uk
Sun Jan 1 19:05:40 CET 2006
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.
>> 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.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-devel
mailing list