From 77c52fc37b5670041bf2b22e3c1c3df29e6f1cfa Mon Sep 17 00:00:00 2001 From: MTAwsl Date: Tue, 20 Dec 2022 23:28:55 +0800 Subject: [PATCH 1/5] Fixed a bug in socket.getfqdn() When getfqdn called with name "::", instead of returning gethostname(), it will call gethostbyaddr("::"). This will raise an exception "socket.herror: [Errno 1] Unknown host", which will cause a 30 seconds(timeout) delay and return incorrect result. The solution is to add a filter to the first if statement, in line 792 socket.py. --- Lib/socket.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/socket.py b/Lib/socket.py index 1c8cef6ce65810..9feb713490568e 100644 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -785,11 +785,11 @@ def getfqdn(name=''): First the hostname returned by gethostbyaddr() is checked, then possibly existing aliases. In case no FQDN is available and `name` - was given, it is returned unchanged. If `name` was empty or '0.0.0.0', + was given, it is returned unchanged. If `name` was empty, '0.0.0.0' or '::', hostname from gethostname() is returned. """ name = name.strip() - if not name or name == '0.0.0.0': + if not name or name == '0.0.0.0' or name == '::': name = gethostname() try: hostname, aliases, ipaddrs = gethostbyaddr(name) From 34f6fa37dc1a39f105a62dc538d9182c06cd8fda Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 20 Dec 2022 16:14:20 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst b/Misc/NEWS.d/next/Core and Builtins/2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst new file mode 100644 index 00000000000000..47cde7437378fa --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst @@ -0,0 +1 @@ +Fixed incorrect result and delays in socket.getfqdn. (Patched by Dominic Socular @MTAwsl) From 2034fabbdfd1c7876ff4713fc68eca18d4044ae5 Mon Sep 17 00:00:00 2001 From: MTAwsl Date: Wed, 21 Dec 2022 13:48:52 +0800 Subject: [PATCH 3/5] Added test case for GH-100374. --- Lib/test/test_socket.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index d808f3f62b96dc..f1b4018c265e18 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -1769,6 +1769,10 @@ def test_getaddrinfo_ipv6_basic(self): ) self.assertEqual(sockaddr, ('ff02::1de:c0:face:8d', 1234, 0, 0)) + def test_getfqdn_filter_localhost(self): + self.assertEqual(socket.getfqdn(), socket.getfqdn("0.0.0.0")) + self.assertEqual(socket.getfqdn(), socket.getfqdn("::")) + @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') @unittest.skipIf(sys.platform == 'win32', 'does not work on Windows') @unittest.skipIf(AIX, 'Symbolic scope id does not work') From c2fb7f65d4a9103a051e73779232102f09069572 Mon Sep 17 00:00:00 2001 From: Dominic Socular Date: Wed, 21 Dec 2022 20:49:41 +0800 Subject: [PATCH 4/5] Update Misc/NEWS.d/next/Core and Builtins/2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst Co-authored-by: Dong-hee Na --- .../2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst b/Misc/NEWS.d/next/Core and Builtins/2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst index 47cde7437378fa..e78352fb188e3c 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2022-12-20-16-14-19.gh-issue-100374.YRrVHT.rst @@ -1 +1 @@ -Fixed incorrect result and delays in socket.getfqdn. (Patched by Dominic Socular @MTAwsl) +Fix incorrect result and delay in :func:`socket.getfqdn`. Patch by Dominic Socular. From 908cd68e4d0340809ca57fc33ae016aebeae319f Mon Sep 17 00:00:00 2001 From: Dominic Socular Date: Wed, 21 Dec 2022 20:51:45 +0800 Subject: [PATCH 5/5] Update Lib/socket.py with proper format Co-authored-by: Dong-hee Na --- Lib/socket.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/socket.py b/Lib/socket.py index 9feb713490568e..3a4f94de9cc03a 100644 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -789,7 +789,7 @@ def getfqdn(name=''): hostname from gethostname() is returned. """ name = name.strip() - if not name or name == '0.0.0.0' or name == '::': + if not name or name in ('0.0.0.0', '::'): name = gethostname() try: hostname, aliases, ipaddrs = gethostbyaddr(name)