8000 fix: address non-working socket configuration by agners · Pull Request #1563 · python-zeroconf/python-zeroconf · GitHub
[go: up one dir, main page]

Skip to content

fix: address non-working socket configuration #1563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 14, 2025
Merged

Conversation

agners
Copy link
Collaborator
@agners agners commented Apr 10, 2025

For IPv6, a shared listener/responder socket is not really possible when sending to link-local IPv6 multicast addresses (ff02::/16): The kernel needs to know which interface to use for routing.

On IPv4, this is historically a bit different, the kernel just uses what it deems the primary/best route interface based on the routing table. But for IPv6, a message is rejected by Linux with OSError no 99 "Cannot assign requested address" and OSError no 65 "No route to host" on macOS.

Removing the InterfaceChoice.Default config when IPv6 is enabled would be a major breaking change. Instead, inform the user with an error log message and a DeprecationWarning that this is not a working configuration.

As a further cleanup, move the socket options for sending multicast packets out of the common socket creation code. For listen only sockets those settings are not needed.

Also don't use a shared listener/responder sockets for dual-stack mode. Using a shared socket for IPv4 and IPv6 is especially problematic on macOS, where the kernel does not support this and even rejects socket options for IPv4 multicast in this case.

With a separate IPv4 socket, this actually fixes the IPv4 multicast query sending when using the default interface. The sending will only fail on the IPv6 socket, so IPv4 requests will make it through.

With that, the macOS error addressed in #392 is not a problem anymore. Actually, we would like to get an exception in case we get into this combination, so remove the explicit exception handling.

Fixes: #1562

chore: use new_respond_socket for all responder sockets

This unifies the socket creation for all interface specific responder sockets to use the new_respond_socket function. If the responder socket isn't used for multicast, it won't get bound to the interface explicitly. This mostly deduplicates code and makes it easier to maintain.

@agners agners force-pushed the improve-socket-handling branch from 9c34623 to 410f1a2 Compare April 10, 2025 17:12
Copy link
codspeed-hq bot commented Apr 10, 2025

CodSpeed Performance Report

Merging #1563 will not alter performance

Comparing improve-socket-handling (a366aa3) with master (cb2f5b1)

Summary

✅ 6 untouched benchmarks

Copy link
codecov bot commented Apr 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.76%. Comparing base (cb2f5b1) to head (a366aa3).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1563      +/-   ##
==========================================
- Coverage   99.79%   99.76%   -0.03%     
==========================================
  Files          33       33              
  Lines        3401     3400       -1     
  Branches      463      461       -2     
==========================================
- Hits         3394     3392       -2     
  Misses          5        5              
- Partials        2        3       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@agners agners force-pushed the improve-socket-handling branch 2 times, most recently from 8edeb70 to aefa298 Compare April 10, 2025 17:21
@agners agners force-pushed the improve-socket-handling branch from aefa298 to 21e24b8 Compare April 10, 2025 17:33
This unifies the socket creation for all interface specific responder
sockets to use the new_respond_socket function. If the responder socket
isn't used for multicast, it won't get bound to the interface
explicitly.

This mostly deduplicates code and makes it easier to maintain.
@agners agners force-pushed the improve-socket-handling branch 2 times, most recently from f5a3b77 to b685214 Compare April 14, 2025 13:45
@agners agners changed the title fix: reject non-working socket configuration fix: address non-working socket configuration Apr 14, 2025
@agners agners force-pushed the improve-socket-handling branch from b685214 to 0349c71 Compare April 14, 2025 15:16
@agners agners force-pushed the improve-socket-handling branch from a73cce7 to 962f1e1 Compare April 14, 2025 15:26
@agners agners requested a review from bdraco April 14, 2025 15:27
For IPv6, a shared listener/responder socket is not really possible
when sending to link-local IPv6 multicast addresses (ff02::/16):
The kernel needs to know which interface to use for routing.

On IPv4, this is historically a bit different, the kernel just uses
what it deems the primary/best route interface based on the routing
table. But for IPv6, a message is rejected by Linux with OSError no 99
"Cannot assign requested address" and OSError no 65 "No route to host"
on macOS.

Removing the InterfaceChoice.Default config when IPv6 is enabled would
be a major breaking change. Instead, inform the user with an error log
message and a DeprecationWarning that this is not a working
configuration.

As a further cleanup, move the socket options for sending multicast
packets out of the common socket creation code. For listen only sockets
those settings are not needed.

Also don't use a shared listener/responder sockets for dual-stack mode.
Using a shared socket for IPv4 and IPv6 is especially problematic on
macOS, where the kernel does not support this and even rejects socket
options for IPv4 multicast in this case.

With a separate IPv4 socket, this actually fixes the IPv4 multicast
query sending when using the default interface. The sending will only
fail on the IPv6 socket, so IPv4 requests will make it through.

With that, the macOS error addressed in #392 is not a problem anymore.
Actually, we would like to get an exception in case we get into
this combination, so remove the explicit exception handling.
@agners agners force-pushed the improve-socket-handling branch from c7e90d8 to 8f1bad1 Compare April 14, 2025 15:31
@bdraco
Copy link
Member
bdraco commented Apr 14, 2025

verified HA zeroconf tests still pass

Copy link
Member
@bdraco bdraco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @agners

@bdraco bdraco merged commit cc0f835 into master Apr 14, 2025
33 of 34 checks passed
@bdraco bdraco deleted the improve-socket-handling branch April 14, 2025 21:10
@bdraco
Copy link
Member
bdraco commented Apr 14, 2025

tested on production. no issues observed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Interface option InterfaceChoice.Default not working for IPv6
2 participants
0