Would it be possible to make the following code not deadlock the browser? `a.html` ```html <html><body><script> fetch('a.js').then(response => response.blob()).then(blob => { let worker = new Worker(URL.createObjectURL(blob)); let sab = new Uint8Array(new SharedArrayBuffer(16)); worker.postMessage(sab); console.log('Waiting for Worker to finish'); while(sab[0] != 1) /*wait to join with the result*/; console.log(`Worker finished. SAB: ${sab[0]}`); }); </script></body></html> ``` `a.js` ```js onmessage = (e) => { console.log('Received SAB'); e.data[0] = 1; } ``` Observed: browser hangs on the `while()` loop. Expected: `new Worker()` would be great to make progress asynchronously, and the code would print out ``` Worker finished. SAB: 1 ``` Some background: This deadlock is causing headaches to users of SharedArrayBuffer/WebAssembly, and leads to poor startup time and over-subscription of web resources on shipped web sites. If it was possible to make the above code work and not deadlock, it would greatly improve startup time and performance of SharedArrayBuffer-utilizing web sites. Note that we are not asking `new Worker(arbitraryUrl)` to necessarily have this forward-progress guarantee, but that at least that `new Worker(blobUrlInMemory)` would do so (like illustrated in above code). Would that be feasible?
Bug 1888109 Comment 0 Edit History
Note: The actual edited comment in the bug view page will always show the original commenter’s name and original timestamp.
Would it be possible to make the following code not deadlock the browser? `a.html` ```html <html><body><script> fetch('a.js').then(response => response.blob()).then(blob => { let worker = new Worker(URL.createObjectURL(blob)); let sab = new Uint8Array(new SharedArrayBuffer(16)); worker.postMessage(sab); console.log('Waiting for Worker to finish'); while(sab[0] != 1) /*wait to join with the result*/; console.log(`Worker finished. SAB: ${sab[0]}`); }); </script></body></html> ``` `a.js` ```js onmessage = (e) => { console.log('Received SAB'); e.data[0] = 1; } ``` Observed: browser hangs on the `while()` loop. Expected: `new Worker()` would be great to make progress asynchronously, and the code would print out ``` Worker finished. SAB: 1 ``` Some background: This deadlock is causing headaches to users of SharedArrayBuffer/WebAssembly, and leads to poor startup time and over-subscription of web resources on shipped web sites. If it was possible to make the above code work and not deadlock, it would greatly improve startup time and performance of SharedArrayBuffer-utilizing web sites. Note that we are not asking `new Worker(arbitraryUrl)` to necessarily have this forward-progress guarantee, but that at least that `new Worker(blobUrlInMemory)` would do so (like illustrated in above code). Would that be feasible? Note that when testing the above code, COOP+COEP HTTP headers are required, or otherwise the SharedArrayBuffer object will not be available. A quick way to get those headers is to download an ad hoc [emrun.py](https://raw.githubusercontent.com/emscripten-core/emscripten/main/emrun.py) web server, and run ``` emrun.py --no_browser --port 8000 . ``` in the directory of `a.html` and `a.js`. That will launch a web server that includes the relevant COOP+COEP headers.