From: "H.Yamamoto" Date: 2005-07-25T11:00:27+09:00 Subject: Re: [ ruby-Bugs-2131 ] ruby ( v183) bcc32: using Socket.new with timeout -> files not closed Hi. >That result was INCORRECT. > (ISP maintenance when I ran that script yesterday) >Sorry for the confusion. > >This is the result after the win32 'setmode' patch: > >["Net::HTTP", "1.126", "1.1"] >D:/RUBY/SRC_CVSINST/lib/ruby/1.9/net/protocol.rb:70:in `close': Bad file number (Errno::EBADF) Sorry, setmode() accepted only text mode flags (O_TEXT, O_BINARY). That magic number was simply ignored. I installed win98 onto another HDD and tried by myself. Yes, definitly fails with EBADF. (This is not ZoneAlarm's fault) I needed this patch to compile ruby-bccwin32 on win98. "copy" => "xcopy" is needed because "copy" cannot handle the path terminated with \. I don't know how to remove it, so I used xcopy instead. Index: bcc32/Makefile.sub =================================================================== RCS file: /src/ruby/bcc32/Makefile.sub,v retrieving revision 1.79 diff -u -w -b -p -r1.79 Makefile.sub --- bcc32/Makefile.sub 1 Jun 2005 02:35:35 -0000 1.79 +++ bcc32/Makefile.sub 24 Jul 2005 11:35:04 -0000 @@ -285,7 +285,7 @@ $(CONFIG_H): $(MKFILES) $(srcdir)bcc32/M \#define RUBY_ARCHLIB "/lib/ruby/$(MAJOR).$(MINOR)/$(ARCH)-$(OS)" \#define RUBY_SITE_ARCHLIB "/lib/ruby/site_ruby/$(MAJOR).$(MINOR)/$(ARCH)-$(OS)" | - @exit > $@ + @exit > $(@:/=\) config.status: $(MKFILES) $(srcdir)bcc32/Makefile.sub $(srcdir)common.mk @echo Creating $@ @@ -340,8 +340,8 @@ s,@AR@,$(AR),;t t s,@ARFLAGS@,$(ARFLAGS) ,;t t s,@LN_S@,$(LN_S),;t t s,@SET_MAKE@,$(SET_MAKE),;t t -s,@CP@,copy > nul,;t t -s,@INSTALL@,copy > nul,;t t +s,@CP@,xcopy > nul,;t t +s,@INSTALL@,xcopy > nul,;t t s,@INSTALL_PROG@,$$(INSTALL),;t t s,@INSTALL_DATA@,$$(INSTALL),;t t s,@LIBOBJS@, acosh.obj crypt.obj erf.obj win32.obj,;t t And another patch. By this patch, your sample code worked fine. # _close calls CloseHandle for socket, and closesocket is called after the call. # We should not close the handle twice anyway. But MSDN says the socket handle socket() and WSASocket() opens must be closed by corresponding closesocket(), they are not refering to CloseHandle(). # If so, I think it's not good to call _open_osfhandle for socket # from the begining. Index: win32/win32.c =================================================================== RCS file: /src/ruby/win32/win32.c,v retrieving revision 1.160 diff -u -w -b -p -r1.160 win32.c --- win32/win32.c 16 Jul 2005 06:59:05 -0000 1.160 +++ win32/win32.c 24 Jul 2005 14:02:18 -0000 @@ -3504,6 +3504,9 @@ rb_w32_fclose(FILE *fp) UnlockFile((HANDLE)sock, 0, 0, LK_LEN, LK_LEN); return fclose(fp); } +#ifdef __BORLANDC__ + return fclose(fp); /* ouch! CloseHandle for socket */ +#else _set_osfhnd(fd, (SOCKET)INVALID_HANDLE_VALUE); fclose(fp); if (closesocket(sock) == SOCKET_ERROR) { @@ -3511,6 +3514,7 @@ rb_w32_fclose(FILE *fp) return -1; } return 0; +#endif } int @@ -3522,6 +3526,9 @@ rb_w32_close(int fd) UnlockFile((HANDLE)sock, 0, 0, LK_LEN, LK_LEN); return _close(fd); } +#ifdef __BORLANDC__ + return _close(fd); /* ouch! CloseHandle for socket */ +#else _set_osfhnd(fd, (SOCKET)INVALID_HANDLE_VALUE); _close(fd); if (closesocket(sock) == SOCKET_ERROR) { @@ -3529,6 +3536,7 @@ rb_w32_close(int fd) return -1; } return 0; +#endif } static int