From 82f881255da75799a11e80c22a6ff5fae8b174fb Mon Sep 17 00:00:00 2001 From: Nathanael Premillieu Date: Thu, 13 Aug 2020 16:10:57 +0200 Subject: [PATCH] base: avoid recreating socket at each call to listen() A new socket was created each time listen() is called, which is problematic when the bind or listen operation on it are not successful (mostly because the associated port is already in use). It can lead gem5 to open too many files and crash for multicores configurations, a socket being created for remote GDB for each core. The other way to deal with this problem would be to close the socket in the case the function return false. But I find the proposed solution simpler. Change-Id: I848955a10c89e1da033bf773c83556a5dc5ef9a2 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/32994 Reviewed-by: Gabe Black Maintainer: Gabe Black Tested-by: kokoro --- src/base/socket.cc | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/base/socket.cc b/src/base/socket.cc index adf831503..fce54aa76 100644 --- a/src/base/socket.cc +++ b/src/base/socket.cc @@ -101,9 +101,12 @@ ListenSocket::listen(int port, bool reuse) if (listening) panic("Socket already listening!"); - fd = ::socket(PF_INET, SOCK_STREAM, 0); - if (fd < 0) - panic("Can't create socket:%s !", strerror(errno)); + // only create socket if not already created by a previous call + if (fd == -1) { + fd = ::socket(PF_INET, SOCK_STREAM, 0); + if (fd < 0) + panic("Can't create socket:%s !", strerror(errno)); + } if (reuse) { int i = 1; -- 2.30.2