Combine some access to the thread local variable for script thread. (#38752)

This combines some access to the thread local variable for script
thread.

- We introduce a new UserInteractingScriptGuard which on drop handles
  the resetting of was_interacting to the previous value. Sometimes
throughout the code `ScriptThread::is_user_interacting` was reset to the
previous value while sometimes just set to false. This should
remove this footgun.
- This also reduces the amount of thread local access for
MutationObservers and task queue.

Testing: WPT tests should cover this.
Fixes: This addresses part of
https://github.com/servo/servo/issues/37969 but there is probably still
stuff to be done.

---------

Signed-off-by: Narfinger <Narfinger@users.noreply.github.com>
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Co-authored-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Narfinger 2025-09-11 11:40:32 +02:00 committed by GitHub
parent 5de041e6ef
commit 19f70dccf6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 156 additions and 117 deletions

View file

@ -22,7 +22,6 @@ use crate::dom::defaultteereadrequest::DefaultTeeReadRequestMicrotask;
use crate::dom::globalscope::GlobalScope;
use crate::dom::html::htmlimageelement::ImageElementMicrotask;
use crate::dom::html::htmlmediaelement::MediaElementMicrotask;
use crate::dom::mutationobserver::MutationObserver;
use crate::dom::promise::WaitForAllSuccessStepsMicrotask;
use crate::realms::enter_realm;
use crate::script_runtime::{CanGc, JSContext, notify_about_rejected_promises};
@ -118,13 +117,11 @@ impl MicrotaskQueue {
match *job {
Microtask::Promise(ref job) => {
if let Some(target) = target_provider(job.pipeline) {
let was_interacting = ScriptThread::is_user_interacting();
ScriptThread::set_user_interacting(job.is_user_interacting);
let _guard = ScriptThread::user_interacting_guard();
let _realm = enter_realm(&*target);
let _ = job
.callback
.Call_(&*target, ExceptionHandling::Report, can_gc);
ScriptThread::set_user_interacting(was_interacting);
}
},
Microtask::User(ref job) => {
@ -155,7 +152,7 @@ impl MicrotaskQueue {
ScriptThread::invoke_backup_element_queue(can_gc);
},
Microtask::NotifyMutationObservers => {
MutationObserver::notify_mutation_observers(can_gc);
ScriptThread::mutation_observers().notify_mutation_observers(can_gc);
},
}
}