Leaving files open


#1

hello list!
i’ve learnt to open files like this:

File.open(“foo”, “w”) do |file|
file.write “foo”
end

because everyone always says it’s dangerous to leave files open…
just, wondering, what could really happen?

suppose i write a program that write to a file, infinitely, i’ll want to
shut this program off after a while, the file will not properly close…
what could actually happen?

greetings, Dirk.


#2

“D” == Dirk M. removed_email_address@domain.invalid writes:

D> suppose i write a program that write to a file, infinitely, i’ll want
to
D> shut this program off after a while, the file will not properly
close…
D> what could actually happen?

You can lost data

moulon% ls -l xxx
ls: xxx: No such file or directory
moulon%

moulon% ruby -e ‘f = File.open(“xxx”, “w”); f.write(“xxx”); exit!’
moulon%

moulon% ls -l xxx
-rw-r–r-- 1 decoux msys 0 2006-06-06 15:49 xxx
moulon%

Guy Decoux


#3

2006/6/6, Dirk M. removed_email_address@domain.invalid:

suppose i write a program that write to a file, infinitely, i’ll want to
shut this program off after a while, the file will not properly close…
what could actually happen?

With buffering IO your changes might not be written to the file (this
might depend on OS and the way your process terminates). Also, if you
open the same file multiple times from the same process you might also
see weird effects. Lastly, keeping a file descriptor open for longer
than needed may cause you troubles if you open many files, because
tehre is a limit on the max number of open file descriptors.

Note that in your scenario you can alternatively do the closing in
at_exit or END block depending on how you architect your software. If
the sole task of the software is to write to a single file then I’d
probably still rather use the block approach and have your main
program inside that block.

Kind regards

robert


#4

On Jun 6, 2006, at 9:45 AM, Dirk M. wrote:

suppose i write a program that write to a file, infinitely, i’ll
want to
shut this program off after a while, the file will not properly
close…
what could actually happen?

greetings, Dirk.

No expert here, and I’m sure it depends on the OS. But I think the
biggest danger you face is loosing some of the data you wrote to the
file. It’s possible that some data is in a buffer and hasn’t
actually been written when the program exits. Closing a file writes
the buffer, so not closing it and just exiting may leave buffer data
unwritten.
-Mat


#5

On Jun 6, 2006, at 8:53 AM, Mat S. wrote:

But I think the biggest danger you face is loosing some of the data
you wrote to the file. It’s possible that some data is in a buffer
and hasn’t actually been written when the program exits. Closing a
file writes the buffer, so not closing it and just exiting may
leave buffer data unwritten.

All files should be closed on exit, assuming exit!() wasn’t called.

James Edward G. II


#6

On Jun 6, 2006, at 9:45 AM, Dirk M. wrote:

suppose i write a program that write to a file, infinitely, i’ll
want to
shut this program off after a while, the file will not properly
close…
what could actually happen?

greetings, Dirk.

The major issue with not closing files on *Nix is that you could
run out of file descriptors for that process. The OS will close the
file at program termination, but you can and should use hooks like
at_exit{} and END {} to make sure (If you need to keep one file open
indefinitely). STDOUT for instance is a file, you don’t see most
people closing that.


#7

On 6/6/06, James Edward G. II removed_email_address@domain.invalid wrote:

On Jun 6, 2006, at 8:53 AM, Mat S. wrote:

But I think the biggest danger you face is loosing some of the data
you wrote to the file. It’s possible that some data is in a buffer
and hasn’t actually been written when the program exits. Closing a
file writes the buffer, so not closing it and just exiting may
leave buffer data unwritten.

FWIW, kill -KILL and program crashes loses written but unsynced data.