[3.14] gh-154291: Fix socket.has_dualstack_ipv6() on DragonFly BSD (GH-154292) (GH-154300)

serhiy-storchaka <[email protected]>
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/b2e8bd6ca949204f2af120779d3695b1434094a8
commit: b2e8bd6ca949204f2af120779d3695b1434094a8
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-07-20T20:25:57Z
summary:

[3.14] gh-154291: Fix socket.has_dualstack_ipv6() on DragonFly BSD (GH-154292) (GH-154300)

On DragonFly BSD setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, 0) succeeds without
actually clearing the flag, so has_dualstack_ipv6() wrongly returned True.
Verify with getsockopt() that IPV6_V6ONLY was cleared.
(cherry picked from commit c7bbf040e721781be8f11a3b0716220835d4de2f)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-07-20-22-03-44.gh-issue-154291.Mi5HeQ.rst
M Lib/socket.py

diff --git a/Lib/socket.py b/Lib/socket.py
index bbb476f2fc010e..9ac0badc6606c1 100644
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -891,7 +891,9 @@ def has_dualstack_ipv6():
     try:
         with socket(AF_INET6, SOCK_STREAM) as sock:
             sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, 0)
-            return True
+            # On some platforms (e.g. DragonFly BSD) setting IPV6_V6ONLY to 0
+            # silently has no effect, so check that it was actually cleared.
+            return sock.getsockopt(IPPROTO_IPV6, IPV6_V6ONLY) == 0
     except error:
         return False
 
diff --git a/Misc/NEWS.d/next/Library/2026-07-20-22-03-44.gh-issue-154291.Mi5HeQ.rst b/Misc/NEWS.d/next/Library/2026-07-20-22-03-44.gh-issue-154291.Mi5HeQ.rst
new file mode 100644
index 00000000000000..d962f8987ad491
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-07-20-22-03-44.gh-issue-154291.Mi5HeQ.rst
@@ -0,0 +1,3 @@
+Fix :func:`socket.has_dualstack_ipv6` to return ``False`` on platforms such as
+DragonFly BSD where setting ``IPV6_V6ONLY`` to 0 silently has no
+effect.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.