diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d0a1fc1..3673d53c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - The DLS will now correctly report `param` declarations directly below a `#if` or `#else` block - The server will no longer send an LSP warning if dml-builtins is not found, instead relying on the client to perform this task - Fixed incorrect handling of explicit in-object constructs, correcting ranges of some error reports and detection of some linting errors +- Fixed an issue where the server would sometimes report `progress end` too early ## 0.9.20 - Made GetKnownContexts custom request not wait for analysis results by default, reducing the chances of overfilling workpool capacity while analysises are running. diff --git a/src/actions/analysis_queue.rs b/src/actions/analysis_queue.rs index 65ce3797..f2d58408 100644 --- a/src/actions/analysis_queue.rs +++ b/src/actions/analysis_queue.rs @@ -33,9 +33,16 @@ use crate::server::ServerToHandle; use crate::logging::{info, debug, trace, error}; use crossbeam::channel; -// Maps in-process device jobs the timestamps of their dependencies -type InFlightDeviceJobTracker = HashMap)>; -type InFlightIsolatedJobTracker = HashMap; +// In-flight jobs are keyed by a per-dispatch id rather than by job hash, since +// several jobs with the same hash can be in flight at once (a cancelled job +// keeps running until it notices) and each must only remove its own entry. +type InFlightJobId = u64; +// Maps in-process device jobs to their hash, device, and the timestamps of +// their dependencies +type InFlightDeviceJobTracker = HashMap)>; +type InFlightIsolatedJobTracker = HashMap; // Queue up analysis tasks and execute them on the same thread (this is slower // than executing in parallel, but allows us to skip indexing tasks). pub struct AnalysisQueue { @@ -51,7 +58,7 @@ pub struct AnalysisQueue { impl AnalysisQueue { // Create a new queue and start the worker thread. - pub fn init(no_more_work: Arc) -> AnalysisQueue { + pub fn init(no_more_work: Arc, server_notify: channel::Sender) -> AnalysisQueue { let queue = Arc::default(); let device_tracker = Arc::default(); let isolated_tracker = Arc::default(); @@ -62,12 +69,13 @@ impl AnalysisQueue { || AnalysisQueue::run_worker_thread(no_more_work, queue, device_tracker, - isolated_tracker) + isolated_tracker, + server_notify) }) .thread() .clone(); - AnalysisQueue { queue, worker_thread, device_tracker, isolated_tracker} + AnalysisQueue { queue, worker_thread, device_tracker, isolated_tracker } } pub fn enqueue_linter_job(&self, @@ -125,29 +133,21 @@ impl AnalysisQueue { device_analysis_options: DeviceAnalysisJobOptions) -> bool { match DeviceAnalysisJob::new(tracking_token, storage, bases, device, device_analysis_options) { Ok(newjob) => { - if let Some((_, previous_bases)) = self.device_tracker - .lock().unwrap() - .get(&newjob.hash) { - let mut newer_bases = false; - for base in &newjob.bases { - // If any base is missing or newer, - // the job is ok to go - if let Some(old_base_timestamp) = - previous_bases.get(&base.stored.path) { - if !timestamp_is_newer(base.timestamp, - *old_base_timestamp) { - continue; - } - } - newer_bases = true; - break; - } - if !newer_bases { - debug!("Skipped enqueueing device analysis job of \ - {:?}, no new dependencies", device); - return false; - } - } + // Skip if an in-flight job already covers these bases, + // i.e. no base is missing from it or newer than in it + let covered = self.device_tracker.lock().unwrap().values() + .filter(|(hash, _, _)| *hash == newjob.hash) + .any(|(_, _, previous_bases)| newjob.bases.iter().all( + |base| previous_bases.get(&base.stored.path) + .is_some_and(|old_base_timestamp| + !timestamp_is_newer( + base.timestamp, + *old_base_timestamp)))); + if covered { + debug!("Skipped enqueueing device analysis job of \ + {:?}, no new dependencies", device); + return false; + } debug!("Enqueued device analysis job of {:?}", device); self.enqueue(newjob.into()); true @@ -177,12 +177,16 @@ impl AnalysisQueue { fn run_worker_thread(no_more_work: Arc, queue: Arc>>, device_tracker: Arc>, - isolated_tracker: Arc>) { + isolated_tracker: Arc>, + server_notify: channel::Sender) { + let mut next_job_id: InFlightJobId = 0; loop { if no_more_work.load(std::sync::atomic::Ordering::SeqCst) { thread::park(); continue; } + let job_id = next_job_id; + next_job_id += 1; let job = { let mut queue = queue.lock().unwrap(); if queue.is_empty() { @@ -195,15 +199,16 @@ impl AnalysisQueue { match queue.first() { Some(QueuedJob::DeviceAnalysisJob(job)) => { device_tracker.lock().unwrap().insert( - job.hash, - (job.root.path.clone(), + job_id, + (job.hash, + job.root.path.clone(), job.bases.iter().map( |base|(base.stored.path.clone(), base.timestamp)).collect())); }, Some(QueuedJob::IsolatedAnalysisJob(job)) => { isolated_tracker.lock().unwrap() - .insert(job.hash, job.path.clone()); + .insert(job_id, job.path.clone()); }, _ => (), } @@ -226,22 +231,24 @@ impl AnalysisQueue { Some(QueuedJob::IsolatedAnalysisJob(job)) => { thread::spawn({ let iso_tracker = Arc::clone(&isolated_tracker); - let hash = job.hash; + let notify = server_notify.clone(); move ||{ - job.process(); - iso_tracker.lock().unwrap().remove(&hash); + let res = job.process(); + iso_tracker.lock().unwrap().remove(&job_id); + notify.send(res).ok(); }}); }, Some(QueuedJob::FileLinterJob(job)) => { - job.process() + server_notify.send(job.process()).ok(); }, Some(QueuedJob::DeviceAnalysisJob(job)) => { thread::spawn({ let dev_tracker = Arc::clone(&device_tracker); - let hash = job.hash; + let notify = server_notify.clone(); move ||{ - job.process(); - dev_tracker.lock().unwrap().remove(&hash); + let res = job.process(); + dev_tracker.lock().unwrap().remove(&job_id); + notify.send(res).ok(); }}); }, Some(QueuedJob::Sentinel) => { @@ -339,7 +346,7 @@ impl AnalysisQueue { } { let device_lock = self.device_tracker.lock().unwrap(); - for (path, _) in device_lock.values() { + for (_, path, _) in device_lock.values() { if paths.contains(path) { trace!("Detected there is still device \ work in-flight on {:?}", @@ -409,7 +416,6 @@ pub struct IsolatedAnalysisJob { client_path: PathBuf, timestamp: SystemTime, report: ResultChannel, - notify: channel::Sender, content: TextFile, context: Option, hash: u64, @@ -438,7 +444,6 @@ impl IsolatedAnalysisJob { client_path, timestamp, report: analysis.report.clone(), - notify: analysis.notify.clone(), hash, context, content, @@ -446,7 +451,7 @@ impl IsolatedAnalysisJob { }) } - fn process(self) { + fn process(self) -> ServerToHandle { info!("Started work on isolated analysis of {}", self.path.as_str()); match IsolatedAnalysis::new(&self.path, &self.client_path, @@ -462,11 +467,11 @@ impl IsolatedAnalysisJob { self.report.send(TimestampedStorage::make_timestamped( self.timestamp, analysis)).ok(); - self.notify.send(ServerToHandle::IsolatedAnalysisDone( + return ServerToHandle::IsolatedAnalysisDone( self.path.clone(), new_context, import_paths - )).ok(); + ); }, Err(AnalysisError::VFSError(e)) => { error!("Failed to create isolated analysis: {}", e); @@ -476,6 +481,7 @@ impl IsolatedAnalysisJob { debug!("Isolated analysis of {} was cancelled", self.path.as_str()); } } + ServerToHandle::AnalysisJobExited } } @@ -487,7 +493,6 @@ pub struct DeviceAnalysisJob { import_sources: HashMap, timestamp: SystemTime, report: ResultChannel, - notify: channel::Sender, hash: u64, token: JobToken, device_analysis_options: DeviceAnalysisJobOptions, @@ -548,14 +553,13 @@ impl DeviceAnalysisJob { timestamp, import_sources, report: analysis.report.clone(), - notify: analysis.notify.clone(), hash, token, device_analysis_options, }) } - fn process(self) { + fn process(self) -> ServerToHandle{ let root_path = self.root.path.clone(); info!("Started work on deviceanalysis of {:?}, depending on {:?}", self.root.path, @@ -568,11 +572,11 @@ impl DeviceAnalysisJob { self.token.status) { Ok(analysis) => { info!("Finished device analysis of {:?}", analysis.name); - self.notify.send(ServerToHandle::DeviceAnalysisDone( - analysis.path.clone())).ok(); + let path = analysis.path.clone(); self.report.send(TimestampedStorage::make_timestamped( self.timestamp, analysis)).ok(); + return ServerToHandle::DeviceAnalysisDone(path); }, // In general, an analysis shouldn't fail to be created Err(AnalysisError::VFSError(e)) => { @@ -583,6 +587,7 @@ impl DeviceAnalysisJob { debug!("Device analysis of {} was cancelled", root_path.as_str()); } } + ServerToHandle::AnalysisJobExited } } @@ -591,7 +596,6 @@ pub struct LinterJob { file: CanonPath, timestamp: SystemTime, report: ResultChannel, - notify: channel::Sender, content: TextFile, hash: u64, ast: IsolatedAnalysis, @@ -617,7 +621,6 @@ impl LinterJob { file: file.to_owned(), timestamp, report: analysis.report.clone(), - notify: analysis.notify.clone(), hash, ast: isolated_analysis.to_owned(), cfg, @@ -629,7 +632,7 @@ impl LinterJob { } } - fn process(self) { + fn process(self) -> ServerToHandle{ debug!("Started work on isolated linting of {:?}", self.file); match LinterAnalysis::new(&self.file, self.content, @@ -640,8 +643,7 @@ impl LinterJob { self.report.send(TimestampedStorage::make_timestamped( self.timestamp, analysis)).ok(); - self.notify.send(ServerToHandle::LinterDone( - self.file.clone())).ok(); + return ServerToHandle::LinterDone(self.file.clone()); }, Err(AnalysisError::VFSError(e)) => { error!("Failed to create isolated linter analysis: {}", e); @@ -650,5 +652,126 @@ impl LinterJob { debug!("Linter analysis of {} was cancelled", self.file.as_str()); } } + ServerToHandle::AnalysisJobExited + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::time::{Duration, Instant}; + use crate::concurrency::ConcurrentJob; + + const TIMEOUT: Duration = Duration::from_secs(10); + + fn wait_until(what: &str, cond: impl Fn() -> bool) { + let start = Instant::now(); + while !cond() { + assert!(start.elapsed() < TIMEOUT, + "timed out waiting until {}", what); + thread::sleep(Duration::from_millis(1)); + } + } + + fn no_isolated_job_queued(queue: &AnalysisQueue) -> bool { + !queue.queue.lock().unwrap().iter() + .any(|job| matches!(job, QueuedJob::IsolatedAnalysisJob(_))) + } + + // We just need some file for the canonpath, content is irrelevant + fn existing_file() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("src") + .join("actions") + .join("analysis_queue.rs") + } + + fn expect(rx: &channel::Receiver, + what: &str, + pred: impl Fn(&ServerToHandle) -> bool) { + let message = rx.recv_timeout(TIMEOUT) + .unwrap_or_else(|e| panic!("no message for {}: {}", what, e)); + assert!(pred(&message), "expected {}, got {:?}", what, message); + } + + // Two isolated jobs on the same file are in flight at once. Each job + // reports its result on its own rendezvous channel, so the test decides + // which job finishes first. + #[test] + fn job_stays_in_flight_when_an_older_job_on_its_file_exits() { + let file = existing_file(); + let path = CanonPath::from_path_buf(file.clone()).unwrap(); + let paths: HashSet = std::iter::once(path.clone()).collect(); + + let (notify_tx, notify_rx) = channel::unbounded(); + let (older_report_tx, older_report_rx) = channel::bounded(0); + let (newer_report_tx, newer_report_rx) = channel::bounded(0); + let mut storage = AnalysisStorage::init(notify_tx.clone()); + storage.report = older_report_tx; + let queue = AnalysisQueue::init(Arc::new(AtomicBool::new(false)), + notify_tx); + let vfs = Vfs::new(); + vfs.set_file(&path, "dml 1.4;\ndevice dev;\n"); + + let (mut older_job, older_token) = ConcurrentJob::new(); + assert!(queue.enqueue_isolated_job(&mut storage, &vfs, None, + path.clone(), file.clone(), + older_token)); + wait_until("the older job is dispatched", + || no_isolated_job_queued(&queue)); + + storage.report = newer_report_tx; + let (mut newer_job, newer_token) = ConcurrentJob::new(); + assert!(queue.enqueue_isolated_job(&mut storage, &vfs, None, + path.clone(), file.clone(), + newer_token)); + wait_until("the newer job is dispatched", + || no_isolated_job_queued(&queue)); + + older_report_rx.recv_timeout(TIMEOUT) + .expect("no result from the older job"); + expect(¬ify_rx, "the older job's analysis", + |m| matches!(m, ServerToHandle::IsolatedAnalysisDone(..))); + // The newer job is still blocked reporting its result. + assert!(queue.has_isolated_work()); + assert!(queue.working_on_isolated_for_paths(&paths)); + assert!(queue.has_work()); + + newer_report_rx.recv_timeout(TIMEOUT) + .expect("no result from the newer job"); + expect(¬ify_rx, "the newer job's analysis", + |m| matches!(m, ServerToHandle::IsolatedAnalysisDone(..))); + assert!(!queue.has_isolated_work()); + assert!(!queue.working_on_isolated_for_paths(&paths)); + wait_until("the queue has no work", || !queue.has_work()); + + older_job.kill(); + newer_job.kill(); + } + + #[test] + fn early_cancelled_isolated_job_exits_and_leaves_no_work() { + let file = existing_file(); + let path = CanonPath::from_path_buf(file.clone()).unwrap(); + let paths: HashSet = std::iter::once(path.clone()).collect(); + + let (notify_tx, notify_rx) = channel::unbounded(); + let mut storage = AnalysisStorage::init(notify_tx.clone()); + let queue = AnalysisQueue::init(Arc::new(AtomicBool::new(false)), + notify_tx); + let vfs = Vfs::new(); + vfs.set_file(&path, "dml 1.4;\ndevice dev;\n"); + + let (mut job, token) = ConcurrentJob::new(); + job.kill(); + assert!(queue.enqueue_isolated_job(&mut storage, &vfs, None, + path, file, token)); + + expect(¬ify_rx, "the cancelled job's exit", + |m| *m == ServerToHandle::AnalysisJobExited); + assert!(storage.results.try_recv().is_err()); + assert!(!queue.has_isolated_work()); + assert!(!queue.working_on_isolated_for_paths(&paths)); + wait_until("the queue has no work", || !queue.has_work()); } } diff --git a/src/actions/mod.rs b/src/actions/mod.rs index 3951d171..f8426211 100644 --- a/src/actions/mod.rs +++ b/src/actions/mod.rs @@ -378,11 +378,12 @@ impl InitActionContext { _client_supports_cmd_run: bool, ) -> InitActionContext { let shut_down = Arc::new(AtomicBool::new(false)); + let server_notify = analysis.as_ref().lock().unwrap().notify.clone(); InitActionContext { vfs, - analysis, analysis_queue: Arc::new(AnalysisQueue::init( - Arc::clone(&shut_down))), + Arc::clone(&shut_down), server_notify)), + analysis, current_notifier: Arc::default(), config, lint_config: Arc::new(Mutex::new(LintCfg::default())), @@ -413,11 +414,12 @@ impl InitActionContext { ) -> InitActionContext { let shut_down = Arc::new(AtomicBool::new(false)); let config = Arc::new(Mutex::new(Config::default())); - + let server_notify = analysis.as_ref().lock().unwrap().notify.clone(); + InitActionContext { analysis, vfs, - analysis_queue: Arc::new(AnalysisQueue::init(Arc::clone(&shut_down))), + analysis_queue: Arc::new(AnalysisQueue::init(Arc::clone(&shut_down), server_notify)), current_notifier: Arc::new(Mutex::new(None)), quiescent: Arc::new(AtomicBool::new(false)), workspace_roots: Arc::new(Mutex::new(Vec::new())), diff --git a/src/server/mod.rs b/src/server/mod.rs index f118491a..0859a099 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -466,7 +466,14 @@ impl LsService { &importpath, Some(source), context, &self.output); } } - } + }, + // Sent after a job has left the in-flight trackers, but without + // a result that must be handled + ServerToHandle::AnalysisJobExited => { + if let ActionContext::Init(ctx) = &mut self.ctx { + ctx.check_state_waits(); + } + }, } } } @@ -652,6 +659,7 @@ pub enum ServerToHandle { DeviceAnalysisDone(CanonPath), LinterDone(CanonPath), AnalysisRequest(PathBuf, Option, CanonPath), + AnalysisJobExited, } // Indicates how the server should proceed.