Skip to content

Commit ac3321c

Browse files
authored
Fix setting MQTT socket buffer size with WebsocketWrapper (#117672)
1 parent 99565be commit ac3321c

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

homeassistant/components/mqtt/client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,14 @@ def _async_start_misc_loop(self) -> None:
540540

541541
def _increase_socket_buffer_size(self, sock: SocketType) -> None:
542542
"""Increase the socket buffer size."""
543+
if not hasattr(sock, "setsockopt") and hasattr(sock, "_socket"):
544+
# The WebsocketWrapper does not wrap setsockopt
545+
# so we need to get the underlying socket
546+
# Remove this once
547+
# https://github.com/eclipse/paho.mqtt.python/pull/843
548+
# is available.
549+
sock = sock._socket # noqa: SLF001
550+
543551
new_buffer_size = PREFERRED_BUFFER_SIZE
544552
while True:
545553
try:

tests/components/mqtt/test_init.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4434,6 +4434,43 @@ async def test_server_sock_buffer_size(
44344434
assert "Unable to increase the socket buffer size" in caplog.text
44354435

44364436

4437+
@patch("homeassistant.components.mqtt.client.INITIAL_SUBSCRIBE_COOLDOWN", 0.0)
4438+
@patch("homeassistant.components.mqtt.client.DISCOVERY_COOLDOWN", 0.0)
4439+
@patch("homeassistant.components.mqtt.client.SUBSCRIBE_COOLDOWN", 0.0)
4440+
async def test_server_sock_buffer_size_with_websocket(
4441+
hass: HomeAssistant,
4442+
mqtt_client_mock: MqttMockPahoClient,
4443+
mqtt_mock_entry: MqttMockHAClientGenerator,
4444+
caplog: pytest.LogCaptureFixture,
4445+
) -> None:
4446+
"""Test handling the socket buffer size fails."""
4447+
mqtt_mock = await mqtt_mock_entry()
4448+
await hass.async_block_till_done()
4449+
assert mqtt_mock.connected is True
4450+
4451+
mqtt_client_mock.loop_misc.return_value = paho_mqtt.MQTT_ERR_SUCCESS
4452+
4453+
client, server = socket.socketpair(
4454+
family=socket.AF_UNIX, type=socket.SOCK_STREAM, proto=0
4455+
)
4456+
client.setblocking(False)
4457+
server.setblocking(False)
4458+
4459+
class FakeWebsocket(paho_mqtt.WebsocketWrapper):
4460+
def _do_handshake(self, *args, **kwargs):
4461+
pass
4462+
4463+
wrapped_socket = FakeWebsocket(client, "127.0.01", 1, False, "/", None)
4464+
4465+
with patch.object(client, "setsockopt", side_effect=OSError("foo")):
4466+
mqtt_client_mock.on_socket_open(mqtt_client_mock, None, wrapped_socket)
4467+
mqtt_client_mock.on_socket_register_write(
4468+
mqtt_client_mock, None, wrapped_socket
4469+
)
4470+
await hass.async_block_till_done()
4471+
assert "Unable to increase the socket buffer size" in caplog.text
4472+
4473+
44374474
@patch("homeassistant.components.mqtt.client.INITIAL_SUBSCRIBE_COOLDOWN", 0.0)
44384475
@patch("homeassistant.components.mqtt.client.DISCOVERY_COOLDOWN", 0.0)
44394476
@patch("homeassistant.components.mqtt.client.SUBSCRIBE_COOLDOWN", 0.0)

0 commit comments

Comments
 (0)