Skip to content
Draft
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
75 changes: 67 additions & 8 deletions src/api/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,9 @@
provider.expect_initialize().returning(|_| {}).once();

let mut api = OpenFeature::default();
api.set_provider(provider).await;

Check warning on line 195 in src/api/api.rs

View workflow job for this annotation

GitHub Actions / lint

Diff in /home/runner/work/rust-sdk/rust-sdk/src/api/api.rs
}

#[spec(
number = "1.1.2.3",
text = "The provider mutator function MUST invoke the shutdown function on the previously registered provider once it's no longer being used to resolve flag values."
)]
#[test]
fn invoke_shutdown_on_old_provider_checked_by_type_system() {}

#[spec(
number = "1.1.3",
Expand Down Expand Up @@ -320,13 +314,78 @@
text = "The API MUST define a shutdown function which, when called, must call the respective shutdown function on the active provider."
)]
#[tokio::test]
async fn shutdown() {
async fn shutdown_calls_provider_shutdown() {
let mut provider = MockFeatureProvider::new();
provider.expect_initialize().returning(|_| {});
provider.expect_shutdown().once().returning(|| {});

let mut api = OpenFeature::default();
api.set_provider(NoOpProvider::default()).await;
api.set_provider(provider).await;

api.shutdown().await;
}

#[spec(
number = "1.6.1",
text = "The API MUST define a shutdown function which, when called, must call the respective shutdown function on the active provider."
)]
#[tokio::test]
async fn shutdown_calls_shutdown_on_named_providers() {
let mut default_provider = MockFeatureProvider::new();
default_provider.expect_initialize().returning(|_| {});
default_provider.expect_shutdown().once().returning(|| {});

let mut named_provider = MockFeatureProvider::new();
named_provider.expect_initialize().returning(|_| {});
named_provider.expect_shutdown().once().returning(|| {});

let mut api = OpenFeature::default();
api.set_provider(default_provider).await;
api.set_named_provider("test", named_provider).await;

api.shutdown().await;
}

#[spec(
number = "1.1.2.3",
text = "The provider mutator function MUST invoke the shutdown function on the previously registered provider once it's no longer being used to resolve flag values."
)]
#[tokio::test]
async fn set_provider_calls_shutdown_on_old_provider() {
let mut old_provider = MockFeatureProvider::new();
old_provider.expect_initialize().returning(|_| {});
old_provider.expect_shutdown().once().returning(|| {});

let mut new_provider = MockFeatureProvider::new();
new_provider.expect_initialize().returning(|_| {});

let mut api = OpenFeature::default();
api.set_provider(old_provider).await;

// When we set a new provider, the old one's shutdown should be called
api.set_provider(new_provider).await;
}

#[spec(
number = "1.1.2.3",
text = "The provider mutator function MUST invoke the shutdown function on the previously registered provider once it's no longer being used to resolve flag values."
)]
#[tokio::test]
async fn set_named_provider_calls_shutdown_on_old_provider() {
let mut old_provider = MockFeatureProvider::new();
old_provider.expect_initialize().returning(|_| {});
old_provider.expect_shutdown().once().returning(|| {});

let mut new_provider = MockFeatureProvider::new();
new_provider.expect_initialize().returning(|_| {});

let mut api = OpenFeature::default();
api.set_named_provider("test", old_provider).await;

// When we set a new provider with the same name, the old one's shutdown should be called
api.set_named_provider("test", new_provider).await;
}

#[spec(
number = "3.2.1.1",
text = "The API, Client and invocation MUST have a method for supplying evaluation context."
Expand Down
22 changes: 15 additions & 7 deletions src/api/provider_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,25 @@ impl ProviderRegistry {
}

pub async fn set_default<T: FeatureProvider>(&self, mut provider: T) {
let mut map = self.providers.write().await;
map.remove("");
// Shutdown the old provider before replacing it.
if let Some(old_provider) = self.providers.write().await.remove("") {
old_provider.get().shutdown().await;
}

provider
.initialize(self.global_evaluation_context.get().await.borrow())
.await;

map.insert(String::default(), FeatureProviderWrapper::new(provider));
self.providers
.write()
.await
.insert(String::default(), FeatureProviderWrapper::new(provider));
}
Comment on lines 34 to 48

Choose a reason for hiding this comment

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

critical

There's a potential race condition here. The provider map is unlocked between removing the old provider and inserting the new one. This could lead to issues if set_default is called concurrently from multiple threads. For example, one thread could set a provider that is immediately overwritten by another, and the first provider's shutdown method would never be called.

To fix this, you can perform the replacement atomically using HashMap::insert, which returns the old value. This also has the benefit of initializing the new provider before it's inserted into the registry.

Suggested change
pub async fn set_default<T: FeatureProvider>(&self, mut provider: T) {
let mut map = self.providers.write().await;
map.remove("");
// Shutdown the old provider before replacing it.
if let Some(old_provider) = self.providers.write().await.remove("") {
old_provider.get().shutdown().await;
}
provider
.initialize(self.global_evaluation_context.get().await.borrow())
.await;
map.insert(String::default(), FeatureProviderWrapper::new(provider));
self.providers
.write()
.await
.insert(String::default(), FeatureProviderWrapper::new(provider));
}
pub async fn set_default<T: FeatureProvider>(&self, mut provider: T) {
provider
.initialize(self.global_evaluation_context.get().await.borrow())
.await;
let old_provider = self
.providers
.write()
.await
.insert(String::default(), FeatureProviderWrapper::new(provider));
if let Some(old_provider) = old_provider {
old_provider.get().shutdown().await;
}
}


pub async fn set_named<T: FeatureProvider>(&self, name: &str, mut provider: T) {
// Drop the already registered provider if any.
if self.get_named(name).await.is_some() {
self.providers.write().await.remove(name);
// Shutdown the old provider before replacing it.
if let Some(old_provider) = self.providers.write().await.remove(name) {
old_provider.get().shutdown().await;
}

provider
Expand Down Expand Up @@ -74,7 +79,10 @@ impl ProviderRegistry {
}

pub async fn clear(&self) {
self.providers.write().await.clear();
let providers: Vec<_> = self.providers.write().await.drain().collect();
for (_, provider) in providers {
provider.get().shutdown().await;
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/provider/feature_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ pub trait FeatureProvider: Send + Sync + 'static {
flag_key: &str,
evaluation_context: &EvaluationContext,
) -> EvaluationResult<ResolutionDetails<StructValue>>;

/// The provider MAY define a shutdown function which performs any cleanup necessary
/// before the provider is disposed of. This may include flushing telemetry events,
/// closing connections, or other resource cleanup.
///
/// See [spec 2.5.1](https://openfeature.dev/specification/sections/providers#requirement-251).
async fn shutdown(&self) {}
}

// ============================================================
Expand Down
8 changes: 6 additions & 2 deletions src/provider/no_op_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ mod tests {
number = "2.5.1",
text = "The provider MAY define a mechanism to gracefully shutdown and dispose of resources."
)]
#[test]
fn shutdown_covered_by_drop_trait() {}
#[tokio::test]
async fn shutdown() {
let provider = NoOpProvider::default();
// NoOpProvider has a default empty shutdown implementation
provider.shutdown().await;
}
}
Loading