luasocket icon indicating copy to clipboard operation
luasocket copied to clipboard

tcp.bind and tcp.listen always failed if settimeout(0).

Open jakitliang opened this issue 1 year ago • 0 comments

As known to all. int bind(...) is an immediately completion function.

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

bind is not an async function but luasocket stupidly make it async

But luasocket needs set timeout(n) n > 0 to finish a simple bind.

Why do a bind need so much seconds?

And also tcp.listen is also an immediately completion function:

int listen(int sockfd, int backlog);

You just:

if (listen(&fd, 100) == -1)

to check the result immediately.

What does luasocket timeout for?

I GUESS the reason why luasocket is always failed coz using a stupid Thread way to do a dead easy bind:

void socked_bind() {
  auto ret = std::thread([]{
     return bind(fd, ...);
  })

  return ret
}

So the Lua side set timeout(0) means not to wait this thread and use the ret as result. And not doing Thread.join(ret).

So the bind procedure is released and failed.

jakitliang avatar Sep 30 '23 07:09 jakitliang