@@ -492,6 +492,7 @@ def __init__(self, client_id="", clean_session=True, userdata=None, protocol=MQT
492492 self ._msgtime_mutex = threading .Lock ()
493493 self ._out_message_mutex = threading .Lock ()
494494 self ._in_message_mutex = threading .Lock ()
495+ self ._mid_generate_mutex = threading .Lock ()
495496 self ._thread = None
496497 self ._thread_terminate = False
497498 self ._ssl = None
@@ -836,7 +837,8 @@ def reconnect(self):
836837
837838 self ._ssl = ssl_context .wrap_socket (sock )
838839 else :
839- self ._ssl = ssl .wrap_socket (
840+ # ssl.wrap_socket is deprecated in Python 3.10+
841+ self ._ssl = ssl .SSLContext .wrap_socket (
840842 sock ,
841843 certfile = self ._tls_certfile ,
842844 keyfile = self ._tls_keyfile ,
@@ -1050,8 +1052,11 @@ def username_pw_set(self, username, password=None):
10501052 username: The username to authenticate with. Need have no relationship to the client id.
10511053 password: The password to authenticate with. Optional, set to None if not required.
10521054 """
1053- self ._username = username .encode ('utf-8' )
1055+ # [MQTT-3.1.3-11] User name must be UTF-8 encoded string
1056+ self ._username = None if username is None else username .encode ('utf-8' )
10541057 self ._password = password
1058+ if isinstance (self ._password , unicode ):
1059+ self ._password = self ._password .encode ('utf-8' )
10551060
10561061 def socket_factory_set (self , socket_factory ):
10571062 """Set a socket factory to custom configure a different socket type for
@@ -1745,10 +1750,12 @@ def _check_keepalive(self):
17451750 self ._callback_mutex .release ()
17461751
17471752 def _mid_generate (self ):
1748- self ._last_mid = self ._last_mid + 1
1749- if self ._last_mid == 65536 :
1750- self ._last_mid = 1
1751- return self ._last_mid
1753+ # Make sure mid generation that was thread-safe.
1754+ with self ._mid_generate_mutex :
1755+ self ._last_mid += 1
1756+ if self ._last_mid == 65536 :
1757+ self ._last_mid = 1
1758+ return self ._last_mid
17521759
17531760 def _topic_wildcard_len_check (self , topic ):
17541761 # Search for + or # in a topic. Return MQTT_ERR_INVAL if found.
@@ -1912,11 +1919,11 @@ def _send_connect(self, keepalive, clean_session):
19121919 connect_flags = connect_flags | 0x04 | ((self ._will_qos & 0x03 ) << 3 ) | ((self ._will_retain & 0x01 ) << 5 )
19131920
19141921 if self ._username :
1915- remaining_length = remaining_length + 2 + len (self ._username )
1922+ remaining_length += 2 + len (self ._username )
19161923 connect_flags = connect_flags | 0x80
19171924 if self ._password :
19181925 connect_flags = connect_flags | 0x40
1919- remaining_length = remaining_length + 2 + len (self ._password )
1926+ remaining_length += 2 + len (self ._password )
19201927
19211928 command = CONNECT
19221929 packet = bytearray ()
0 commit comments