Skip to content
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

fix(core): CONFIG_OVERRIDE mutex poison #3946

Merged
merged 16 commits into from
Nov 13, 2024
Merged
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
42 changes: 25 additions & 17 deletions crates/iota-protocol-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::sync::{
RwLock,
atomic::{AtomicBool, Ordering},
use std::{
cell::RefCell,
sync::atomic::{AtomicBool, Ordering},
};

use clap::*;
Expand Down Expand Up @@ -1083,14 +1083,16 @@ impl ProtocolConfig {
let mut ret = Self::get_for_version_impl(version, chain);
ret.version = version;

if let Some(override_fn) = &*CONFIG_OVERRIDE.read().unwrap() {
warn!(
"overriding ProtocolConfig settings with custom settings (you should not see this log outside of tests)"
);
override_fn(version, ret)
} else {
ret
}
CONFIG_OVERRIDE.with(|ovr| {
if let Some(override_fn) = &*ovr.borrow() {
warn!(
"overriding ProtocolConfig settings with custom settings (you should not see this log outside of tests)"
);
override_fn(version, ret)
} else {
ret
}
})
}

/// Get the value ProtocolConfig that are in effect during the given
Expand Down Expand Up @@ -1708,10 +1710,12 @@ impl ProtocolConfig {
pub fn apply_overrides_for_testing(
override_fn: impl Fn(ProtocolVersion, Self) -> Self + Send + Sync + 'static,
) -> OverrideGuard {
let mut option = CONFIG_OVERRIDE.write().unwrap();
assert!(option.is_none(), "config override already present");
*option = Some(Box::new(override_fn));
OverrideGuard
CONFIG_OVERRIDE.with(|ovr| {
let mut cur = ovr.borrow_mut();
assert!(cur.is_none(), "config override already present");
*cur = Some(Box::new(override_fn));
OverrideGuard
})
}
}

Expand Down Expand Up @@ -1760,15 +1764,19 @@ impl ProtocolConfig {

type OverrideFn = dyn Fn(ProtocolVersion, ProtocolConfig) -> ProtocolConfig + Send + Sync;

static CONFIG_OVERRIDE: RwLock<Option<Box<OverrideFn>>> = RwLock::new(None);
thread_local! {
static CONFIG_OVERRIDE: RefCell<Option<Box<OverrideFn>>> = const { RefCell::new(None) };
}

#[must_use]
pub struct OverrideGuard;

impl Drop for OverrideGuard {
fn drop(&mut self) {
info!("restoring override fn");
*CONFIG_OVERRIDE.write().unwrap() = None;
CONFIG_OVERRIDE.with(|ovr| {
*ovr.borrow_mut() = None;
});
}
}

Expand Down
Loading