script: Handle null contexts better during JS runtime shutdown. (#34769)

* script: Handle null contexts better during JS runtime shutdown.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* lock file

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
Co-authored-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
Josh Matthews 2024-12-26 03:23:27 -05:00 committed by GitHub
parent 981616f918
commit 4a6d2f8ff0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 41 additions and 30 deletions

View file

@ -93,8 +93,9 @@ impl Drop for CallbackObject {
#[allow(unsafe_code)]
fn drop(&mut self) {
unsafe {
let cx = Runtime::get();
RemoveRawValueRoot(cx, self.permanent_js_root.get_unsafe());
if let Some(cx) = Runtime::get() {
RemoveRawValueRoot(cx.as_ptr(), self.permanent_js_root.get_unsafe());
}
}
}
}

View file

@ -70,7 +70,9 @@ impl Clone for ServoJSPrincipals {
impl Drop for ServoJSPrincipals {
#[inline]
fn drop(&mut self) {
unsafe { JS_DropPrincipals(Runtime::get(), self.as_raw()) };
if let Some(cx) = Runtime::get() {
unsafe { JS_DropPrincipals(cx.as_ptr(), self.as_raw()) };
}
}
}

View file

@ -117,9 +117,9 @@ impl AutoIncumbentScript {
pub fn new(global: &GlobalScope) -> Self {
// Step 2-3.
unsafe {
let cx = Runtime::get();
assert!(!cx.is_null());
HideScriptedCaller(cx);
let cx =
Runtime::get().expect("Creating a new incumbent script after runtime shutdown");
HideScriptedCaller(cx.as_ptr());
}
STACK.with(|stack| {
trace!("Prepare to run a callback with {:p}", global);
@ -156,9 +156,9 @@ impl Drop for AutoIncumbentScript {
});
unsafe {
// Step 1-2.
let cx = Runtime::get();
assert!(!cx.is_null());
UnhideScriptedCaller(cx);
if let Some(cx) = Runtime::get() {
UnhideScriptedCaller(cx.as_ptr());
}
}
}
}
@ -174,9 +174,12 @@ pub fn incumbent_global() -> Option<DomRoot<GlobalScope>> {
// there's nothing on the JS stack, which will cause us to check the
// incumbent script stack below.
unsafe {
let cx = Runtime::get();
assert!(!cx.is_null());
let global = GetScriptedCallerGlobal(cx);
let Some(cx) = Runtime::get() else {
// It's not meaningful to return a global object if the runtime
// no longer exists.
return None;
};
let global = GetScriptedCallerGlobal(cx.as_ptr());
if !global.is_null() {
return Some(GlobalScope::from_object(global));
}

View file

@ -2253,7 +2253,10 @@ impl GlobalScope {
#[allow(unsafe_code)]
pub fn get_cx() -> SafeJSContext {
unsafe { SafeJSContext::from_ptr(Runtime::get()) }
let cx = Runtime::get()
.expect("Can't obtain context after runtime shutdown")
.as_ptr();
unsafe { SafeJSContext::from_ptr(cx) }
}
pub fn crypto(&self) -> DomRoot<Crypto> {
@ -3058,14 +3061,13 @@ impl GlobalScope {
/// ["current"]: https://html.spec.whatwg.org/multipage/#current
#[allow(unsafe_code)]
pub fn current() -> Option<DomRoot<Self>> {
let cx = Runtime::get()?;
unsafe {
let cx = Runtime::get();
assert!(!cx.is_null());
let global = CurrentGlobalOrNull(cx);
let global = CurrentGlobalOrNull(cx.as_ptr());
if global.is_null() {
None
} else {
Some(global_scope_from_global(global, cx))
Some(global_scope_from_global(global, cx.as_ptr()))
}
}
}

View file

@ -78,9 +78,9 @@ impl Drop for Promise {
unsafe {
let object = self.permanent_js_root.get().to_object();
assert!(!object.is_null());
let cx = Runtime::get();
assert!(!cx.is_null());
RemoveRawValueRoot(cx, self.permanent_js_root.get_unsafe());
if let Some(cx) = Runtime::get() {
RemoveRawValueRoot(cx.as_ptr(), self.permanent_js_root.get_unsafe());
}
}
}
}

View file

@ -621,7 +621,9 @@ impl Runtime {
&*(closure as *mut NetworkingTaskSource);
let runnable = Runnable(dispatchable);
let task = task!(dispatch_to_event_loop_message: move || {
runnable.run(RustRuntime::get(), Dispatchable_MaybeShuttingDown::NotShuttingDown);
if let Some(cx) = RustRuntime::get() {
runnable.run(cx.as_ptr(), Dispatchable_MaybeShuttingDown::NotShuttingDown);
}
});
networking_task_src.queue_unconditionally(task).is_ok()
@ -801,6 +803,8 @@ impl Runtime {
impl Drop for Runtime {
#[allow(unsafe_code)]
fn drop(&mut self) {
self.microtask_queue.clear();
unsafe {
DeleteJobQueue(self.job_queue);
}