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 15, 2023
1 parent c2daa16 commit 1a9f8d6
Show file tree
Hide file tree
Showing 3 changed files with 36 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 @@ -11,7 +11,7 @@ use std::os::raw::{c_char, c_int, c_long};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::Arc;
use std::sync::{Arc, OnceLock};
use std::thread::{self, JoinHandle};
use std::{cmp, mem, ptr};

Expand Down Expand Up @@ -46,6 +46,8 @@ use crate::libgit::{
};
use crate::util::{ImmutBString, OsStrExt, PrefixWriter, ReadExt, SliceExt, ToBoxed};

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

mod git_http_state {
use std::ffi::CString;
use std::ptr;
Expand Down Expand Up @@ -79,6 +81,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
{
crate::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);

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

Please sign in to comment.