Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use coincident if crossOriginIsolated, comlink otherwise #126

Merged
merged 6 commits into from
Jul 23, 2024

Conversation

jtpio
Copy link
Member

@jtpio jtpio commented Jul 12, 2024

Looking into jupyterlite/jupyterlite#1424.

coincident uses a SharedArrayBuffer here: https://github.com/WebReflection/coincident/blob/01cc8a1e90abb360deb9eabd7773ef4355fcbc3d/esm/index.js#L82

However in environments that are not crossOriginIsolated, usings coincident on the worker side leads to the following issues:

TypeError: [object Int32Array] is not a shared typed array.

So this PR takes the following approach:

Fixes #127

@jtpio jtpio added the bug Something isn't working label Jul 12, 2024
@jtpio jtpio added this to the 0.4.0 milestone Jul 12, 2024
Copy link
Contributor

lite-badge 👈 Try it on ReadTheDocs

@jtpio
Copy link
Member Author

jtpio commented Jul 12, 2024

Current status, after a naive replacement of postMessage by coincident:

image

@bollwyvl
Copy link
Contributor

Maybe we need to be using coincident/structured?

@jtpio
Copy link
Member Author

jtpio commented Jul 15, 2024

Right, I'll have a closer look.

@jtpio
Copy link
Member Author

jtpio commented Jul 17, 2024

This seems to be caused by coincident using a SharedArrayBuffer here: https://github.com/WebReflection/coincident/blob/01cc8a1e90abb360deb9eabd7773ef4355fcbc3d/esm/index.js#L82

It seems to be working fine when serving the site with right headers, for example with: npx static-handler --cors --coop --coep --corp ./build/docs-app

I thought coincident would default to the comlink behavior when SharedArrayBuffer are not available, but it looks like it's not the case.

@jtpio
Copy link
Member Author

jtpio commented Jul 17, 2024

Switching to coincident on the worker side does seem to be fixing jupyterlite/jupyterlite#1424.

coincident-widget-error.webm

I thought coincident would default to the comlink behavior when SharedArrayBuffer are not available, but it looks like it's not the case.

Maybe we need to clearly separate the case when SharedArrayBuffer are available on the page from when they are not. Meaning using coincident when they are, and comlink when they are not (on both the worker and the main threads).

cc @martinRenou in case you have some input on this

@martinRenou
Copy link
Member

Meaning using coincident when they are, and comlink when they are not (on both the worker and the main threads).

That's unfortunate, but if it's the only solution than let's go for it.

coincident is really taking the burden of implementing a proper communication protocol using SharedArrayBuffers, so it's really useful to us in that case. That's unfortunate it cannot work in an asynchronous fashion though, closer to the comlink approach.

@martinRenou
Copy link
Member

Thanks for looking into it by the way 👍🏽 I'm swamped with other projects. We should apply similar changes to jupyterlite/xeus once we have a proper solution.

@bollwyvl
Copy link
Contributor

bollwyvl commented Jul 17, 2024 via email

@jtpio
Copy link
Member Author

jtpio commented Jul 17, 2024

Looks like we could keep the current logic of going over the SharedArrayBuffer if they are available, and default to the service worker when they are not.

If the SharedArrayBuffers are available, we use coincident on both the main thread and the worker. If they are not we use comlink.

@jtpio
Copy link
Member Author

jtpio commented Jul 17, 2024

I'll push a first draft towards that direction and will clean things up a bit afterwards.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could technically keep both entrypoints in a single file. But having a separate file for coincident and comlink may be easier to reason about.

@jtpio jtpio changed the title Switch from postMessage to coincident Use coincident when crossOriginIsolated, comlink otherwise Jul 17, 2024
@jtpio jtpio changed the title Use coincident when crossOriginIsolated, comlink otherwise Use coincident if crossOriginIsolated, comlink otherwise Jul 17, 2024
@jtpio jtpio mentioned this pull request Jul 22, 2024
Copy link
Member

@martinRenou martinRenou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@jtpio
Copy link
Member Author

jtpio commented Jul 23, 2024

Thanks for the review!

@jtpio jtpio merged commit 42e18ca into jupyterlite:main Jul 23, 2024
11 checks passed
@jtpio jtpio deleted the coincident branch July 23, 2024 12:37
@martenrichter
Copy link
Contributor

This PR caused the breakage of interact, see:
#143

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Uncaught TypeError happened in Pydodide intialization Uncaught TypeError: i is undefined
4 participants