diff --git a/components/script/dom/globalscope.rs b/components/script/dom/globalscope.rs index 255f2c34839..e3a1db7a1a7 100644 --- a/components/script/dom/globalscope.rs +++ b/components/script/dom/globalscope.rs @@ -47,6 +47,8 @@ use std::collections::HashMap; use std::collections::hash_map::Entry; use std::ffi::CString; use std::rc::Rc; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; use task::TaskCanceller; use task_source::file_reading::FileReadingTaskSource; use task_source::networking::NetworkingTaskSource; @@ -55,6 +57,17 @@ use time::{Timespec, get_time}; use timers::{IsInterval, OneshotTimerCallback, OneshotTimerHandle}; use timers::{OneshotTimers, TimerCallback}; +#[derive(JSTraceable)] +pub struct AutoCloseWorker( + Arc, +); + +impl Drop for AutoCloseWorker { + fn drop(&mut self) { + self.0.store(true, Ordering::SeqCst); + } +} + #[dom_struct] pub struct GlobalScope { eventtarget: EventTarget, @@ -110,6 +123,10 @@ pub struct GlobalScope { /// #[ignore_malloc_size_of = "Rc is hard"] microtask_queue: Rc, + + /// Vector storing closing references of all workers + #[ignore_malloc_size_of = "Arc"] + list_auto_close_worker: DomRefCell>, } impl GlobalScope { @@ -142,9 +159,14 @@ impl GlobalScope { timers: OneshotTimers::new(timer_event_chan, scheduler_chan), origin, microtask_queue, + list_auto_close_worker: Default::default(), } } + pub fn track_worker(&self, closing_worker: Arc) { + self.list_auto_close_worker.borrow_mut().push(AutoCloseWorker(closing_worker)); + } + /// Returns the global scope of the realm that the given DOM object's reflector /// was created in. #[allow(unsafe_code)] diff --git a/components/script/dom/worker.rs b/components/script/dom/worker.rs index f397ef42148..1b044d62e76 100644 --- a/components/script/dom/worker.rs +++ b/components/script/dom/worker.rs @@ -79,6 +79,7 @@ impl Worker { let (sender, receiver) = channel(); let closing = Arc::new(AtomicBool::new(false)); let worker = Worker::new(global, sender.clone(), closing.clone()); + global.track_worker(closing.clone()); let worker_ref = Trusted::new(&*worker); let worker_load_origin = WorkerScriptLoadOrigin { diff --git a/tests/html/child.html b/tests/html/child.html new file mode 100644 index 00000000000..bf2bbb4b06a --- /dev/null +++ b/tests/html/child.html @@ -0,0 +1,3 @@ + diff --git a/tests/html/parent.html b/tests/html/parent.html new file mode 100644 index 00000000000..6516089766a --- /dev/null +++ b/tests/html/parent.html @@ -0,0 +1,8 @@ + + + diff --git a/tests/html/work.js b/tests/html/work.js new file mode 100644 index 00000000000..8fef1f11654 --- /dev/null +++ b/tests/html/work.js @@ -0,0 +1,4 @@ +console.log("reached inside work.js file"); +setInterval(function() { + console.log('hi'); +}, 500);