Bug 198665 - Race condition makes ESM modules to evaluate twice
Summary: Race condition makes ESM modules to evaluate twice
Status: RESOLVED WORKSFORME
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: Safari 12
Hardware: Mac macOS 10.14
: P2 Critical
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2019-06-07 10:11 PDT by Manu Mtz-Almeida
Modified: 2022-02-03 15:33 PST (History)
5 users (show)

See Also:


Attachments
Repo case (20.80 KB, application/zip)
2019-06-07 10:11 PDT, Manu Mtz-Almeida
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Manu Mtz-Almeida 2019-06-07 10:11:38 PDT
Created attachment 371592 [details]
Repo case

Looks like there is a race condition when loading ESM modules in Safari, that leads the same module.
When using dynamic import(), the same ES module is evaludated more than once.

Here's the simplest repo case that reproduces it:
https://github.com/manucorporat/webkit-esm-bug

- Server the repo with a HTTP server
- Open the Safari dev tools,
- Reload several times until you can see several "load" console logs

HTML
```

<body>
  <script type="module">
    import "/build/index.js";
  </script>
</body>
```

index.js
```
let a = 0;
console.log('load', a++);
function hello() {
  import('./chunk.js')
}
hello();
```

chunk.js
```
import "./index.js";
```

EXPECTED BEHAVIOUR:

Same as Chrome, or Firefox.
The `index.js` module should be evaluated only once. This is causing crashes in a production website: https://stenciljs.com/
And most likely any users of Stenciljs in iOS
Comment 1 Radar WebKit Bug Importer 2019-06-07 13:30:17 PDT
<rdar://problem/51533282>
Comment 2 Manu Mtz-Almeida 2019-06-07 14:42:13 PDT
After some extra debugging I found it's also related with some external extension slowing down the page: https://itunes.apple.com/es/app/adguard-for-safari/id1440147259?l=en&mt=12

In any case, seems like there is a race condition that leads to the same ESM module being loaded twice, breaking some websites.
Comment 3 Fabian 2022-02-03 02:19:28 PST
I am wondering if this issue breaks using stencil js applications in Safari. They work fine after a reload.

I have posted a reference to this issue in the issue board: https://github.com/ionic-team/stencil/issues/2774#issuecomment-1028829579

Any help is appreciated!
Comment 4 Yusuke Suzuki 2022-02-03 02:23:55 PST
Hmm, I tried it but I cannot reproduce this issue.
Comment 5 Yusuke Suzuki 2022-02-03 02:29:28 PST
Hmm, yeah, I tried many times with the offered repro case, but so far, I cannot reproduce it.
Is it possible that this is related to installed extensions?
Can you reproduce it without enabling extensions?
Comment 6 Fabian 2022-02-03 02:32:53 PST
Same for me, I cannot reproduce in Safar 15.3 (17612.4.9.1.5) (Monterey 12.2)
Comment 7 Yusuke Suzuki 2022-02-03 15:33:30 PST
OK, closing this since I cannot reproduce this. I wonder if it is related to the installed extensions.
Please feel free to paste a new repro case etc. if you find :)