@@ -386,7 +386,7 @@ impl TlsBackend {
386
386
) -> anyhow:: Result < ( ) > {
387
387
let client = match self {
388
388
#[ cfg( feature = "reqwest-rustls-tls" ) ]
389
- Self :: Rustls => & reqwest_be:: CLIENT_RUSTLS_TLS ,
389
+ Self :: Rustls => reqwest_be:: rustls_client ( ) ? ,
390
390
#[ cfg( feature = "reqwest-native-tls" ) ]
391
391
Self :: NativeTls => & reqwest_be:: CLIENT_NATIVE_TLS ,
392
392
} ;
@@ -523,10 +523,10 @@ mod curl {
523
523
#[ cfg( any( feature = "reqwest-rustls-tls" , feature = "reqwest-native-tls" ) ) ]
524
524
mod reqwest_be {
525
525
use std:: io;
526
- #[ cfg( feature = "reqwest-rustls-tls" ) ]
527
- use std:: sync:: Arc ;
528
- #[ cfg( any( feature = "reqwest-rustls-tls" , feature = "reqwest-native-tls" ) ) ]
526
+ #[ cfg( feature = "reqwest-native-tls" ) ]
529
527
use std:: sync:: LazyLock ;
528
+ #[ cfg( feature = "reqwest-rustls-tls" ) ]
529
+ use std:: sync:: { Arc , OnceLock } ;
530
530
use std:: time:: Duration ;
531
531
532
532
use anyhow:: { Context , anyhow} ;
@@ -587,30 +587,36 @@ mod reqwest_be {
587
587
}
588
588
589
589
#[ cfg( feature = "reqwest-rustls-tls" ) ]
590
- pub ( super ) static CLIENT_RUSTLS_TLS : LazyLock < Client > = LazyLock :: new ( || {
590
+ pub ( super ) fn rustls_client ( ) -> Result < & ' static Client , DownloadError > {
591
+ if let Some ( client) = CLIENT_RUSTLS_TLS . get ( ) {
592
+ return Ok ( client) ;
593
+ }
594
+
591
595
let mut tls_config =
592
596
rustls:: ClientConfig :: builder_with_provider ( Arc :: new ( aws_lc_rs:: default_provider ( ) ) )
593
597
. with_safe_default_protocol_versions ( )
594
598
. unwrap ( )
595
599
. with_platform_verifier ( )
600
+ . map_err ( |err| {
601
+ DownloadError :: Message ( format ! ( "failed to initialize platform verifier: {err}" ) )
602
+ } ) ?
596
603
. with_no_client_auth ( ) ;
597
604
tls_config. alpn_protocols = vec ! [ b"h2" . to_vec( ) , b"http/1.1" . to_vec( ) ] ;
598
605
599
- let catcher = || {
600
- client_generic ( )
601
- . use_preconfigured_tls ( tls_config)
602
- . user_agent ( super :: REQWEST_RUSTLS_TLS_USER_AGENT )
603
- . build ( )
604
- } ;
606
+ let client = client_generic ( )
607
+ . use_preconfigured_tls ( tls_config)
608
+ . user_agent ( super :: REQWEST_RUSTLS_TLS_USER_AGENT )
609
+ . build ( )
610
+ . map_err ( DownloadError :: Reqwest ) ?;
605
611
606
- // woah, an unwrap?!
607
- // It's OK. This is the same as what is happening in curl.
608
- //
609
- // The curl::Easy::new() internally assert!s that the initialized
610
- // Easy is not null. Inside reqwest, the errors here would be from
611
- // the TLS library returning a null pointer as well.
612
- catcher ( ) . unwrap ( )
613
- } ) ;
612
+ let _ = CLIENT_RUSTLS_TLS . set ( client ) ;
613
+ // "The cell is guaranteed to contain a value when `set` returns, though not necessarily
614
+ // the one provided."
615
+ Ok ( CLIENT_RUSTLS_TLS . get ( ) . unwrap ( ) )
616
+ }
617
+
618
+ # [ cfg ( feature = "reqwest-rustls-tls" ) ]
619
+ static CLIENT_RUSTLS_TLS : OnceLock < Client > = OnceLock :: new ( ) ;
614
620
615
621
#[ cfg( feature = "reqwest-native-tls" ) ]
616
622
pub ( super ) static CLIENT_NATIVE_TLS : LazyLock < Client > = LazyLock :: new ( || {
0 commit comments