Jui-min Lee has submitted this change. ( https://gem5-review.googlesource.com/c/public/gem5/+/55943 )

Change subject: base: Fix ListenSocket binding logic
......................................................................

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 <yuhsi...@google.com>
Reviewed-by: Gabe Black <gabe.bl...@gmail.com>
Maintainer: Gabe Black <gabe.bl...@gmail.com>
Tested-by: kokoro <noreply+kok...@google.com>
Reviewed-by: Nathanael Premillieu <nathanael.premill...@huawei.com>
---
M src/base/socket.cc
1 file changed, 32 insertions(+), 1 deletion(-)

Approvals:
Gabe Black: Looks good to me, but someone else must approve; Looks good to me, approved
  Yu-hsin Wang: Looks good to me, but someone else must approve
  Nathanael Premillieu: Looks good to me, approved
  kokoro: Regressions pass




diff --git a/src/base/socket.cc b/src/base/socket.cc
index 860249a..5cf67fd 100644
--- a/src/base/socket.cc
+++ b/src/base/socket.cc
@@ -155,7 +155,12 @@
     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;
     }


--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/55943
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I7272ea3c3b6ab56e4b904f3a3a45ed389d00dd05
Gerrit-Change-Number: 55943
Gerrit-PatchSet: 3
Gerrit-Owner: Jui-min Lee <f...@google.com>
Gerrit-Reviewer: Bobby Bruce <bbr...@ucdavis.edu>
Gerrit-Reviewer: Daniel Carvalho <oda...@yahoo.com.br>
Gerrit-Reviewer: Earl Ou <shunhsin...@google.com>
Gerrit-Reviewer: Gabe Black <gabe.bl...@gmail.com>
Gerrit-Reviewer: Gabe Black <gabebl...@google.com>
Gerrit-Reviewer: Jui-min Lee <f...@google.com>
Gerrit-Reviewer: Nathanael Premillieu <nathanael.premill...@huawei.com>
Gerrit-Reviewer: Yu-hsin Wang <yuhsi...@google.com>
Gerrit-Reviewer: kokoro <noreply+kok...@google.com>
Gerrit-MessageType: merged
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to