-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
chore(watch): simplify watch data synchronization #9154
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
use std::{cell::RefCell, collections::HashSet, sync::Arc}; | ||
use std::{ | ||
collections::HashSet, | ||
ops::DerefMut as _, | ||
sync::{Arc, Mutex}, | ||
}; | ||
|
||
use futures::StreamExt; | ||
use miette::{Diagnostic, SourceSpan}; | ||
use thiserror::Error; | ||
use tokio::{ | ||
select, | ||
sync::{Mutex, Notify}, | ||
task::JoinHandle, | ||
}; | ||
use tokio::{select, sync::Notify, task::JoinHandle}; | ||
use tracing::{instrument, trace}; | ||
use turborepo_repository::package_graph::PackageName; | ||
use turborepo_telemetry::events::command::CommandEventBuilder; | ||
|
@@ -172,14 +172,14 @@ impl WatchClient { | |
// We explicitly use a tokio::sync::Mutex here to avoid deadlocks. | ||
// If we used a std::sync::Mutex, we could deadlock by spinning the lock | ||
// and not yielding back to the tokio runtime. | ||
let changed_packages = Mutex::new(RefCell::new(ChangedPackages::default())); | ||
let changed_packages = Mutex::new(ChangedPackages::default()); | ||
let notify_run = Arc::new(Notify::new()); | ||
let notify_event = notify_run.clone(); | ||
|
||
let event_fut = async { | ||
while let Some(event) = events.next().await { | ||
let event = event?; | ||
Self::handle_change_event(&changed_packages, event.event.unwrap()).await?; | ||
Self::handle_change_event(&changed_packages, event.event.unwrap())?; | ||
notify_event.notify_one(); | ||
} | ||
|
||
|
@@ -189,9 +189,13 @@ impl WatchClient { | |
let run_fut = async { | ||
loop { | ||
notify_run.notified().await; | ||
let changed_packages_guard = changed_packages.lock().await; | ||
if !changed_packages_guard.borrow().is_empty() { | ||
let changed_packages = changed_packages_guard.take(); | ||
let some_changed_packages = { | ||
let mut changed_packages_guard = | ||
changed_packages.lock().expect("poisoned lock"); | ||
(!changed_packages_guard.is_empty()) | ||
.then(|| std::mem::take(changed_packages_guard.deref_mut())) | ||
}; | ||
if let Some(changed_packages) = some_changed_packages { | ||
self.execute_run(changed_packages).await?; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
@@ -214,8 +218,8 @@ impl WatchClient { | |
} | ||
|
||
#[instrument(skip(changed_packages))] | ||
async fn handle_change_event( | ||
changed_packages: &Mutex<RefCell<ChangedPackages>>, | ||
fn handle_change_event( | ||
changed_packages: &Mutex<ChangedPackages>, | ||
event: proto::package_change_event::Event, | ||
) -> Result<(), Error> { | ||
// Should we recover here? | ||
|
@@ -225,7 +229,7 @@ impl WatchClient { | |
}) => { | ||
let package_name = PackageName::from(package_name); | ||
|
||
match changed_packages.lock().await.get_mut() { | ||
match changed_packages.lock().expect("poisoned lock").deref_mut() { | ||
ChangedPackages::All => { | ||
// If we've already changed all packages, ignore | ||
} | ||
|
@@ -235,7 +239,7 @@ impl WatchClient { | |
} | ||
} | ||
proto::package_change_event::Event::RediscoverPackages(_) => { | ||
*changed_packages.lock().await.get_mut() = ChangedPackages::All; | ||
*changed_packages.lock().expect("poisoned lock") = ChangedPackages::All; | ||
} | ||
proto::package_change_event::Event::Error(proto::PackageChangeError { message }) => { | ||
return Err(DaemonError::Unavailable(message).into()); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only acquire the guard in the block so we ensure we release the lock once we take the changed packages. This lets the
event_fut
make progress whileself.execute_run
is pending.