IO.copy_stream leaves opened files in ObjectSpace #3590

Closed
scarfacedeb opened this Issue Jan 10, 2016 · 3 comments

Comments

Projects
None yet
3 participants
@scarfacedeb

It seems to me that JRuby deviates from MRI implementation of IO.copy_stream:

To demonstrate run the following snippet in MRI and jruby:

puts ObjectSpace.each_object(File).reject(&:closed?).count # == 1
IO.copy_stream "README.md", "/tmp/readme"
puts ObjectSpace.each_object(File).reject(&:closed?).count # == 1 in MRI, == 3 in jruby

In MRI IO.copy_stream "README.md", "/tmp/readme" doesn't leave opened files in the ObjectSpace.

But in jruby, ObjectSpace.each_object(File).reject(&:closed?) would include 2 extra implicitly created file descriptors from IO.copy_stream.

It is an expected behaviour?

Relevant refile/refile issue: refile/refile#414

@headius

This comment has been minimized.

Show comment
Hide comment
@headius

headius Jan 21, 2016

Member

Weird. And even weirder that you have a related issue.

Member

headius commented Jan 21, 2016

Weird. And even weirder that you have a related issue.

@headius

This comment has been minimized.

Show comment
Hide comment
@headius

headius Jan 21, 2016

Member

Tell ya what...I'll at least make sure that streams opened by IO#copy_stream get closed when it's done. They would close due to finalization, so this wasn't really a leak...but it was bad form.

Member

headius commented Jan 21, 2016

Tell ya what...I'll at least make sure that streams opened by IO#copy_stream get closed when it's done. They would close due to finalization, so this wasn't really a leak...but it was bad form.

@headius headius closed this in d7aefef Jan 21, 2016

@headius headius added this to the JRuby 9.0.5.0 milestone Jan 21, 2016

@jowl

This comment has been minimized.

Show comment
Hide comment
@jowl

jowl Mar 31, 2016

Contributor

FYI: This also solves another issue I stumbled upon using JRuby 1.7.19, which was that the RubyIO instances could be GC'd during #copy_stream, since they weren't referenced after getting their channels, which caused their finalizers to run and close them. This error appeared as

Errno::EBADF: Bad file descriptor - Bad file descriptor
  copy_stream at org/jruby/RubyIO.java:4694
Contributor

jowl commented Mar 31, 2016

FYI: This also solves another issue I stumbled upon using JRuby 1.7.19, which was that the RubyIO instances could be GC'd during #copy_stream, since they weren't referenced after getting their channels, which caused their finalizers to run and close them. This error appeared as

Errno::EBADF: Bad file descriptor - Bad file descriptor
  copy_stream at org/jruby/RubyIO.java:4694
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment