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 postMessage instead of a comlink produced MessageChannel #148

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/pyodide-kernel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@jupyterlite/contents": "^0.4.5",
"@jupyterlite/kernel": "^0.4.5",
"coincident": "^1.2.3",
"comlink": "^4.4.1"
"comlink": "^4.4.2"
},
"devDependencies": {
"@babel/core": "^7.22.17",
Expand Down
8 changes: 8 additions & 0 deletions packages/pyodide-kernel/src/comlink.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class PyodideDriveFS extends DriveFS {
}

export class PyodideComlinkKernel extends PyodideRemoteKernel {
constructor() {
super();
this._sendWorkerMessage = (msg: any) => {
// use postMessage, but in a format, that comlink would not process.
postMessage({ jMsg: msg });
Copy link
Member

Choose a reason for hiding this comment

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

Wondering if something a bit more explicit like _kernelMessage could also work? With or without the leading _, but having it could help communicate it's used for "internal" message handling?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, but then also the coincident worker needs to rename the change.

Copy link
Member

Choose a reason for hiding this comment

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

I meant for the jMsg key, we could still keep _sendWorkerMessage as the method name:

Suggested change
postMessage({ jMsg: msg });
postMessage({ _kernelMessage: msg });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, got it. and it is also unique enough that comlink ignores it. (comlink ignores it if no id field is set...)

};
}

/**
* Setup custom Emscripten FileSystem
*/
Expand Down
10 changes: 8 additions & 2 deletions packages/pyodide-kernel/src/kernel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import coincident from 'coincident';

import { Remote, proxy, wrap } from 'comlink';
import { Remote, wrap } from 'comlink';

import { PromiseDelegate } from '@lumino/coreutils';

Expand Down Expand Up @@ -86,7 +86,13 @@ export class PyodideKernel extends BaseKernel implements IKernel {
};
} else {
remote = wrap(this._worker) as IPyodideWorkerKernel;
remote.registerCallback(proxy(this._processWorkerMessage.bind(this)));
// we use the normal postMessage mechanism
this._worker.addEventListener('message', (ev) => {
if (typeof ev?.data?.jMsg !== 'undefined') {
// only process non comlink messages
this._processWorkerMessage(ev.data.jMsg);
}
});
}
const remoteOptions = this.initRemoteOptions(options);
remote.initialize(remoteOptions).then(this._ready.resolve.bind(this._ready));
Expand Down
11 changes: 9 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3061,7 +3061,7 @@ __metadata:
"@jupyterlite/kernel": ^0.4.5
"@types/jest": ^29.5.4
coincident: ^1.2.3
comlink: ^4.4.1
comlink: ^4.4.2
esbuild: ^0.19.2
jest: ^29.7.0
pyodide: 0.26.4
Expand Down Expand Up @@ -5941,13 +5941,20 @@ __metadata:
languageName: node
linkType: hard

"comlink@npm:^4.3.1, comlink@npm:^4.4.1":
"comlink@npm:^4.3.1":
version: 4.4.1
resolution: "comlink@npm:4.4.1"
checksum: 16d58a8f590087fc45432e31d6c138308dfd4b75b89aec0b7f7bb97ad33d810381bd2b1e608a1fb2cf05979af9cbfcdcaf1715996d5fcf77aeb013b6da3260af
languageName: node
linkType: hard

"comlink@npm:^4.4.2":
Copy link
Member

Choose a reason for hiding this comment

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

Looks like the two versions of comlink could be deduplicated with:

"deduplicate": "yarn-berry-deduplicate -s fewer --fail",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I will try this. I am not so fluent with yarn, I normally use npm. I'll be back in the evening at my dev computer.

Copy link
Member

Choose a reason for hiding this comment

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

Running jlpm deduplicate at the top-level should normally perform the deduplication step.

version: 4.4.2
resolution: "comlink@npm:4.4.2"
checksum: 35313f26fdd78c202be21be49a7607f57e9e91963399b524676742ee0cf7db589be500d923ed0809b53f37996461186fb345bf867d93236be22bfae3cb7bd07e
languageName: node
linkType: hard

"commander@npm:^10.0.1":
version: 10.0.1
resolution: "commander@npm:10.0.1"
Expand Down
Loading