@@ -138,11 +138,11 @@ pub async fn connect_tcp<Ws: WithSocket>(
138138 // })
139139 // }
140140 // };
141- eprintln ! ( "wasip3: creating tcp socket for port {}" , port) ;
141+ debug ! ( "wasip3: creating tcp socket for port {}" , port) ;
142142 let sock =
143143 wasip3:: sockets:: types:: TcpSocket :: create ( wasip3:: sockets:: types:: IpAddressFamily :: Ipv4 )
144144 . expect ( "failed to create TCP socket" ) ;
145- eprintln ! ( "wasip3: created tcp socket for port {}" , port) ;
145+ debug ! ( "wasip3: created tcp socket for port {}" , port) ;
146146 sock. connect ( wasip3:: sockets:: types:: IpSocketAddress :: Ipv4 (
147147 wasip3:: sockets:: types:: Ipv4SocketAddress {
148148 address : ( 127 , 0 , 0 , 1 ) ,
@@ -151,7 +151,7 @@ pub async fn connect_tcp<Ws: WithSocket>(
151151 ) )
152152 . await
153153 . map_err ( |e| {
154- eprintln ! ( "wasip3: connect failed: {:?}" , e) ;
154+ debug ! ( "wasip3: connect failed: {:?}" , e) ;
155155 e
156156 } )
157157 . expect ( & format ! ( "failed to connect to 127.0.0.1:{port}" ) ) ;
@@ -160,57 +160,110 @@ pub async fn connect_tcp<Ws: WithSocket>(
160160 let ( rx_tx, rx_rx) = mpsc:: channel :: < Vec < u8 > > ( 1 ) ;
161161 let ( tx_tx, mut tx_rx) = mpsc:: channel :: < Vec < u8 > > ( 1 ) ;
162162 let ( mut send_tx, send_rx) = wasip3:: wit_stream:: new ( ) ;
163- eprintln ! ( "wasip3: created wit_stream for send/recv" ) ;
163+ debug ! ( "wasip3: created wit_stream for send/recv" ) ;
164164 let ( mut recv_rx, recv_fut) = sock. receive ( ) ;
165165
166166 // Spawn a background task using the wasip3 async runtime and make it abortable.
167167 let ( abort_handle, abort_registration) = AbortHandle :: new_pair ( ) ;
168-
168+ // Give the wasip3 scheduler a quick yield before spawning the background
169+ // task. Use the host-aware `yield_async` so spawned tasks are eligible to
170+ // be polled promptly by the local runtime.
171+ async_support:: yield_async ( ) . await ;
169172 let background = Abortable :: new (
170173 async move {
171174 let sock = Arc :: new ( sock) ;
172- eprintln ! ( "wasip3: background task starting; sock arc cloned" ) ;
175+ debug ! ( "wasip3: background task starting; sock arc cloned" ) ;
173176
174177 let ( ready_tx, ready_rx) = oneshot:: channel ( ) ;
178+ let spawn_ts = std:: time:: SystemTime :: now ( )
179+ . duration_since ( std:: time:: UNIX_EPOCH )
180+ . map ( |d| d. as_millis ( ) )
181+ . unwrap_or_default ( ) ;
182+ debug ! ( "wasip3: spawning sock.send task at {}ms" , spawn_ts) ;
183+
175184 async_support:: spawn ( {
176185 let sock = Arc :: clone ( & sock) ;
177186 async move {
178- eprintln ! ( "wasip3: starting sock.send task" ) ;
187+ let start_ts = std:: time:: SystemTime :: now ( )
188+ . duration_since ( std:: time:: UNIX_EPOCH )
189+ . map ( |d| d. as_millis ( ) )
190+ . unwrap_or_default ( ) ;
191+ debug ! ( "wasip3: sock.send task started at {}ms" , start_ts) ;
179192 let fut = sock. send ( send_rx) ;
193+ let sig_ts = std:: time:: SystemTime :: now ( )
194+ . duration_since ( std:: time:: UNIX_EPOCH )
195+ . map ( |d| d. as_millis ( ) )
196+ . unwrap_or_default ( ) ;
180197 _ = ready_tx. send ( ( ) ) ;
198+ debug ! ( "wasip3: sock.send signalled ready at {}ms" , sig_ts) ;
181199 match fut. await {
182- Ok ( _) => eprintln ! ( "wasip3: sock.send completed" ) ,
183- Err ( e) => eprintln ! ( "wasip3: sock.send error: {:?}" , e) ,
200+ Ok ( _) => {
201+ let done_ts = std:: time:: SystemTime :: now ( )
202+ . duration_since ( std:: time:: UNIX_EPOCH )
203+ . map ( |d| d. as_millis ( ) )
204+ . unwrap_or_default ( ) ;
205+ debug ! ( "wasip3: sock.send completed at {}ms" , done_ts) ;
206+ }
207+ Err ( e) => {
208+ let err_ts = std:: time:: SystemTime :: now ( )
209+ . duration_since ( std:: time:: UNIX_EPOCH )
210+ . map ( |d| d. as_millis ( ) )
211+ . unwrap_or_default ( ) ;
212+ debug ! ( "wasip3: sock.send error at {}ms: {:?}" , err_ts, e) ;
213+ }
184214 }
185215 drop ( sock) ;
186216 }
187217 } ) ;
218+ // Yield after spawning the send task so the runtime can poll it.
219+ async_support:: yield_async ( ) . await ;
188220 async_support:: spawn ( {
189221 let sock = Arc :: clone ( & sock) ;
190222 async move {
191- eprintln ! ( "wasip3: starting recv_fut task" ) ;
223+ let start_ts = std:: time:: SystemTime :: now ( )
224+ . duration_since ( std:: time:: UNIX_EPOCH )
225+ . map ( |d| d. as_millis ( ) )
226+ . unwrap_or_default ( ) ;
227+ debug ! ( "wasip3: recv_fut task started at {}ms" , start_ts) ;
192228 match recv_fut. await {
193- Ok ( _) => eprintln ! ( "wasip3: recv_fut completed" ) ,
194- Err ( e) => eprintln ! ( "wasip3: recv_fut error: {:?}" , e) ,
229+ Ok ( _) => {
230+ let done_ts = std:: time:: SystemTime :: now ( )
231+ . duration_since ( std:: time:: UNIX_EPOCH )
232+ . map ( |d| d. as_millis ( ) )
233+ . unwrap_or_default ( ) ;
234+ debug ! ( "wasip3: recv_fut completed at {}ms" , done_ts) ;
235+ }
236+ Err ( e) => {
237+ let err_ts = std:: time:: SystemTime :: now ( )
238+ . duration_since ( std:: time:: UNIX_EPOCH )
239+ . map ( |d| d. as_millis ( ) )
240+ . unwrap_or_default ( ) ;
241+ debug ! ( "wasip3: recv_fut error at {}ms: {:?}" , err_ts, e) ;
242+ }
195243 }
196244 drop ( sock) ;
197245 }
198246 } ) ;
247+ // Yield to the wasip3 scheduler to give the spawned tasks a chance
248+ // to be polled immediately. Without this yield the local runtime
249+ // may not poll newly spawned tasks until the current task yields,
250+ // which can cause head-of-line blocking observed during handshakes.
251+ async_support:: yield_async ( ) . await ;
199252 futures_util:: join!(
200253 async {
201254 while let Some ( result) = recv_rx. next( ) . await {
202255 // `recv_rx` yields single bytes from the wasip3 receive stream.
203- eprintln !( "wasip3: recv_rx.next yielded byte: {:#x}" , result) ;
256+ debug !( "wasip3: recv_rx.next yielded byte: {:#x}" , result) ;
204257 _ = rx_tx. send( vec![ result] ) . await ;
205258 }
206259 drop( recv_rx) ;
207260 drop( rx_tx) ;
208261 } ,
209262 async {
210263 _ = ready_rx. await ;
211- eprintln !( "wasip3: send task ready, draining tx_rx -> send_tx" ) ;
264+ debug !( "wasip3: send task ready, draining tx_rx -> send_tx" ) ;
212265 while let Some ( buf) = tx_rx. recv( ) . await {
213- eprintln !( "wasip3: writing {} bytes to send_tx" , buf. len( ) ) ;
266+ debug !( "wasip3: writing {} bytes to send_tx" , buf. len( ) ) ;
214267 let _ = send_tx. write( buf) . await ;
215268 }
216269 drop( tx_rx) ;
0 commit comments