Bug #20611
closedRuby 3.3: Tempfile#size returns 0 when file is not empty
Description
Hi
I searched to see if this was reported already, or if it was somehow intentional but I'm having an issue with Ruby 3.3.3. I run under rbenv 2.7.6 on amd64 linux (debian 6.6.13-1 kernel). For me, ruby 3.3.3 exhibits some oddness with the Tempfile class whereas Ruby 3.2 and older does not. I have only tested ruby 3.3.3 not other 3.3.x releases:
ff = Tempfile.new('foo'); puts "Size now: #{ff.size}"; File.open(ff.path, "a").tap {|f|| f.write('moredata'); f.close } ; puts "Finish size: #{ff.size} vs actual: #{File.size(ff.path)}"
Size now: 0
Finish size: 0 vs actual: 8
As you can see, ff.size should have returned 8. If i try the same code on ruby 3.2.2 for example it works as expected:
ff = Tempfile.new('foo'); puts "Size now: #{ff.size}"; File.open(ff.path, "a").tap {|f| f.write('moredata'); f.close } ; ff.size; puts "Finish size: #{ff.size} vs actual: #{File.size(ff.path)}"
Size now: 0
Finish size: 8 vs actual: 8
Updated by jbw (Jerry W) about 1 year ago
- Subject changed from Ruby 3.3: Tempfile.size returns 0 to Ruby 3.3: Tempfile#size returns 0 when file is not 0
Updated by jbw (Jerry W) about 1 year ago
- Subject changed from Ruby 3.3: Tempfile#size returns 0 when file is not 0 to Ruby 3.3: Tempfile#size returns 0 when file is not empty
Updated by jbw (Jerry W) about 1 year ago
Sorry there is a double pipe on the tap call of the first code block - that was a mis-paste by me & I'm unable to edit to correct it.
The code I used was the same everywhere (but no double || :-) )::
ff = Tempfile.new('foo'); puts "Size now: #{ff.size}"; File.open(ff.path, "a").tap {|f| f.write('moredata'); f.close } ; ff.size; puts "Finish size: #{ff.size} vs actual: #{File.size(ff.path)}"
Updated by jbw (Jerry W) about 1 year ago
I just realised now that Tempfile is atually a separate gem. Looking there I can see that the size method is using its internal buffer length if the file is not closed, otherwise it falls back on File.size. Now it's become clear to me that the reason I'm seeing this bug is because my Tempfile too must be modified outside of its instance and this is causing this bug to be manifested on modern rubies. You can close this, sorry for wasting your time
Updated by jeremyevans0 (Jeremy Evans) about 1 year ago
- Status changed from Open to Closed