@@ -39,7 +39,7 @@ use crate::{
3939} ;
4040
4141use address:: { scores, AddressStore } ;
42- use futures:: { Stream , StreamExt } ;
42+ use futures:: { future :: BoxFuture , Stream , StreamExt } ;
4343use indexmap:: IndexMap ;
4444use multiaddr:: { Multiaddr , Protocol } ;
4545use multihash:: Multihash ;
@@ -252,6 +252,11 @@ pub struct TransportManager {
252252
253253 /// Opening connections errors.
254254 opening_errors : HashMap < ConnectionId , Vec < ( Multiaddr , DialError ) > > ,
255+
256+ /// Pending accept future with associated connection information.
257+ /// When a connection is accepted, we must wait for the accept future to complete
258+ /// (which notifies all protocols) before emitting the ConnectionEstablished event.
259+ pending_accept : Option < ( PeerId , Endpoint , BoxFuture < ' static , crate :: Result < ( ) > > ) > ,
255260}
256261
257262/// Builder for [`crate::transport::manager::TransportManager`].
@@ -365,6 +370,7 @@ impl TransportManagerBuilder {
365370 pending_connections : HashMap :: new ( ) ,
366371 connection_limits : limits:: ConnectionLimits :: new ( self . connection_limits_config ) ,
367372 opening_errors : HashMap :: new ( ) ,
373+ pending_accept : None ,
368374 }
369375 }
370376}
@@ -1090,6 +1096,35 @@ impl TransportManager {
10901096 /// Poll next event from [`crate::transport::manager::TransportManager`].
10911097 pub async fn next ( & mut self ) -> Option < TransportEvent > {
10921098 loop {
1099+ // First, check if we have a pending accept future to poll
1100+ if let Some ( ( peer, endpoint, mut future) ) = self . pending_accept . take ( ) {
1101+ match future. as_mut ( ) . await {
1102+ Ok ( ( ) ) => {
1103+ tracing:: trace!(
1104+ target: LOG_TARGET ,
1105+ ?peer,
1106+ ?endpoint,
1107+ "connection accepted and protocols notified" ,
1108+ ) ;
1109+
1110+ return Some ( TransportEvent :: ConnectionEstablished {
1111+ peer,
1112+ endpoint,
1113+ } ) ;
1114+ }
1115+ Err ( error) => {
1116+ tracing:: debug!(
1117+ target: LOG_TARGET ,
1118+ ?peer,
1119+ ?endpoint,
1120+ ?error,
1121+ "failed to notify protocols about connection" ,
1122+ ) ;
1123+ // If notification failed, we don't emit the ConnectionEstablished event
1124+ }
1125+ }
1126+ }
1127+
10931128 tokio:: select! {
10941129 event = self . event_rx. recv( ) => {
10951130 let Some ( event) = event else {
@@ -1270,16 +1305,27 @@ impl TransportManager {
12701305 "accept connection" ,
12711306 ) ;
12721307
1273- let _ = self
1308+ match self
12741309 . transports
12751310 . get_mut( & transport)
12761311 . expect( "transport to exist" )
1277- . accept( endpoint. connection_id( ) ) ;
1278-
1279- return Some ( TransportEvent :: ConnectionEstablished {
1280- peer,
1281- endpoint,
1282- } ) ;
1312+ . accept( endpoint. connection_id( ) )
1313+ {
1314+ Ok ( future) => {
1315+ // Store the accept future to be polled in the next iteration
1316+ // This ensures protocols are notified before we emit ConnectionEstablished
1317+ self . pending_accept = Some ( ( peer, endpoint, future) ) ;
1318+ }
1319+ Err ( error) => {
1320+ tracing:: debug!(
1321+ target: LOG_TARGET ,
1322+ ?peer,
1323+ ?endpoint,
1324+ ?error,
1325+ "failed to accept connection" ,
1326+ ) ;
1327+ }
1328+ }
12831329 }
12841330 Ok ( ConnectionEstablishedResult :: Reject ) => {
12851331 tracing:: trace!(
0 commit comments