Bug #2992


File operations leak file handles in Windows XP

Added by phiggins (Pete Higgins) over 12 years ago. Updated about 11 years ago.

Target version:
ruby -v:
ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-mingw32]


Various operations on files leak file handles in Windows XP. In the two attached scripts, Task Manager will report ~30 open file handles for the ruby process at the first gets, then ~230 at the second gets.

This has only been reproducible in Windows XP. This behavior is not seen in Windows Vista or 7.

More discussion of this issue occurred on the RubyInstaller mailing list in this thread:


test_md5.rb (272 Bytes) test_md5.rb phiggins (Pete Higgins), 03/21/2010 05:34 AM
test_file_read.rb (415 Bytes) test_file_read.rb phiggins (Pete Higgins), 03/21/2010 05:34 AM
Actions #1

Updated by phasis68 (Heesob Park) over 12 years ago

After some inspection, I found this leakage is not file handle but event handle.
When ReadFile fails with EOF, it returns without closing event handle.

Here is a patch for trunck verion ruby 1.9.2dev (2010-03-24 trunk 27027)

--- win32.c 2010-03-24 11:24:43.149695643 +0900
+++ 2010-03-24 11:26:44.522700645 +0900
@@ -4887,6 +4887,7 @@
if (!ReadFile((HANDLE)_osfhnd(fd), buf, size, &read, pol)) {
err = GetLastError();
if (err != ERROR_IO_PENDING) {

  •       if(pol) CloseHandle(ol.hEvent);
          if (err == ERROR_ACCESS_DENIED)
              errno = EBADF;
          else if (err == ERROR_BROKEN_PIPE || err == ERROR_HANDLE_EOF) {


Actions #2

Updated by yugui (Yuki Sonoda) over 12 years ago

  • Assignee set to usa (Usaku NAKAMURA)



Actions #3

Updated by yugui (Yuki Sonoda) over 12 years ago

  • Target version set to 1.9.2



Actions #4

Updated by nobu (Nobuyoshi Nakada) over 12 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r27042.
Pete, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.



Also available in: Atom PDF