Skip to content

Commit 76158db

Browse files
committed
windows: add Fiddle.win32_last_socket_error{,=}
GitHub: fix GH-72 Users can't use WSAGetLastError() with Ruby 3.0 or later because rb_funcall() resets the last socket error internally. Users can get the last socket error by Fiddle.win32_last_socket_error. Reported by Kentaro Hayashi. Thanks!!!
1 parent 99c6b20 commit 76158db

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

ext/fiddle/function.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,17 @@ function_call(int argc, VALUE argv[], VALUE self)
375375
(void)rb_thread_call_without_gvl(nogvl_ffi_call, &args, 0, 0);
376376
}
377377

378-
rb_funcall(mFiddle, rb_intern("last_error="), 1, INT2NUM(errno));
378+
{
379+
int errno_keep = errno;
379380
#if defined(_WIN32)
380-
rb_funcall(mFiddle, rb_intern("win32_last_error="), 1, INT2NUM(errno));
381+
int socket_error = WSAGetLastError();
382+
rb_funcall(mFiddle, rb_intern("win32_last_error="), 1,
383+
INT2NUM(errno_keep));
384+
rb_funcall(mFiddle, rb_intern("win32_last_socket_error="), 1,
385+
INT2NUM(socket_error));
381386
#endif
387+
rb_funcall(mFiddle, rb_intern("last_error="), 1, INT2NUM(errno_keep));
388+
}
382389

383390
ALLOCV_END(alloc_buffer);
384391

lib/fiddle.rb

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ def self.win32_last_error
1717
def self.win32_last_error= error
1818
Thread.current[:__FIDDLE_WIN32_LAST_ERROR__] = error
1919
end
20+
21+
# Returns the last win32 socket +Error+ of the current executing
22+
# +Thread+ or nil if none
23+
def self.win32_last_socket_error
24+
Thread.current[:__FIDDLE_WIN32_LAST_SOCKET_ERROR__]
25+
end
26+
27+
# Sets the last win32 socket +Error+ of the current executing
28+
# +Thread+ to +error+
29+
def self.win32_last_socket_error= error
30+
Thread.current[:__FIDDLE_WIN32_LAST_SOCKET_ERROR__] = error
31+
end
2032
end
2133

2234
# Returns the last +Error+ of the current executing +Thread+ or nil if none

0 commit comments

Comments
 (0)