From ripley@stats.ox.ac.uk  Thu Feb 28 18:00:00 2002
From: ripley@stats.ox.ac.uk (ripley@stats.ox.ac.uk)
Date: Thu, 28 Feb 2002 19:00:00 +0100 (MET)
Subject: [Rd] Failed file("foo", "r") keeps file open? (PR#1333)
Message-ID: <200202281800.TAA09556@pubhealth.ku.dk>

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.

It's better to call file() and then open(), which will avoid the problem.


> 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


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] Failed file("foo", "r") keeps file open? (PR#1333)
ripley@stats.ox.ac.uk