Skip to content

Commit

Permalink
Fix setting MQTT socket buffer size with WebsocketWrapper (#117672)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored May 20, 2024
1 parent 99565be commit ac3321c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
8 changes: 8 additions & 0 deletions homeassistant/components/mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,14 @@ def _async_start_misc_loop(self) -> None:

def _increase_socket_buffer_size(self, sock: SocketType) -> None:
"""Increase the socket buffer size."""
if not hasattr(sock, "setsockopt") and hasattr(sock, "_socket"):
# The WebsocketWrapper does not wrap setsockopt
# so we need to get the underlying socket
# Remove this once
# https://github.com/eclipse/paho.mqtt.python/pull/843
# is available.
sock = sock._socket # noqa: SLF001

new_buffer_size = PREFERRED_BUFFER_SIZE
while True:
try:
Expand Down
37 changes: 37 additions & 0 deletions tests/components/mqtt/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4434,6 +4434,43 @@ async def test_server_sock_buffer_size(
assert "Unable to increase the socket buffer size" in caplog.text


@patch("homeassistant.components.mqtt.client.INITIAL_SUBSCRIBE_COOLDOWN", 0.0)
@patch("homeassistant.components.mqtt.client.DISCOVERY_COOLDOWN", 0.0)
@patch("homeassistant.components.mqtt.client.SUBSCRIBE_COOLDOWN", 0.0)
async def test_server_sock_buffer_size_with_websocket(
hass: HomeAssistant,
mqtt_client_mock: MqttMockPahoClient,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test handling the socket buffer size fails."""
mqtt_mock = await mqtt_mock_entry()
await hass.async_block_till_done()
assert mqtt_mock.connected is True

mqtt_client_mock.loop_misc.return_value = paho_mqtt.MQTT_ERR_SUCCESS

client, server = socket.socketpair(
family=socket.AF_UNIX, type=socket.SOCK_STREAM, proto=0
)
client.setblocking(False)
server.setblocking(False)

class FakeWebsocket(paho_mqtt.WebsocketWrapper):
def _do_handshake(self, *args, **kwargs):
pass

wrapped_socket = FakeWebsocket(client, "127.0.01", 1, False, "/", None)

with patch.object(client, "setsockopt", side_effect=OSError("foo")):
mqtt_client_mock.on_socket_open(mqtt_client_mock, None, wrapped_socket)
mqtt_client_mock.on_socket_register_write(
mqtt_client_mock, None, wrapped_socket
)
await hass.async_block_till_done()
assert "Unable to increase the socket buffer size" in caplog.text


@patch("homeassistant.components.mqtt.client.INITIAL_SUBSCRIBE_COOLDOWN", 0.0)
@patch("homeassistant.components.mqtt.client.DISCOVERY_COOLDOWN", 0.0)
@patch("homeassistant.components.mqtt.client.SUBSCRIBE_COOLDOWN", 0.0)
Expand Down

0 comments on commit ac3321c

Please sign in to comment.