summaryrefslogtreecommitdiff
path: root/win32/win32.c
diff options
context:
space:
mode:
Diffstat (limited to 'win32/win32.c')
-rw-r--r--win32/win32.c27
1 files changed, 19 insertions, 8 deletions
diff --git a/win32/win32.c b/win32/win32.c
index 8fb1d2d860..44de047716 100644
--- a/win32/win32.c
+++ b/win32/win32.c
@@ -2376,21 +2376,32 @@ int
rb_w32_accept(int s, struct sockaddr *addr, int *addrlen)
{
SOCKET r;
+ int fd;
if (!NtSocketsInitialized) {
StartSockets();
}
RUBY_CRITICAL({
- r = accept(TO_SOCKET(s), addr, addrlen);
- if (r == INVALID_SOCKET) {
- errno = map_errno(WSAGetLastError());
- s = -1;
- }
- else {
- s = rb_w32_open_osfhandle(r, O_RDWR|O_BINARY);
+ HANDLE h = CreateFile("NUL", 0, 0, NULL, OPEN_ALWAYS, 0, NULL);
+ fd = rb_w32_open_osfhandle((long)h, O_RDWR|O_BINARY|O_NOINHERIT);
+ if (fd != -1) {
+ r = accept(TO_SOCKET(s), addr, addrlen);
+ if (r != INVALID_SOCKET) {
+ MTHREAD_ONLY(EnterCriticalSection(&(_pioinfo(fd)->lock)));
+ _set_osfhnd(fd, r);
+ MTHREAD_ONLY(LeaveCriticalSection(&_pioinfo(fd)->lock));
+ CloseHandle(h);
+ }
+ else {
+ errno = map_errno(WSAGetLastError());
+ close(fd);
+ fd = -1;
+ }
}
+ else
+ CloseHandle(h);
});
- return s;
+ return fd;
}
#undef bind