Skip to content

Commit

Permalink
Only ever call curl_global_cleanup once
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Dec 24, 2023
1 parent 5f4918b commit 3854fb7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/hg_connect_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use curl_sys::{
use either::Either;
use flate2::read::ZlibDecoder;
use itertools::Itertools;
use once_cell::sync::Lazy;
use once_cell::sync::{Lazy, OnceCell};
use url::{form_urlencoded, Url};
use zstd::stream::read::Decoder as ZstdDecoder;

Expand All @@ -50,6 +50,8 @@ use crate::util::{ImmutBString, OsStrExt, PrefixWriter, ReadExt, SliceExt, ToBox

use self::git_http_state::{GitHttpStateToken, GIT_HTTP_STATE};

pub static CURL_GLOBAL_INIT: OnceCell<()> = OnceCell::new();

mod git_http_state {
use std::{ffi::CString, ptr, sync::Mutex};

Expand Down Expand Up @@ -82,6 +84,13 @@ mod git_http_state {
match &self.url {
Some(url) if url == &normalized_url => {}
_ => {
super::CURL_GLOBAL_INIT.get_or_init(|| {
if unsafe { curl_sys::curl_global_init(curl_sys::CURL_GLOBAL_ALL) }
!= curl_sys::CURLE_OK
{
die!("curl_global_init failed");
}
});
let c_url = CString::new(normalized_url.to_string()).unwrap();
unsafe {
if self.url.is_some() {
Expand Down
24 changes: 21 additions & 3 deletions src/http.c.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
diff --git a/http.c b/http.c
index 2a722f6357..c7e0395abd 100644
index 239421a983..8d1086ca4d 100644
--- a/http.c
+++ b/http.c
@@ -63,7 +63,7 @@ static const char *curl_no_proxy;
@@ -68,7 +68,7 @@ static const char *curl_no_proxy;
#ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
static const char *ssl_pinnedkey;
#endif
Expand All @@ -11,7 +11,7 @@ index 2a722f6357..c7e0395abd 100644
static long curl_low_speed_limit = -1;
static long curl_low_speed_time = -1;
static int curl_ftp_no_epsv;
@@ -288,11 +288,13 @@ static int http_options(const char *var, const char *value, void *cb)
@@ -406,11 +406,13 @@ static int http_options(const char *var, const char *value,
curl_ssl_try = git_config_bool(var, value);
return 0;
}
Expand All @@ -25,3 +25,21 @@ index 2a722f6357..c7e0395abd 100644

if (!strcmp("http.schannelcheckrevoke", var)) {
if (value && !strcmp(value, "best-effort")) {
@@ -1300,9 +1302,6 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
}
#endif

- if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
- die("curl_global_init failed");
-
http_proactive_auth = proactive_auth;

if (remote && remote->http_proxy)
@@ -1391,7 +1390,6 @@ void http_cleanup(void)
curl_easy_cleanup(curl_default);

curl_multi_cleanup(curlm);
- curl_global_cleanup();

string_list_clear(&extra_http_headers, 0);

6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4242,6 +4242,12 @@ unsafe extern "C" fn cinnabar_main(_argc: c_int, argv: *const *const c_char) ->
Some(_) | None => Ok(1),
};
done_cinnabar();

if hg_connect_http::CURL_GLOBAL_INIT.get().is_some() {
unsafe {
curl_sys::curl_global_cleanup();
}
}
match ret {
Ok(code) => code,
Err(msg) => {
Expand Down

0 comments on commit 3854fb7

Please sign in to comment.