@@ -135,6 +135,17 @@ def enact_state_change(self, state: ConnectionState, reason: AblyException | Non
135135 self .__state = state
136136 if reason :
137137 self .__error_reason = reason
138+
139+ # RTN16d: Clear connection state when entering SUSPENDED or terminal states
140+ if state == ConnectionState .SUSPENDED or state in (
141+ ConnectionState .CLOSED ,
142+ ConnectionState .FAILED
143+ ):
144+ self .__connection_details = None
145+ self .connection_id = None
146+ self .__connection_key = None
147+ self .msg_serial = 0
148+
138149 self ._emit ('connectionstate' , ConnectionStateChange (current_state , state , state , reason ))
139150
140151 def check_connection (self ) -> bool :
@@ -157,6 +168,10 @@ async def __get_transport_params(self) -> dict:
157168 # RTN2a: Set format to msgpack if use_binary_protocol is enabled
158169 if self .options .use_binary_protocol :
159170 params ["format" ] = "msgpack"
171+
172+ # Add any custom transport params from options
173+ params .update (self .options .transport_params )
174+
160175 return params
161176
162177 async def close_impl (self ) -> None :
@@ -165,13 +180,23 @@ async def close_impl(self) -> None:
165180 self .cancel_suspend_timer ()
166181 self .start_transition_timer (ConnectionState .CLOSING , fail_state = ConnectionState .CLOSED )
167182 if self .transport :
168- await self .transport .dispose ()
183+ # Try to send protocol CLOSE message in the background
184+ asyncio .create_task (self .transport .close ())
185+ # Yield to event loop to give the close message a chance to send
186+ await asyncio .sleep (0 )
187+ await self .transport .dispose () # Dispose transport resources
169188 if self .connect_base_task :
170189 self .connect_base_task .cancel ()
171190 if self .disconnect_transport_task :
172191 await self .disconnect_transport_task
173192 self .cancel_retry_timer ()
174193
194+ # Clear connection details to prevent resume on next connect
195+ # When explicitly closed, we want a fresh connection, not a resume
196+ self .__connection_details = None
197+ self .connection_id = None
198+ self .msg_serial = 0
199+
175200 self .notify_state (ConnectionState .CLOSED )
176201
177202 async def send_protocol_message (self , protocol_message : dict ) -> None :
@@ -648,7 +673,6 @@ def on_suspend_timer_expire() -> None:
648673 AblyException ("Connection to server unavailable" , 400 , 80002 )
649674 )
650675 self .__fail_state = ConnectionState .SUSPENDED
651- self .__connection_details = None
652676
653677 self .suspend_timer = Timer (Defaults .connection_state_ttl , on_suspend_timer_expire )
654678
0 commit comments