Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions core/src/main/java/org/jruby/ext/socket/RubyBasicSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import jnr.posix.POSIX;
import jnr.unixsocket.UnixSocketAddress;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyBasicObject;
import org.jruby.RubyBoolean;
import org.jruby.RubyClass;
Expand All @@ -56,9 +57,11 @@
import org.jruby.api.Access;
import org.jruby.api.Convert;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.exceptions.RaiseException;
import org.jruby.ext.fcntl.FcntlLibrary;
import org.jruby.platform.Platform;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
Expand Down Expand Up @@ -1062,4 +1065,31 @@ protected static class ReceiveTuple {
RubyString result;
InetSocketAddress sender;
}

// MRI: rsock_s_socketpair, which yields the pair and then closes the second socket and the first
protected static IRubyObject yieldPair(ThreadContext context, RubyBasicSocket sock0, RubyBasicSocket sock1, Block block) {
RubyArray<?> pair = newArray(context, sock0, sock1);
if (!block.isGiven()) return pair;

try {
return block.yield(context, pair);
} finally {
try {
closeRescued(context, sock1);
} finally {
closeRescued(context, sock0);
}
}
}

// MRI: ext/socket's io_close, which rescues any StandardError, unlike RubyIO.ioClose
private static void closeRescued(ThreadContext context, IRubyObject io) {
IRubyObject errorInfo = context.getErrorInfo();
try {
io.callMethod(context, "close");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this really need to be dynamic? given these seems to be of RubyBasicSocket type...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IO.pipe's block form seems to do it like this: https://github.com/jruby/jruby/blob/61230eb921/core/src/main/java/org/jruby/RubyIO.java#L2367.

I think it's needed here since you can override close.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CRuby also dynamically invokes close for its ensure around the yield.

https://github.com/ruby/ruby/blob/b9a50a0b81a1befe0ab02bd93f01a9e684c55149/ext/socket/socket.c#L156-L167

} catch (RaiseException re) {
if (!context.runtime.getStandardError().isInstance(re.getException())) throw re;
context.setErrorInfo(errorInfo);
}
}
}// RubyBasicSocket
17 changes: 9 additions & 8 deletions core/src/main/java/org/jruby/ext/socket/RubySocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
Expand Down Expand Up @@ -328,19 +329,19 @@ public static IRubyObject ip_address_list(ThreadContext context, IRubyObject sel
}

@JRubyMethod(name = {"socketpair", "pair"}, meta = true)
public static IRubyObject socketpair(ThreadContext context, IRubyObject recv, IRubyObject domain, IRubyObject type, IRubyObject protocol) {
public static IRubyObject socketpair(ThreadContext context, IRubyObject recv, IRubyObject domain, IRubyObject type, IRubyObject protocol, Block block) {
ProtocolFamily pf = SocketUtils.protocolFamilyFromArg(context, protocol);
if (pf == null ) pf = ProtocolFamily.PF_UNIX;

if (pf != ProtocolFamily.PF_UNIX && pf.ordinal() != 0) {
throw context.runtime.newErrnoEOPNOTSUPPError("Socket.socketpair only supports streaming UNIX sockets");
}

return socketpair(context, recv, domain, type);
return socketpair(context, recv, domain, type, block);
}

@JRubyMethod(name = {"socketpair", "pair"}, meta = true)
public static IRubyObject socketpair(ThreadContext context, IRubyObject recv, IRubyObject domain, IRubyObject type) {
public static IRubyObject socketpair(ThreadContext context, IRubyObject recv, IRubyObject domain, IRubyObject type, Block block) {
AddressFamily af = SocketUtils.addressFamilyFromArg(context, domain);
if (af == null) af = AddressFamily.AF_UNIX;
Sock s = SocketUtils.sockFromArg(context, type);
Expand All @@ -355,25 +356,25 @@ public static IRubyObject socketpair(ThreadContext context, IRubyObject recv, IR
// TODO: type and protocol

UnixSocketChannel[] sp;
final RubyClass socketClass = Access.getClass(context, "Socket");
RubySocket sock0 = new RubySocket(runtime, socketClass);
RubySocket sock1 = new RubySocket(runtime, socketClass);

try {
sp = UnixSocketChannel.pair();
final RubyClass socketClass = Access.getClass(context, "Socket");

RubySocket sock0 = new RubySocket(runtime, socketClass);
ChannelFD fd0 = newChannelFD(runtime, sp[0]);
sock0.initFieldsFromDescriptor(runtime, fd0);
sock0.initSocket(fd0);

RubySocket sock1 = new RubySocket(runtime, socketClass);
ChannelFD fd1 = newChannelFD(runtime, sp[1]);
sock1.initFieldsFromDescriptor(runtime, fd1);
sock1.initSocket(fd1);

return newArray(context, sock0, sock1);
} catch (IOException ioe) {
throw runtime.newIOErrorFromException(ioe);
}

return yieldPair(context, sock0, sock1, block);
}

private void initFieldsFromDescriptor(Ruby runtime, ChannelFD fd) {
Expand Down
15 changes: 7 additions & 8 deletions core/src/main/java/org/jruby/ext/socket/RubyUNIXSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.jruby.api.Define;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
Expand Down Expand Up @@ -238,30 +239,28 @@ public IRubyObject recv_io(ThreadContext context, IRubyObject[] args) {
}

@JRubyMethod(name = {"socketpair", "pair"}, optional = 2, checkArity = false, meta = true)
public static IRubyObject socketpair(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
public static IRubyObject socketpair(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
Arity.checkArgumentCount(context, args, 0, 2);

final Ruby runtime = context.runtime;

// TODO: type and protocol

UnixSocketChannel[] sp;
final RubyClass UNIXSocket = Access.getClass(context, "UNIXSocket");
RubyUNIXSocket sock = (RubyUNIXSocket)(Helpers.invoke(context, UNIXSocket, "allocate"));
RubyUNIXSocket sock2 = (RubyUNIXSocket)(Helpers.invoke(context, UNIXSocket, "allocate"));

try {
sp = UnixSocketChannel.pair();

final RubyClass UNIXSocket = Access.getClass(context, "UNIXSocket");
RubyUNIXSocket sock = (RubyUNIXSocket)(Helpers.invoke(context, UNIXSocket, "allocate"));
sock.init_sock(runtime, sp[0], "");

RubyUNIXSocket sock2 = (RubyUNIXSocket)(Helpers.invoke(context, UNIXSocket, "allocate"));
sock2.init_sock(runtime, sp[1], "");

return newArray(context, sock, sock2);

} catch (IOException ioe) {
throw runtime.newIOErrorFromException(ioe);
}

return yieldPair(context, sock, sock2, block);
}

@Override
Expand Down
31 changes: 31 additions & 0 deletions spec/ruby/library/socket/socket/pair_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,36 @@
s2.close
end
end

it "yields the sockets, returns the block's value and closes them afterwards" do
sockets = nil
Socket.pair(:UNIX, :STREAM) { |s1, s2| sockets = [s1, s2]; :result }.should == :result
sockets.map(&:closed?).should == [true, true]
end

it "closes the sockets when the block raises" do
sockets = nil
-> { Socket.pair(:UNIX, :STREAM) { |s1, s2| sockets = [s1, s2]; raise "boom" } }.should.raise(RuntimeError, "boom")
sockets.map(&:closed?).should == [true, true]
end

it "does not raise when the block closes the sockets itself" do
Socket.pair(:UNIX, :STREAM) { |s1, s2| s1.close; s2.close; :ok }.should == :ok
end

it "ignores a StandardError from closing a socket and still closes the other one" do
sockets = nil
Socket.pair(:UNIX, :STREAM) do |s1, s2|
sockets = [s1, s2]
def s2.close
raise IOError, "close failed"
end
:result
end.should == :result
sockets[0].should.closed?
$!.should == nil
ensure
IO.instance_method(:close).bind_call(sockets[1]) if sockets
end
end
end
27 changes: 27 additions & 0 deletions spec/ruby/library/socket/unixsocket/pair_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,31 @@
@s2.path.should == ""
end
end

it "yields the sockets, returns the block's value and closes them afterwards" do
sockets = nil
UNIXSocket.pair { |s1, s2| sockets = [s1, s2]; :result }.should == :result
sockets.map(&:closed?).should == [true, true]
end

it "closes the sockets when the block raises" do
sockets = nil
-> { UNIXSocket.pair { |s1, s2| sockets = [s1, s2]; raise "boom" } }.should.raise(RuntimeError, "boom")
sockets.map(&:closed?).should == [true, true]
end

it "ignores a StandardError from closing a socket and still closes the other one" do
sockets = nil
UNIXSocket.pair do |s1, s2|
sockets = [s1, s2]
def s2.close
raise IOError, "close failed"
end
:result
end.should == :result
sockets[0].should.closed?
$!.should == nil
ensure
IO.instance_method(:close).bind_call(sockets[1]) if sockets
end
end
2 changes: 2 additions & 0 deletions test/mri/excludes/TestFiberIOClose.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exclude :test_io_close_across_fibers, "hangs: close does not call the scheduler's fiber_interrupt"
exclude :test_io_close_blocking_thread, "on Linux, a thread blocked reading the IO sees EOF or stays blocked instead of raising IOError when it is closed"
1 change: 1 addition & 0 deletions test/mri/excludes/TestSocketNonblock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
exclude :test_recv_nonblock_no_exception, "needs investigation"
exclude :test_recvfrom_nonblock_no_exception, "needs investigation"
exclude :test_recvmsg_nonblock_error, "needs investigation"
exclude :test_sendmsg_nonblock_seqpacket, "sendmsg_nonblock is not implemented"
exclude :test_socket_recvfrom_nonblock, "needs investigation"
exclude :test_tcp_recv_nonblock, "needs investigation"
exclude :test_udp_recv_nonblock, "needs investigation"
Expand Down
2 changes: 2 additions & 0 deletions test/mri/excludes/TestSocket_UNIXSocket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
exclude :test_cloexec, "needs investigation"
exclude :test_dgram_pair, "needs investigation"
exclude :test_dgram_pair_sendrecvmsg_errno_set, "needs investigation"
exclude :test_epipe, "writing after shutdown(SHUT_WR) raises IOError, not Errno::EPIPE"
exclude :test_fd_passing, "close_on_exec? not implemented"
exclude :test_fd_passing_class_mode, "send_io fails with EPIPE"
exclude :test_fd_passing_race_condition, "needs investigation"
exclude :test_getpeereid, "needs investigation"
exclude :test_seqpacket_pair, "hangs (tested on macos M1)"
Expand Down
Loading