servo: Ocassional crash in hashbrown when using hash{map, set} in script_module.rs when advancing modules

Running WPT tests with /home/user/Desktop/servo/target/release/servo
Running 1 tests in web-platform-tests


This GDB supports auto-downloading debuginfo from the following URLs:
  <https://debuginfod.ubuntu.com>
Enable debuginfod for this session? (y or [n]) [answered N; input not from terminal]
Debuginfod has been disabled.
To make this setting permanent, add 'set debuginfod enabled off' to .gdbinit.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[Detaching after vfork from child process 240167]
[Detaching after vfork from child process 240169]
[Detaching after vfork from child process 240175]
[New Thread 0x7fffea3ff6c0 (LWP 240176)]
[New Thread 0x7fffe9bfe6c0 (LWP 240177)]
[New Thread 0x7fffe93fd6c0 (LWP 240179)]
[New Thread 0x7fffe8bfc6c0 (LWP 240180)]
[New Thread 0x7fffdbdff6c0 (LWP 240181)]
[New Thread 0x7fffdb5fe6c0 (LWP 240182)]
[New Thread 0x7fffdabfd6c0 (LWP 240183)]
[Thread 0x7fffdabfd6c0 (LWP 240183) exited]
[New Thread 0x7fffdabfd6c0 (LWP 240184)]
[New Thread 0x7fffda3ff6c0 (LWP 240188)]
[New Thread 0x7fffda1fe6c0 (LWP 240191)]
[New Thread 0x7fffd9ffd6c0 (LWP 240195)]
[New Thread 0x7fffd987c6c0 (LWP 240196)]
[New Thread 0x7fffd967b6c0 (LWP 240197)]
[New Thread 0x7fffd947a6c0 (LWP 240198)]
[New Thread 0x7fffd8fff6c0 (LWP 240199)]
[New Thread 0x7fffaffff6c0 (LWP 240200)]
[New Thread 0x7fffafdfe6c0 (LWP 240201)]
[New Thread 0x7fffaefff6c0 (LWP 240202)]
[New Thread 0x7fffaebfe6c0 (LWP 240203)]
[New Thread 0x7fffadfff6c0 (LWP 240204)]
[Thread 0x7fffadfff6c0 (LWP 240204) exited]
[New Thread 0x7fffadfff6c0 (LWP 240205)]
[New Thread 0x7fffad3ff6c0 (LWP 240206)]
[New Thread 0x7fffac9ff6c0 (LWP 240207)]
[New Thread 0x7fffac7fe6c0 (LWP 240208)]
[New Thread 0x7fff8bfff6c0 (LWP 240209)]
[New Thread 0x7fff83fff6c0 (LWP 240210)]
[New Thread 0x7fff8b7ff6c0 (LWP 240211)]
[New Thread 0x7fff8b5fe6c0 (LWP 240212)]
[New Thread 0x7fff8afff6c0 (LWP 240213)]
[New Thread 0x7fff8a3fe6c0 (LWP 240214)]
[New Thread 0x7fff8a5ff6c0 (LWP 240215)]
[New Thread 0x7fff899ff6c0 (LWP 240216)]
[New Thread 0x7fff88dff6c0 (LWP 240217)]
[New Thread 0x7fff83bfd6c0 (LWP 240219)]
[New Thread 0x7fff83dfe6c0 (LWP 240218)]
[New Thread 0x7fff831ff6c0 (LWP 240220)]
[New Thread 0x7fff829ff6c0 (LWP 240221)]
[New Thread 0x7fff825fe6c0 (LWP 240222)]
[New Thread 0x7fff81fff6c0 (LWP 240223)]
[New Thread 0x7fff811ff6c0 (LWP 240224)]
[New Thread 0x7fff80bff6c0 (LWP 240225)]
[New Thread 0x7fff803ff6c0 (LWP 240226)]
[New Thread 0x7fff7bbff6c0 (LWP 240227)]
[New Thread 0x7fff7b3ff6c0 (LWP 240228)]
[New Thread 0x7fff78fff6c0 (LWP 240229)]
[New Thread 0x7fff78dfe6c0 (LWP 240230)]
[Thread 0x7fffd987c6c0 (LWP 240196) exited]
[New Thread 0x7fffd987c6c0 (LWP 240231)]
[New Thread 0x7fff73fff6c0 (LWP 240232)]
[New Thread 0x7fffd8bfe6c0 (LWP 240233)]
[New Thread 0x7fffad7fe6c0 (LWP 240234)]
[New Thread 0x7fffacffe6c0 (LWP 240235)]
[New Thread 0x7fff8bdfe6c0 (LWP 240236)]
[New Thread 0x7fff8abfe6c0 (LWP 240237)]
[New Thread 0x7fff895fe6c0 (LWP 240238)]
[New Thread 0x7fff889fe6c0 (LWP 240239)]
[New Thread 0x7fff82dfe6c0 (LWP 240240)]
[New Thread 0x7fff706ff6c0 (LWP 240241)]
[New Thread 0x7fff704fe6c0 (LWP 240242)]
[New Thread 0x7fff702fd6c0 (LWP 240243)]
[New Thread 0x7fff615ff6c0 (LWP 240244)]
[New Thread 0x7fff613fe6c0 (LWP 240245)]
[New Thread 0x7fff611fd6c0 (LWP 240246)]
[New Thread 0x7fff60ffc6c0 (LWP 240247)]
[New Thread 0x7fff60dfb6c0 (LWP 240248)]
[New Thread 0x7fff60bfa6c0 (LWP 240249)]
[New Thread 0x7fff609f96c0 (LWP 240250)]
[New Thread 0x7fff607f86c0 (LWP 240251)]
[New Thread 0x7fff605f76c0 (LWP 240252)]
[New Thread 0x7fff603f66c0 (LWP 240253)]
[New Thread 0x7ffeebfff6c0 (LWP 240254)]
[New Thread 0x7ffeebdfe6c0 (LWP 240255)]
[New Thread 0x7ffeebbfd6c0 (LWP 240256)]
[New Thread 0x7fffae9766c0 (LWP 240262)]
[New Thread 0x7fffae8f56c0 (LWP 240263)]
[New Thread 0x7fffac5fd6c0 (LWP 240264)]

Thread 48 "Script(1,1)" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffd987c6c0 (LWP 240231)]
0x0000555559593cc1 in hashbrown::map::HashMap<K,V,S,A>::insert ()
#0  0x0000555559593cc1 in hashbrown::map::HashMap<K,V,S,A>::insert ()
#1  0x00005555596036db in script::script_module::ModuleTree::find_first_parse_error ()
#2  0x0000555559603a99 in script::script_module::ModuleTree::find_first_parse_error ()
#3  0x0000555559603a99 in script::script_module::ModuleTree::find_first_parse_error ()
#4  0x0000555559603a99 in script::script_module::ModuleTree::find_first_parse_error ()
#5  0x0000555559603a99 in script::script_module::ModuleTree::find_first_parse_error ()
#6  0x0000555559603a99 in script::script_module::ModuleTree::find_first_parse_error ()
#7  0x0000555559603a99 in script::script_module::ModuleTree::find_first_parse_error ()
#8  0x0000555559603a99 in script::script_module::ModuleTree::find_first_parse_error ()
#9  0x0000555559603a99 in script::script_module::ModuleTree::find_first_parse_error ()
#10 0x000055555960590b in script::script_module::ModuleTree::advance_finished_and_link ()
---------------------- ommited -----------------------------------
#7543 0x0000555559605851 in script::script_module::ModuleTree::advance_finished_and_link ()
#7544 0x00005555596050a8 in script::script_module::ModuleTree::fetch_module_descendants ()
#7545 0x0000555559608c19 in <script::script_module::ModuleContext as net_traits::FetchResponseListener>::process_response_eof ()
#7546 0x000055555993b474 in <net_traits::FetchResponseMsg as net_traits::Action<T>>::process ()
#7547 0x000055555960011a in <script::network_listener::ListenerTask<A,Listener> as script::task::TaskOnce>::run_once ()
#7548 0x0000555558f9ad2b in <T as script::task::TaskBox>::run_box ()
#7549 0x000055555942f0a0 in script::script_thread::ScriptThread::handle_msg_from_script ()
#7550 0x00005555594271bf in script::script_thread::ScriptThread::handle_msgs::{{closure}} ()
#7551 0x0000555559423f04 in script::script_thread::ScriptThread::handle_msgs ()
#7552 0x000055555941de80 in script::script_thread::ScriptThread::start ()
#7553 0x0000555558f5b8ff in profile_traits::mem::ProfilerChan::run_with_memory_reporting ()
#7554 0x0000555558fe2ee6 in std::sys_common::backtrace::__rust_begin_short_backtrace ()
#7555 0x0000555558fe84a2 in core::ops::function::FnOnce::call_once{{vtable.shim}} ()
#7556 0x000055555bc07075 in alloc::boxed::{impl#47}::call_once<(), dyn core::ops::function::FnOnce<(), Output=()>, alloc::alloc::Global> () at library/alloc/src/boxed.rs:2007
#7557 alloc::boxed::{impl#47}::call_once<(), alloc::boxed::Box<dyn core::ops::function::FnOnce<(), Output=()>, alloc::alloc::Global>, alloc::alloc::Global> () at library/alloc/src/boxed.rs:2007
#7558 std::sys::unix::thread::{impl#2}::new::thread_start () at library/std/src/sys/unix/thread.rs:108
#7559 0x00007ffff6c97b5a in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:444
#7560 0x00007ffff6d285fc in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78
  ▶ TIMEOUT [expected ERROR] /_webgpu/webgpu/cts.https.html?q=webgpu:shader,execution,expression,call,builtin,dot:f32_vec4:*


Ran 1 tests finished in 9.4 seconds.
  • 0 ran as expected. 0 tests skipped.
  • 1 tests timed out unexpectedly

Tests with unexpected results:
  ▶ TIMEOUT [expected ERROR] /_webgpu/webgpu/cts.https.html?q=webgpu:shader,execution,expression,call,builtin,dot:f32_vec4:*

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 57 (57 by maintainers)

Commits related to this issue

Most upvoted comments

I can try looking into this this weekend. Per spec, I did a quick check and I think it’s already updated and the algorithm of find first parse error doesn’t exist anymore. I will also check if we can fix the spec update to solve this issue as well.

@sagudev sorry for missing to update with you 🙏 I had an in-progress patch locally to remove the recursive call though, it’s still buggy around propagating errors. I will try to work on it and update more here (possibly with a PR when it passes more tests) 🙇

I recalled https://github.com/servo/servo/issues/26903 which should be related to the issue what we’d like to fix here.

You can also see this by the way it is called: https://searchfox.org/mozilla-central/source/js/loader/ModuleLoaderBase.cpp#955.

OH, some more observation sharing: so, the mAwaitingImports would be decremented in this function: https://searchfox.org/mozilla-central/rev/a39aed6c2042ba2fa5af779e8b97e8a3f660e480/js/loader/ModuleLoadRequest.cpp#240-250

and the function would be triggered when the ModuleRequest is gc-ed (iiuc)

https://searchfox.org/mozilla-central/rev/a39aed6c2042ba2fa5af779e8b97e8a3f660e480/js/loader/ModuleLoadRequest.cpp#28-37

You can also see this by the way it is called: https://searchfox.org/mozilla-central/source/js/loader/ModuleLoaderBase.cpp#955.

I see. Thank you!

iirc, if we only do a zero checking (which we have incomplete_fetch_urls for the same purpose), modules sometimes could be instantiated wrongly. Or, possibly there are still some other difference between our impl and Gecko’s impl. Let me check it carefully 🙏

I noticed they also need to check if dependencies are all fetched or not at

After quickly skimming at code I think DependenciesLoaded is not checker but more of an assert. It just verifies that all deps are loaded and do some post-steps after that, so it is supposed to be run after they are already loaded. You can also see this by the way it is called: https://searchfox.org/mozilla-central/source/js/loader/ModuleLoaderBase.cpp#955.

But I might be wrong, as I only took a quick glance at code.

While reading through the Gecko implementation, I noticed they also need to check if dependencies are all fetched or not at

https://searchfox.org/mozilla-central/rev/a39aed6c2042ba2fa5af779e8b97e8a3f660e480/js/loader/ModuleLoadRequest.cpp#184-200

however, I don’t fully understand this part:

  for (const auto& childRequest : mImports) {
    ModuleScript* childScript = childRequest->mModuleScript;
    if (!childScript) {
      mModuleScript = nullptr;
      LOG(("ScriptLoadRequest (%p):   %p failed (load error)", this,
           childRequest.get()));
      return;
    }

    MOZ_DIAGNOSTIC_ASSERT(mModuleScript->HadImportMap() ==
                          childScript->HadImportMap());
  }

https://searchfox.org/mozilla-central/rev/a39aed6c2042ba2fa5af779e8b97e8a3f660e480/js/loader/ModuleLoadRequest.cpp#202-223

In this for-loop, is it run in a blocking way? or how does it work? if a child request is still fetching, I think mModuleScript is still an initial null pointer though, in that case, if this for-loop doesn’t wait for the request to be done, wouldn’t it break the checking? 🤔


In the meanwhile, I also checked how WebKit implemented the logic of fetching modules with Promises so that it’s easier to handle cyclic dependencies than manual checking:

https://github.com/WebKit/WebKit/blob/d4c4d4bd1fcfb8bfee7f7a51333f8bebf5e0e369/Source/JavaScriptCore/builtins/ModuleLoader.js#L430-L434

and actually, when I implemented the script_module, it was initially implemented with Promise.all too… but I changed it to manual dependencies status checking in https://github.com/servo/servo/pull/26395.

With looking at the WebKit implementation, I start to think that maybe it’s worth trying moving the fetch mechanism for modules back to Promise.all so that it would be easier to maintain. I will try to experiment loading modules with Promise.all (again).

GECKO: https://searchfox.org/mozilla-central/source/dom/workers/loader/WorkerModuleLoader.h#28

Thanks! as we don’t support worker script modules yet, let me share the DOM script modules here as well: https://searchfox.org/mozilla-central/source/dom/script/ModuleLoader.cpp#285

Another spec mismatch is at https://html.spec.whatwg.org/multipage/#fetch-the-descendants-of-and-link-a-module-script. Currently, SpiderMonkey doesn’t provide such API for LoadRequestedModules so I will look into how Gecko implemented descendant fetches and align our implementation with Gecko first. (because, in advance_finish_and_link, it contains another recursive call, I guess it could be another cause for the occasional crash)

Hm, I never got this. Maybe you are already running something on port 8889?

hmm, I’ve also tried to check it with lsof -i :8889 but there’s nothing.

./mach clean is sometimes needed after new pull.

Let me try to clean and rebuild. I hope it will work.


Quick update: I’ve noticed the find first parse error already became the step 8-1 of https://html.spec.whatwg.org/multipage/#creating-a-javascript-module-script so I will try to adapt current implementation to match it.

Hm, I never got this. Maybe you are already running something on port 8889? I do not think we actually need ws for cts tests (actually we probably do not need it for any wpt test except for websocket tests).

./mach clean is sometimes needed after new pull.

I will look into this issue again today 🙏 but somehow after pulling latest main branch to my local, I ran into Servers failed to start: wss:8889 issue that it seems we had similar issue before in https://github.com/servo/servo/issues/20734 🤔 I will work on spec-update changes first and look into fixing this wpt error lately.

→ ./mach test-wpt -r '_webgpu/webgpu/cts.https.html?q=webgpu:shader,execution,expression,unary,i32_conversion:f16:*' --processes=1 --repeat-until-unexpected
Running WPT tests with /Users/cybai/codespace/servo/servo/target/release/servo
Please ensure all the necessary WPT subdomains are mapped to a loopback device in /etc/hosts.
See https://web-platform-tests.org/running-tests/from-local-system.html#system-setup for instructions.
Error running mach:

    ['test-wpt', '-r', '_webgpu/webgpu/cts.https.html?q=webgpu:shader,execution,expression,unary,i32_conversion:f16:*', '--processes=1', '--repeat-until-unexpected']

The error occurred in code that was called by the mach command. This is either
a bug in the called code itself or in the way that mach is calling it.
You can invoke |./mach busted| to check if this issue is already on file. If it
isn't, please use |./mach busted file| to report it. If |./mach busted| is
misbehaving, you can also inspect the dependencies of bug 1543241.

If filing a bug, please include the full output of mach, including this error
message.

The details of the failure are as follows:

OSError: Servers failed to start: wss:8889

  File "/Users/cybai/codespace/servo/servo/python/servo/command_base.py", line 740, in configuration_decorator
    return original_function(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/cybai/codespace/servo/servo/python/servo/testing_commands.py", line 306, in test_wpt
    return self._test_wpt(build_type=build_type, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/cybai/codespace/servo/servo/python/servo/testing_commands.py", line 328, in _test_wpt
    return_value = wpt.run.run_tests(binary_path, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/cybai/codespace/servo/servo/python/wpt/run.py", line 129, in run_tests
    wptrunner.run_tests(**kwargs)
  File "/Users/cybai/codespace/servo/servo/tests/wpt/tests/tools/wptrunner/wptrunner/wptrunner.py", line 453, in run_tests
    test_environment.ensure_started()
  File "/Users/cybai/codespace/servo/servo/tests/wpt/tests/tools/wptrunner/wptrunner/environment.py", line 283, in ensure_started
    raise OSError(msg)

I observed the same thing in https://github.com/servo/servo/issues/31397#issuecomment-1961802764 and later did further investigation in https://github.com/servo/servo/issues/31397#issuecomment-1979288715, I also did try to create minimal repro with cyclic deps, but it was all fruitless, so unfortunately webgpu tests are the best we have right now.

I am pretty confident that bringing us closer to spec should solve this issue, so thank you for doing the work.

The log is too long so I omitted most of them but basically before crashing it almost running into the endless loop for those cyclic modules… I think currently we don’t handle multiple cyclic modules properly and it results in crashing in the endless loop for multiple cyclic modules.

...
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] module instantiated successfully
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] Going to advance and finish for: https://web-platform.test:8443/_webgpu/webgpu/webgpu/shader/execution/expression/expectation.js
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] module instantiated successfully
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] Going to advance and finish for: https://web-platform.test:8443/_webgpu/webgpu/webgpu/shader/execution/expression/case_cache.js
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] module instantiated successfully
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] Going to advance and finish for: https://web-platform.test:8443/_webgpu/webgpu/webgpu/util/compare.js
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] module instantiated successfully
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] Going to advance and finish for: https://web-platform.test:8443/_webgpu/webgpu/webgpu/shader/execution/expression/expectation.js
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] module instantiated successfully
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] Going to advance and finish for: https://web-platform.test:8443/_webgpu/webgpu/webgpu/shader/execution/expression/case_cache.js
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] module instantiated successfully
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] Going to advance and finish for: https://web-platform.test:8443/_webgpu/webgpu/webgpu/util/compare.js
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] module instantiated successfully
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] Going to advance and finish for: https://web-platform.test:8443/_webgpu/webgpu/webgpu/shader/execution/expression/expectation.js
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] module instantiated successfully
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] Going to advance and finish for: https://web-platform.test:8443/_webgpu/webgpu/webgpu/shader/execution/expression/case_cache.js
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] module instantiated successfully
  │ [2024-03-17T08:07:36Z DEBUG script::script_module] Going to advance and finish for: https://web-platform.test:8443/_webgpu/webgpu/webgpu/util/compare.js
  └ [2024-03-17T08:07:36Z DEBUG script::script_module] module instantiated successfully

What exactly does not work with debugger?

I posted some instructions for VSCode in #31397 (comment), but you might need to change the path for the binary. There is also --debugger lldb option you can use via mach.

Yes! I following the instructions in https://github.com/servo/servo/issues/31397#issuecomment-1987158499 to add the launch.json for my VSCode though, somehow the debugger is not always attached on my editor 🤔 and when it’s attached, I couldn’t find stacktraces related to script_module.

If vscode lldb still doesn’t work for me, I may try --debugger lldb later 🙏 Thank you!

Great news.

What exactly does not work with debugger?

I posted some instructions for VSCode in https://github.com/servo/servo/issues/31397#issuecomment-1987158499, but you might need to change the path for the binary. There is also --debugger lldb option you can use via mach.

some progress update here:

I’m trying to get rid of the recursive find-first-parse-error locally, while the algorithm is not fully spec compliant yet, with adding following patch, I can already pass the WPT test more times and it needs more times to get the CRASH from webgpu:shader,execution,expression,unary,i32_conversion:f16:* test.

On the other hand, I’m still trying to use lldb to attach but it doesn’t work well on my local yet; I may be able to find more information when I can use debugger to debug 🙏

Open to check the patch
diff --git a/components/script/script_module.rs b/components/script/script_module.rs
index c6b7f4814c..cc0d300e05 100644
--- a/components/script/script_module.rs
+++ b/components/script/script_module.rs
@@ -160,6 +160,12 @@ impl ModuleIdentity {
     }
 }
 
+#[derive(JSTraceable)]
+enum ModuleFetchError {
+    Rethrow(RethrowError),
+    Network(#[no_trace] NetworkError),
+}
+
 #[derive(JSTraceable)]
 pub struct ModuleTree {
     #[no_trace]
@@ -628,63 +634,6 @@ impl ModuleTree {
         return ServoUrl::parse_with_base(Some(url), &specifier_str.clone());
     }
 
-    /// <https://html.spec.whatwg.org/multipage/#finding-the-first-parse-error>
-    fn find_first_parse_error(
-        &self,
-        global: &GlobalScope,
-        discovered_urls: &mut HashSet<ServoUrl>,
-    ) -> (Option<NetworkError>, Option<RethrowError>) {
-        // 3.
-        discovered_urls.insert(self.url.clone());
-
-        // 4.
-        let record = self.get_record().borrow();
-        if record.is_none() {
-            return (
-                self.network_error.borrow().clone(),
-                self.rethrow_error.borrow().clone(),
-            );
-        }
-
-        let module_map = global.get_module_map().borrow();
-        let mut parse_error: Option<RethrowError> = None;
-
-        // 5-6.
-        let descendant_urls = self.descendant_urls.borrow();
-        for descendant_module in descendant_urls
-            .iter()
-            // 7.
-            .filter_map(|url| module_map.get(&url.clone()))
-        {
-            // 8-2.
-            if discovered_urls.contains(&descendant_module.url) {
-                continue;
-            }
-
-            // 8-3.
-            let (child_network_error, child_parse_error) =
-                descendant_module.find_first_parse_error(&global, discovered_urls);
-
-            // Due to network error's priority higher than parse error,
-            // we will return directly when we meet a network error.
-            if child_network_error.is_some() {
-                return (child_network_error, None);
-            }
-
-            // 8-4.
-            //
-            // In case of having any network error in other descendants,
-            // we will store the "first" parse error and keep running this
-            // loop to ensure we don't have any network error.
-            if child_parse_error.is_some() && parse_error.is_none() {
-                parse_error = child_parse_error;
-            }
-        }
-
-        // Step 9.
-        return (None, parse_error);
-    }
-
     #[allow(unsafe_code)]
     /// <https://html.spec.whatwg.org/multipage/#fetch-the-descendants-of-a-module-script>
     fn fetch_module_descendants(
@@ -725,7 +674,7 @@ impl ModuleTree {
             // Step 3.
             Ok(valid_specifier_urls) if valid_specifier_urls.len() == 0 => {
                 debug!("Module {} doesn't have any dependencies.", self.url.clone());
-                self.advance_finished_and_link(&global);
+                self.advance_finished_and_link(&global, &None);
             },
             Ok(valid_specifier_urls) => {
                 self.descendant_urls
@@ -753,7 +702,7 @@ impl ModuleTree {
                         "After checking with visited urls, module {} doesn't have dependencies to load.",
                         self.url.clone()
                     );
-                    self.advance_finished_and_link(&global);
+                    self.advance_finished_and_link(&global, &None);
                     return;
                 }
 
@@ -779,16 +728,15 @@ impl ModuleTree {
                     );
                 }
             },
-            Err(error) => {
-                self.set_rethrow_error(error);
-                self.advance_finished_and_link(&global);
+            Err(err) => {
+                self.advance_finished_and_link(&global, &Some(ModuleFetchError::Rethrow(err)));
             },
         }
     }
 
     /// <https://html.spec.whatwg.org/multipage/#fetch-the-descendants-of-and-link-a-module-script>
     /// step 4-7.
-    fn advance_finished_and_link(&self, global: &GlobalScope) {
+    fn advance_finished_and_link(&self, global: &GlobalScope, error: &Option<ModuleFetchError>) {
         {
             if !self.has_all_ready_descendants(&global) {
                 return;
@@ -799,6 +747,25 @@ impl ModuleTree {
 
         debug!("Going to advance and finish for: {}", self.url.clone());
 
+        match &error {
+            None => {
+                let module_record = self.get_record().borrow();
+                if let Some(record) = &*module_record {
+                    let instantiated = self.instantiate_module_tree(&global, record.handle());
+
+                    if let Err(exception) = instantiated {
+                        self.set_rethrow_error(exception);
+                    }
+                }
+            },
+            Some(ModuleFetchError::Network(network_error)) => {
+                self.set_network_error(network_error.clone());
+            },
+            Some(ModuleFetchError::Rethrow(rethrow_error)) => {
+                self.set_rethrow_error(rethrow_error.clone());
+            },
+        }
+
         {
             // Notify parents of this module to finish
             //
@@ -815,34 +782,11 @@ impl ModuleTree {
 
                 if incomplete_count_before_remove > 0 {
                     parent_tree.remove_incomplete_fetch_url(self.url.clone());
-                    parent_tree.advance_finished_and_link(&global);
+                    parent_tree.advance_finished_and_link(&global, &error);
                 }
             }
         }
 
-        let mut discovered_urls: HashSet<ServoUrl> = HashSet::new();
-        let (network_error, rethrow_error) =
-            self.find_first_parse_error(&global, &mut discovered_urls);
-
-        match (network_error, rethrow_error) {
-            (Some(network_error), _) => {
-                self.set_network_error(network_error);
-            },
-            (None, None) => {
-                let module_record = self.get_record().borrow();
-                if let Some(record) = &*module_record {
-                    let instantiated = self.instantiate_module_tree(&global, record.handle());
-
-                    if let Err(exception) = instantiated {
-                        self.set_rethrow_error(exception);
-                    }
-                }
-            },
-            (None, Some(error)) => {
-                self.set_rethrow_error(error);
-            },
-        }
-
         let promise = self.promise.borrow();
         if let Some(promise) = promise.as_ref() {
             promise.resolve_native(&());
@@ -957,10 +901,6 @@ impl ModuleOwner {
         let cx = GlobalScope::get_cx();
         let module_tree = module_identity.get_module_tree(&global);
 
-        // In the timing of executing this `finish_dynamic_module` function,
-        // we've run `find_first_parse_error` which means we've had the highest
-        // priority error in the tree. So, we can just get both `network_error` and
-        // `rethrow_error` directly here.
         let network_error = module_tree.get_network_error().borrow().as_ref().cloned();
         let existing_rethrow_error = module_tree.get_rethrow_error().borrow().as_ref().cloned();
 
@@ -1141,8 +1081,7 @@ impl FetchResponseListener for ModuleContext {
         match load {
             Err(err) => {
                 error!("Failed to fetch {} with error {:?}", self.url.clone(), err);
-                module_tree.set_network_error(err);
-                module_tree.advance_finished_and_link(&global);
+                module_tree.advance_finished_and_link(&global, &Some(ModuleFetchError::Network(err)));
             },
             Ok(ref resp_mod_script) => {
                 module_tree.set_text(resp_mod_script.text());
@@ -1157,8 +1096,7 @@ impl FetchResponseListener for ModuleContext {
 
                 match compiled_module {
                     Err(exception) => {
-                        module_tree.set_rethrow_error(exception);
-                        module_tree.advance_finished_and_link(&global);
+                        module_tree.advance_finished_and_link(&global, &Some(ModuleFetchError::Rethrow(exception)));
                     },
                     Ok(record) => {
                         module_tree.set_record(record);
@@ -1597,7 +1535,7 @@ fn fetch_single_module_script(
                 ModuleStatus::Fetching => {},
                 // Step 3.
                 ModuleStatus::FetchingDescendants | ModuleStatus::Finished => {
-                    module_tree.advance_finished_and_link(&global);
+                    module_tree.advance_finished_and_link(&global, &None);
                 },
             }

SIDE-NOTE: I think the best for testing is _webgpu/webgpu/cts.https.html?q=webgpu:shader,execution,expression,unary,i32_conversion:f16:* as it passes (if not crashing) and is pretty quick.

OH! Good catch! Thank you! The test is running expectedly on my local now; as I’m using zsh, I need to wrap the path with single quotes 👇

$  ./mach test-wpt -r '_webgpu/webgpu/cts.https.html?q=webgpu:shader,execution,expression,unary,i32_conversion:f16:*' --processes=1 --repeat-until-unexpected

I may not have enough bandwidth to work on this during weekdays but I will share more progress here.

@sagudev Thanks for the tips! I’m going to look into the issue and I will ping you if I find any questions about testing it.

@CYBAI I did some cleanup on https://github.com/servo/servo/pull/31194, so you can base on it for your work and then rebase it on top of main. I usually built with --production --with-asan and test with ./mach test-wpt --production _webgpu/webgpu/cts.https.html?=webgpu:shader,execution,expression,unary,u32_conversion:u32:* --repeat-until-unexpected which will rerun test until it crashes, which usually happens in 30 runs. If you need any help with testing, don’t hesitate to ping me.