Skip to content

cargo-credential-libsecret: load libsecret only once #15295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ blake3 = "1.5.5"
build-rs = { version = "0.3.1", path = "crates/build-rs" }
cargo = { path = "" }
cargo-credential = { version = "0.4.2", path = "credential/cargo-credential" }
cargo-credential-libsecret = { version = "0.4.15", path = "credential/cargo-credential-libsecret" }
cargo-credential-libsecret = { version = "0.5.0", path = "credential/cargo-credential-libsecret" }
cargo-credential-macos-keychain = { version = "0.4.15", path = "credential/cargo-credential-macos-keychain" }
cargo-credential-wincred = { version = "0.4.15", path = "credential/cargo-credential-wincred" }
cargo-platform = { path = "crates/cargo-platform", version = "0.3.0" }
Expand Down
2 changes: 1 addition & 1 deletion credential/cargo-credential-libsecret/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-credential-libsecret"
version = "0.4.15"
version = "0.5.0"
rust-version = "1.87" # MSRV:1
edition.workspace = true
license.workspace = true
Expand Down
40 changes: 26 additions & 14 deletions credential/cargo-credential-libsecret/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ mod linux {
...
) -> *mut gchar;

pub struct LibSecretCredential;
pub struct LibSecretCredential {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: its a big help for reviewers to split up commits. In this case, I'd do

  • Version bump
  • Switch to LibSecretCredential owning libsecret
  • Add the OnceLock

I mention this more for future reference to the point that I wasn't going to mention it if there wasn't other feedback to avoid you doing extra churn on this without a reason.

libsecret: Library,
}

fn label(index_url: &str) -> CString {
CString::new(format!("cargo-registry:{}", index_url)).unwrap()
Expand All @@ -105,31 +107,41 @@ mod linux {
}
}

impl Credential for LibSecretCredential {
impl LibSecretCredential {
pub fn new() -> Result<LibSecretCredential, Error> {
// Dynamically load libsecret to avoid users needing to install
// additional -dev packages when building this provider.
let libsecret = unsafe { Library::new("libsecret-1.so.0") }.context(
"failed to load libsecret: try installing the `libsecret` \
or `libsecret-1-0` package with the system package manager",
)?;
Ok(Self { libsecret })
}
}

impl Credential for &LibSecretCredential {
fn perform(
&self,
registry: &RegistryInfo<'_>,
action: &Action<'_>,
_args: &[&str],
) -> Result<CredentialResponse, Error> {
// Dynamically load libsecret to avoid users needing to install
// additional -dev packages when building this provider.
let lib;
let secret_password_lookup_sync: Symbol<'_, SecretPasswordLookupSync>;
let secret_password_store_sync: Symbol<'_, SecretPasswordStoreSync>;
let secret_password_clear_sync: Symbol<'_, SecretPasswordClearSync>;
unsafe {
lib = Library::new("libsecret-1.so.0").context(
"failed to load libsecret: try installing the `libsecret` \
or `libsecret-1-0` package with the system package manager",
)?;
secret_password_lookup_sync = lib
secret_password_lookup_sync = self
.libsecret
.get(b"secret_password_lookup_sync\0")
.map_err(Box::new)?;
secret_password_store_sync =
lib.get(b"secret_password_store_sync\0").map_err(Box::new)?;
secret_password_clear_sync =
lib.get(b"secret_password_clear_sync\0").map_err(Box::new)?;
secret_password_store_sync = self
.libsecret
.get(b"secret_password_store_sync\0")
.map_err(Box::new)?;
secret_password_clear_sync = self
.libsecret
.get(b"secret_password_clear_sync\0")
.map_err(Box::new)?;
}

let index_url_c = CString::new(registry.index_url).unwrap();
Expand Down
23 changes: 22 additions & 1 deletion src/cargo/util/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,27 @@ static BUILT_IN_PROVIDERS: &[&'static str] = &[
"cargo:libsecret",
];

/// Retrieves a cached instance of `LibSecretCredential`.
/// Must be cached to avoid repeated load/unload cycles, which are not supported by `glib`.
#[cfg(target_os = "linux")]
fn get_credential_libsecret(
) -> CargoResult<&'static cargo_credential_libsecret::LibSecretCredential> {
static CARGO_CREDENTIAL_LIBSECRET: std::sync::OnceLock<
cargo_credential_libsecret::LibSecretCredential,
> = std::sync::OnceLock::new();
// Unfortunately `get_or_try_init` is not yet stable. This workaround is not threadsafe but
// loading libsecret twice will only temporary increment the ref counter, which is decrement
// again when `drop` is called.
match CARGO_CREDENTIAL_LIBSECRET.get() {
Some(lib) => Ok(lib),
None => {
let _ = CARGO_CREDENTIAL_LIBSECRET
.set(cargo_credential_libsecret::LibSecretCredential::new()?);
Ok(CARGO_CREDENTIAL_LIBSECRET.get().unwrap())
}
}
}

fn credential_action(
gctx: &GlobalContext,
sid: &SourceId,
Expand Down Expand Up @@ -545,7 +566,7 @@ fn credential_action(
#[cfg(target_os = "macos")]
"cargo:macos-keychain" => Box::new(cargo_credential_macos_keychain::MacKeychain {}),
#[cfg(target_os = "linux")]
"cargo:libsecret" => Box::new(cargo_credential_libsecret::LibSecretCredential {}),
"cargo:libsecret" => Box::new(get_credential_libsecret()?),
name if BUILT_IN_PROVIDERS.contains(&name) => {
Box::new(cargo_credential::UnsupportedCredential {})
}
Expand Down