base: Fix ListenSocket binding logic

The original implementation does not cleanup the socket after it failed
to listen. However, the API doesn't give our user a way to bypass the
bind part and the next try will always break at the bind call.
Furthermore, the next failure will be EINVAL instead of EADDRINUSE so
gem5 will just abort without giving any meaningful message.

In this CL we cleanup the socket if we failed to invoke listen, so the
user can retry with a clean state and even retry on another port.

Test: Try to launch two gem5 that both bind to gdb port (7000) and
    repeat it for 100 times.
Change-Id: I7272ea3c3b6ab56e4b904f3a3a45ed389d00dd05
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/55943
Reviewed-by: Yu-hsin Wang <yuhsingw@google.com>
Reviewed-by: Gabe Black <gabe.black@gmail.com>
Maintainer: Gabe Black <gabe.black@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Nathanael Premillieu <nathanael.premillieu@huawei.com>
This commit is contained in:
Jui-min Lee
2022-01-25 09:14:21 +08:00
parent 06d455ec4e
commit 9d63b391d2

View File

@@ -155,7 +155,12 @@ ListenSocket::listen(int port, bool reuse)
if (::listen(fd, 1) == -1) {
if (errno != EADDRINUSE)
panic("ListenSocket(listen): listen() failed!");
// User may decide to retry with a different port later; however, the
// socket is already bound to a port and the next bind will surely
// fail. We'll close the socket and reset fd to -1 so our user can
// retry with a cleaner state.
close(fd);
fd = -1;
return false;
}