@@ -464,15 +464,22 @@ private static Task GetHostEntryOrAddressesCoreAsync(string hostName, bool justR
464464 {
465465 ValidateHostName ( hostName ) ;
466466
467+ Task ? t ;
467468 if ( NameResolutionTelemetry . Log . IsEnabled ( ) )
468469 {
469- return justAddresses
470- ? ( Task ) GetAddrInfoWithTelemetryAsync < IPAddress [ ] > ( hostName , justAddresses )
471- : ( Task ) GetAddrInfoWithTelemetryAsync < IPHostEntry > ( hostName , justAddresses ) ;
470+ t = justAddresses
471+ ? ( Task ? ) GetAddrInfoWithTelemetryAsync < IPAddress [ ] > ( hostName , justAddresses )
472+ : ( Task ? ) GetAddrInfoWithTelemetryAsync < IPHostEntry > ( hostName , justAddresses ) ;
472473 }
473474 else
474475 {
475- return NameResolutionPal . GetAddrInfoAsync ( hostName , justAddresses ) ;
476+ t = NameResolutionPal . GetAddrInfoAsync ( hostName , justAddresses ) ;
477+ }
478+
479+ // If async resolution started, return task to user. otherwise fall back to sync API on threadpool.
480+ if ( t != null )
481+ {
482+ return t ;
476483 }
477484 }
478485
@@ -481,20 +488,34 @@ private static Task GetHostEntryOrAddressesCoreAsync(string hostName, bool justR
481488 RunAsync ( s => GetHostEntryCore ( ( string ) s ) , hostName ) ;
482489 }
483490
484- private static async Task < T > GetAddrInfoWithTelemetryAsync < T > ( string hostName , bool justAddresses )
491+ private static Task < T > ? GetAddrInfoWithTelemetryAsync < T > ( string hostName , bool justAddresses )
485492 where T : class
486493 {
487- ValueStopwatch stopwatch = NameResolutionTelemetry . Log . BeforeResolution ( hostName ) ;
494+ ValueStopwatch stopwatch = ValueStopwatch . StartNew ( ) ;
495+ Task ? task = NameResolutionPal . GetAddrInfoAsync ( hostName , justAddresses ) ;
488496
489- T ? result = null ;
490- try
497+ if ( task != null )
491498 {
492- result = await ( ( Task < T > ) NameResolutionPal . GetAddrInfoAsync ( hostName , justAddresses ) ) . ConfigureAwait ( false ) ;
493- return result ;
499+ return CompleteAsync ( task , hostName , stopwatch ) ;
494500 }
495- finally
501+
502+ // If resolution even did not start don't bother with telemetry.
503+ // We will retry on thread-pool.
504+ return null ;
505+
506+ static async Task < T > CompleteAsync ( Task task , string hostName , ValueStopwatch stopwatch )
496507 {
497- NameResolutionTelemetry . Log . AfterResolution ( stopwatch , successful : result is not null ) ;
508+ _ = NameResolutionTelemetry . Log . BeforeResolution ( hostName ) ;
509+ T ? result = null ;
510+ try
511+ {
512+ result = await ( ( Task < T > ) task ) . ConfigureAwait ( false ) ;
513+ return result ;
514+ }
515+ finally
516+ {
517+ NameResolutionTelemetry . Log . AfterResolution ( stopwatch , successful : result is not null ) ;
518+ }
498519 }
499520 }
500521
0 commit comments