iij/mruby-socket

SocketError on mruby 1.3

yamori813 opened this issue · 11 comments

I tested mruby 1.3 on FreeBSD 10.3R at amd64 and mips.

I have SocketError.

microserver % ./mruby /tmp/kame.rb
trace:
[0] /tmp/kame.rb:1
[1] /storage/home/hiroki/opensource/mruby-1.3.0/build/mrbgems/mruby-io/m
rblib/io.rb:15:in open
[3] /storage/home/hiroki/opensource/mruby-1.3.0/build/mrbgems/mruby-sock
et/mrblib/socket.rb:252:in initialize
/storage/home/hiroki/opensource/mruby-1.3.0/build/mrbgems/mruby-socket/mrblib/so
cket.rb:252:SocketError

mruby 1.2 don't have this problem.

related issue? mruby/mruby#3750

matz commented

Could you show us kame.rb to reproduce the issue?

I had some test errors of SocketError in mruby-polarssl when I was using it with 1.3.
Those it relate with this?

kame.rb is this.

s = TCPSocket.open("www.kame.net", 80)
s.write("GET / HTTP/1.0\r\n\r\n")
puts s.read
s.close

Patching like this seems to fix it.
What return in ruby do is similar to raising exception so it's hard to implement...
Seems like return unexpectedly stopped in raise e.(Since empty SocketError is raised)

diff --git a/mrblib/socket.rb b/mrblib/socket.rb
index f7d4ce3..14e1840 100644
--- a/mrblib/socket.rb
+++ b/mrblib/socket.rb
@@ -232,9 +232,11 @@ class TCPSocket
       super(host, service)
     else
       s = nil
-      e = SocketError
+      e = nil
+      ret = false
       Addrinfo.foreach(host, service) { |ai|
         begin
+          next if ret
           s = Socket._socket(ai.afamily, Socket::SOCK_STREAM, 0)
           if local_host or local_service
             local_host ||= (ai.afamily == Socket::AF_INET) ? "0.0.0.0" : "::"
@@ -244,12 +246,15 @@ class TCPSocket
           end
           Socket._connect(s, ai.to_sockaddr)
           super(s, "r+")
+          ret = true
+          e = nil
+          next
           return
         rescue => e0
           e = e0
         end
       }
-      raise e
+      raise e if e
     end
   end
matz commented

I still think this is a 1.3 issue so we have to fix mruby.

This is what i had to do to make in run with MRB_INT64: Asmod4n@cd238b3

I just ran into this as well, mruby 1.3.0 causing SocketError when using mruby-httprequest

I'm building on Alpine Linux incase the info helps. Downgraded to 1.2.0 for now.

matz commented

It doesn't happen on the latest mruby (to be 1.4). You can close this issue now.

I checked at mruby/mruby 550c8782162d319edcbaec3bfdeec2d269dfbdfc. That is work fine.

Thanks