Skip to content

Commit 0406f54

Browse files
committed
Use a faster and simpler #accept for TCPServer and UNIXServer
* The Addrinfo and extra native calls are not needed for those. * This is also less prone to races related to a concurrent Socket#close as it is a single native call instead of multiple.
1 parent 665d812 commit 0406f54

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

lib/truffle/socket/tcp_server.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,12 @@ def listen(backlog)
6363
end
6464

6565
def accept
66-
socket, _ = Truffle::Socket.accept_and_addrinfo(self, TCPSocket, true)
67-
68-
socket
66+
Truffle::Socket.accept(self, TCPSocket, true)
6967
end
7068

7169
private def __accept_nonblock(exception)
7270
self.nonblock = true
73-
socket, _ = Truffle::Socket.accept_and_addrinfo(self, TCPSocket, exception)
74-
75-
socket
71+
Truffle::Socket.accept(self, TCPSocket, exception)
7672
end
7773

7874
def sysaccept

lib/truffle/socket/truffle.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ def self.accept_and_addrinfo(source, new_class, exception)
124124
end
125125
end
126126

127+
def self.accept(source, new_class, exception)
128+
raise IOError, 'socket has been closed' if source.closed?
129+
130+
fd = Truffle::Socket::Foreign.accept(source.fileno, ::FFI::Pointer::NULL, ::FFI::Pointer::NULL)
131+
if fd < 0
132+
if !exception and Errno.errno == Truffle::POSIX::EAGAIN_ERRNO
133+
return :wait_readable
134+
else
135+
Error.read_error('accept(2)', source)
136+
end
137+
end
138+
139+
new_class.for_fd(fd)
140+
end
141+
127142
def self.listen(source, backlog)
128143
backlog = Primitive.rb_to_int(backlog)
129144
err = Foreign.listen(source.fileno, backlog)

lib/truffle/socket/unix_server.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,12 @@ def listen(backlog)
5151
end
5252

5353
def accept
54-
socket, _ = Truffle::Socket.accept_and_addrinfo(self, UNIXSocket, true)
55-
56-
socket
54+
Truffle::Socket.accept(self, UNIXSocket, true)
5755
end
5856

5957
private def __accept_nonblock(exception)
6058
self.nonblock = true
61-
socket, _ = Truffle::Socket.accept_and_addrinfo(self, UNIXSocket, exception)
62-
63-
socket
59+
Truffle::Socket.accept(self, UNIXSocket, exception)
6460
end
6561

6662
def sysaccept

0 commit comments

Comments
 (0)