Skip to content

Commit

Permalink
feat: prevent duplicate providers from being created
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Nov 18, 2024
1 parent 708aabf commit 6826f42
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/desktop/src/providers/provider_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ impl ProviderManager {
};
}

// Hold the lock for `provider_refs` to prevent duplicate providers
// from potentially being created.
let mut provider_refs = self.provider_refs.lock().await;

// No-op if the provider has already been created (but has not emitted
// yet). Multiple frontend clients can call `create` for the same
// provider, and all will receive the same output once the provider
// emits.
if provider_refs.contains_key(&config_hash) {
return Ok(());
}

let (async_input_tx, async_input_rx) = mpsc::channel(1);
let (sync_input_tx, sync_input_rx) = crossbeam::channel::bounded(1);

Expand All @@ -185,8 +197,7 @@ impl ProviderManager {
runtime_type,
};

let mut providers = self.provider_refs.lock().await;
providers.insert(config_hash, provider_ref);
provider_refs.insert(config_hash, provider_ref);

Ok(())
}
Expand Down

0 comments on commit 6826f42

Please sign in to comment.