Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/zeroconf/_utils/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,25 @@ def new_socket(
bind_tup,
)
return None
if ex.errno == errno.EADDRINUSE:
if sys.platform.startswith("darwin") or sys.platform.startswith("freebsd"):
log.error(
"Address in use when binding to %s; "
"On BSD based systems sharing the same port with another "
"stack may require processes to run with the same UID; "
"When using avahi, make sure disallow-other-stacks is set"
" to no in avahi-daemon.conf",
bind_tup,
)
else:
log.error(
"Address in use when binding to %s; "
"When using avahi, make sure disallow-other-stacks is set"
" to no in avahi-daemon.conf",
bind_tup,
)
# This is still a fatal error as its not going to work
# if we can't hear the traffic coming in.
raise
log.debug("Created socket %s", s)
return s
Expand Down
34 changes: 34 additions & 0 deletions tests/utils/test_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,40 @@ def _mock_socket(*args, **kwargs):
netutils.new_socket(("0.0.0.0", 0)) # type: ignore[arg-type]


def test_bind_raises_address_in_use(caplog: pytest.LogCaptureFixture) -> None:
"""Test bind failing in new_socket returns None on EADDRINUSE."""

def _mock_socket(*args, **kwargs):
sock = MagicMock()
sock.bind = MagicMock(side_effect=OSError(errno.EADDRINUSE, f"Error: {errno.EADDRINUSE}"))
return sock

with (
pytest.raises(OSError),
patch.object(sys, "platform", "darwin"),
patch("socket.socket", _mock_socket),
):
netutils.new_socket(("0.0.0.0", 0)) # type: ignore[arg-type]
assert (
"On BSD based systems sharing the same port with "
"another stack may require processes to run with the same UID"
) in caplog.text
assert (
"When using avahi, make sure disallow-other-stacks is set to no in avahi-daemon.conf" in caplog.text
)

caplog.clear()
with pytest.raises(OSError), patch.object(sys, "platform", "linux"), patch("socket.socket", _mock_socket):
netutils.new_socket(("0.0.0.0", 0)) # type: ignore[arg-type]
assert (
"On BSD based systems sharing the same port with "
"another stack may require processes to run with the same UID"
) not in caplog.text
assert (
"When using avahi, make sure disallow-other-stacks is set to no in avahi-daemon.conf" in caplog.text
)


def test_new_respond_socket_new_socket_returns_none():
"""Test new_respond_socket returns None if new_socket returns None."""
with patch.object(netutils, "new_socket", return_value=None):
Expand Down