[Rd] Failed file("foo", "r") keeps file open? (PR#1333)
hb@maths.lth.se
hb@maths.lth.se
Thu, 28 Feb 2002 20:16:08 +0100 (MET)
On Thu, 28 Feb 2002, Prof Brian Ripley wrote:
> On Thu, 28 Feb 2002 hb@maths.lth.se wrote:
>
> > Full_Name: Henrik Bengtsson
> > Version: R v1.4.0
> > OS: Solaris 8
> > Submission from: (NULL) (130.235.3.49)
> >
> >
> > When I am trying the following on Rv1.4.0 on Solaris 8, I get that the
> > "cannot close output sink connection":
> >
> > % R --vanilla
> >
> > tmpfile <- tempfile() # Create a filename that does not exist
> > for(k in 1:100) {
> > cat(k, ": ");
> > try(fh <- file(tmpfile, "r"))
> > close(file("tmp.txt", "w")) # Opens and closes a file for writing.
> > }
> >
> > 1 Error in file(tempfile(), "r") : cannot open file `/tmp/fileR70d5S167e'
> > 2 Error in file(tempfile(), "r") : cannot open file `/tmp/fileR70d5S2781'
> > ...
> > 47 Error in file(tempfile(), "r") : cannot open file `/tmp/fileR70d5S345b'
> > Error in close.connection(file("tmp.txt", "w")) :
> > cannot close output sink connection
> >
> > It always happens after 48 failed file openings.
> >
> >
> > To me it looks like failed 'file(tmpfile, "r")' does not correctly
> > clean up after itself. And since 'fh' is never assigned it can neither
> > be solved by a 'close(fh)'.
>
> It does correctly clean up: it should leave a connection which is not
> opened. That may not be the best design, but it is the design.
>
> Take a look at closeAllConnections.
Accepting the design, calling file(tempfile(), open="r"), will, as
you say, create a "closed" connection as seen by
showConnections(all=TRUE) and it can be deleted by identifying the
connection id from the list and the closing it with
fh <- getConnection(id)
close(fh)
or one could call closeAllConnections(), which is not always what you
want. However, if one creates TOO MANY failed connections as above,
neither of the above works:
% R --vanilla
K <- 48; # K <- 47 is ok.
tmpfile <- tempfile()
for(k in 1:K) try(fh <- file(tmpfile, "r"))
will make [R] stall when closeAllConnections() is called. Doing Ctrl+C and
trying do anything, e.g. "ls() [ENTER]", will generate a "Segmentation
fault" or "0 [ENTER]" will generate a "Bus error". Trying to use
fh <- getConnection(id)
close(fh)
gives "Error in close.connection(fh) : cannot close output sink
connection". In this case there is not fatal quitting of [R].
I don't think it is a common thing to end up in this situation, but even
with the design you refer to, I still think something should be fixed
since it is possible to force [R] into an instable and unleavable state,
which is likely to crash [R].
> It's better to call file() and then open(), which will avoid the problem.
I definitely agree, now when you have told me.
> > FYI: Doing a cat(file="tmp.txt", k") instead of the close(file(...)) call,
> > will produce a "sink stack is full" error.
> >
> > FYI2: After the failure above, trying to call sink() will result in a
> > "Segmentation fault" and [R] is stopped.
> >
> > Background: I ran into this problem after have been running the same
> > script several times over several days I eventually ran into the problem.
> > It was hard to identify the problem, but I believe the above example shows
> > the core of it and I could not track the problem further.
> >
> > Unfortunately, I do not have access to R v1.4.1 for Solaris 8.
> >
> >
> > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> > r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> > Send "info", "help", or "[un]subscribe"
> > (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch
> > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> >
>
> --
> Brian D. Ripley, ripley@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 272860 (secr)
> Oxford OX1 3TG, UK Fax: +44 1865 272595
Thanks
Henrik Bengtsson
Dept. of Mathematical Statistics @ Centre for Mathematical Sciences
Lund Institute of Technology/Lund University, Sweden (+2h UTC)
Office: P316, +46 46 222 9611 (phone), +46 46 222 4623 (fax)
h b @ m a t h s . l t h . s e, http://www.maths.lth.se/bioinformatics/
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._