[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: luasocket invalidates its internal state by closing the socket when setsockname() fails
- From: Sam Roberts <vieuxtech@...>
- Date: Wed, 24 Feb 2010 14:34:38 -0800
This is purely a luasocket behaviour. The system calls allow bind() to
fail, leaving the socket valid.
This behaviour of luasocket is particularly anti-social when
attempting to repeatedly bind to local ports, to search for one that
is free.
Code:
require"socket"
print(socket._VERSION)
s = assert(socket.udp())
print("setname", s:setsockname("*", 9876))
print("getname", s:getsockname())
c = assert(socket.udp())
print("setname", c:setsockname("*", 9876))
print("setname", c:setsockname("*", 9875))
Output:
lua udpe.lua
LuaSocket 2.0.1
setname 1
getname 0.0.0.0 9876
setname nil address already in use
setname nil Bad file descriptor
Strace:
bind(3, {sa_family=AF_INET, sin_port=htons(9876),
sin_addr=inet_addr("0.0.0.0")}, 16) = 0
socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 4
bind(4, {sa_family=AF_INET, sin_port=htons(9876),
sin_addr=inet_addr("0.0.0.0")}, 16) = -1 EADDRINUSE (Address already
in use)
bind of descriptor 4 fails, this should be a no-op, but instead
luasocket closes the fd!
close(4) = 0
write(1, "setname\tnil\taddress already in u"..., 35setname nil
address already in use
) = 35
It doesn't remember that it closed it, though, and keeps trying to use it...
fcntl(-1, F_GETFL) = -1 EBADF (Bad file descriptor)
fcntl(-1, F_SETFL,
O_ACCMODE|O_CREAT|O_EXCL|O_NOCTTY|O_TRUNC|O_APPEND|O_SYNC|O_ASYNC|O_DIRECT|O_LARGEFILE|O_DIRECTORY|O_NOFOLLOW|O_NOATIME|0xfff8003c)
= -1 EBADF (Bad file descriptor)
bind(4294967295, {sa_family=AF_INET, sin_port=htons(9875),
sin_addr=inet_addr("0.0.0.0")}, 16) = -1 EBADF (Bad file descriptor)