Skip to content

Commit

Permalink
fix: fix TypeError on invalid username and password combination
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikaalund committed Mar 11, 2021
1 parent 4b2579a commit f0008c7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ asyncio-mqtt combines the stability of the time-proven [paho-mqtt](https://githu
* Fully type-hinted
* Did we mention no more callbacks?

The whole thing is less than [500 lines of code](https://github.com/sbtinstruments/asyncio-mqtt/blob/master/asyncio_mqtt/client.py).
The whole thing is less than [600 lines of code](https://github.com/sbtinstruments/asyncio-mqtt/blob/master/asyncio_mqtt/client.py).

## Installation 📚

Expand Down
22 changes: 19 additions & 3 deletions asyncio_mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Optional,
Tuple,
Type,
Union,
cast,
)

Expand Down Expand Up @@ -155,10 +156,8 @@ async def connect(self, *, timeout: int = 10) -> None:
await loop.run_in_executor(
None, self._client.connect, self._hostname, self._port, 60
)
# paho.mqttClient.socket() return non-None after the call to connect.
client_socket = self._client.socket()
if not isinstance(client_socket, mqtt.WebsocketWrapper):
client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2048)
_set_client_socket_defaults(client_socket)
# paho.mqtt.Client.connect may raise one of several exceptions.
# We convert all of them to the common MqttError for user convenience.
# See: https://github.com/eclipse/paho.mqtt.python/blob/v1.5.0/src/paho/mqtt/client.py#L1770
Expand Down Expand Up @@ -500,3 +499,20 @@ async def __aexit__(
f'Could not gracefully disconnect due to "{error}". Forcing disconnection.'
)
await self.force_disconnect()


_PahoSocket = Union[socket.socket, mqtt.WebsocketWrapper]


def _set_client_socket_defaults(client_socket: Optional[_PahoSocket]) -> None:
# Note that socket may be None if, e.g., the username and
# password combination didn't work. In this case, we return early.
if client_socket is None:
return
# Furthermore, paho sometimes gives us a socket wrapper instead of
# the raw socket. E.g., for WebSocket-based connections.
if isinstance(client_socket, socket.socket):
return
# At this point, we know that we got an actual socket. We change
# some of the default options.
client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2048)

0 comments on commit f0008c7

Please sign in to comment.