From: merch-redmine@... Date: 2021-06-15T04:13:18+00:00 Subject: [ruby-core:104264] [Ruby master Bug#17986] Ractor is stdlib Socket unfriendly Issue #17986 has been updated by jeremyevans0 (Jeremy Evans). The code you are showing (`make_shareable` with a socket) is broken because in order to be shareable, an object must be immutable, and a socket cannot really be immutable and usable. You can work around it using `for_fd` (this should show that both ractors are processing the socket): ```ruby require 'socket' Thread.new do client = TCPServer.new(9100).accept 10.times do |i| sleep 1 client.write i.to_s end end sleep 1 socket = TCPSocket.open('localhost', 9100) sock_ractor = lambda do |i, socket| Ractor.new(i, socket.to_i) do |i, fd| sock = TCPSocket.for_fd(fd) while socks = IO.select([sock]).first socks.each do |s| $stderr.syswrite("#{i}:#{s.read(1)}\n") end end end end r1 = sock_ractor[1, socket] r2 = sock_ractor[2, socket] sleep 11 ``` I don't think the behavior you are showing is a bug, so I think this should be closed. However, I'll wait for a while and see if another core team member feels differently. ---------------------------------------- Bug #17986: Ractor is stdlib Socket unfriendly https://bugs.ruby-lang.org/issues/17986#change-92474 * Author: kvokka (Mike Beliakov) * Status: Open * Priority: Normal * ruby -v: ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-darwin19] * Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN ---------------------------------------- **Description** In the process of playing with Ractors was found, that there is no way to use stdlib Sockets. My intent was to implement bidirectional connection with Socket using Ractors. This small console snippet is self explanatory: ```ruby [1] pry(main)> s=Ractor.make_shareable(TCPSocket.open('localhost', 9100)) => # [2] pry(main)> s.print 'foo' FrozenError: can't modify frozen TCPSocket: # from (pry):5:in `write' [3] pry(main)> s=Ractor.make_shareable(TCPSocket.open('localhost', 9100), copy: true) => # [4] pry(main)> s.print 'foo' FrozenError: can't modify frozen TCPSocket: # ``` The only option is to `move` socket in Ractor, but in this case there is no way to share the socket between 2 Ractors, so I can not put a listener loop on the socket. -- https://bugs.ruby-lang.org/ Unsubscribe: