File.size (org.jruby.RubyFile.size() ) impacts underlying file handle #1272

Closed
jayjlawrence opened this Issue Nov 26, 2013 · 4 comments

Projects

None yet

2 participants

@jayjlawrence

Using Jruby 1.7.8 deployed in a Tomcat7 container (warbled war) - I have created a demonstration project

I have not recreated the issue but have isolated and observed it in my application code.

Using the "spreadsheet" gem in Windows relies on the Ruby-OLE gem. This gem was returning a IOError exception on a @io.rewind statement. @io is a File object.

Bisecting the issue I have narrowed it down to immediately prior to the @io.rewind is a @io.size > 0 test. If this is changed to @io.stat.size > 0 then the code proceeds. This suggests that the RubyFile size method irritates an (probably Windows) issue that damages the underlying file handle.

How do you feel about using the same approach as with "ctime" or "atime"? i.e. context.runtime.newFileStat vs runtime.getPosix().fstat ... just a thought.

I have created a project that demonstrates is issue at https://github.com/jayjlawrence/jruby-File.size

System stats:
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

Windows 2008 R2 64 bit in a VMware VM

@jayjlawrence

Edited the original report - note that I have created a project that exposes this issue at https://github.com/jayjlawrence/jruby-File.size

@enebo
Member
enebo commented Nov 27, 2013

Ouch I can see what is wrong....

https://github.com/jnr/jnr-posix/blob/master/src/main/java/jnr/posix/WindowsPOSIX.java#L282

((WindowsLibC) libc())._close(fd);

This is getting called unconditionally and is closing the same descriptor that win32ole impl must be using. Another wrinkle in this is that I cannot reproduce #770 which is using the same method on 1.7 branch currently so fixing the close issue most likely might still mean there is something wacky with this impl. We enabled real native errors a while back for syscalls on windows and keep getting really weird exceptions back for uncommon values. Enough rambling. I at least know your core issue...

@enebo enebo added a commit that closed this issue Nov 27, 2013
@enebo enebo Fixes #1272 and hopefully #770 91097da
@enebo enebo closed this in 91097da Nov 27, 2013
@enebo
Member
enebo commented Nov 27, 2013

So I took your advice and added a windows path to impl size like we do atime/ctime, but this is non-optimal and so I added a FIXME. The truth is our fstat impl in jnr-posix is broken in two ways:

  1. It is osf_close'ing the native handle and this is closing the win32ole use. We should not be doing this
  2. various paths which do not throw exceptions in other platforms return weird ones on windows. like a path of "" (which won't happen in this case but hey it is just an example) will return very weird error on windows if you stat it.
@jayjlawrence jayjlawrence added a commit to jayjlawrence/jruby that referenced this issue Dec 4, 2013
@enebo @jayjlawrence enebo + jayjlawrence Fixes #1272 and hopefully #770 ced5edc
@jayjlawrence

An update to confirm that the fix appears to work. I pulled the commit into 1.7.8 and ran code that uses filehandle.size quite extensively. THANKS for the amazing turnaround on this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment