Skip to content

feat(node): worker_threads#1151

Merged
kt3k merged 10 commits intodenoland:mainfrom
Mesteery:worker_threads
Mar 22, 2022
Merged

feat(node): worker_threads#1151
kt3k merged 10 commits intodenoland:mainfrom
Mesteery:worker_threads

Conversation

@Mesteery
Copy link
Copy Markdown
Contributor

No description provided.

@Mesteery
Copy link
Copy Markdown
Contributor Author

CI errors are related to denoland/deno#9696.

@Mesteery Mesteery marked this pull request as draft August 23, 2021 00:07
Copy link
Copy Markdown
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

This looks good @Mesteery, but please some more tests that actually execute some code in workers.

@Mesteery
Copy link
Copy Markdown
Contributor Author

Yes of course, the more complete tests are already added locally.
The problem is that it's not completely ready, since the implementation of events differs from Node.js (and this causes some problems with worker_threads). So I will rewrite it.

Copy link
Copy Markdown
Contributor Author

@Mesteery Mesteery left a comment

Choose a reason for hiding this comment

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

This is almost ready except for...

Comment thread node/worker_threads.ts Outdated
@@ -0,0 +1,217 @@
/// <reference lib="webworker"/>
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The tests fail systematically because of the typing errors involved in this reference, so the solution to make it work is --no-check. Do you know another way?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@kitsonk could you please advice on this situation?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does /// <reference lib="deno.worker" /> fix it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment thread node/worker_threads_test.ts Outdated
@Mesteery Mesteery requested a review from bartlomieju August 26, 2021 22:41
@Mesteery Mesteery marked this pull request as ready for review August 27, 2021 11:27
@Mesteery Mesteery marked this pull request as draft September 7, 2021 04:04
@bartlomieju
Copy link
Copy Markdown
Member

Ooops, looks like I borked the merge.

@Mesteery
Copy link
Copy Markdown
Contributor Author

no worries, it's fixed

@bartlomieju
Copy link
Copy Markdown
Member

bartlomieju commented Oct 11, 2021

@Mesteery I discussed this offline with @kitsonk. We believe the way forward right now is to use declare global { } inside this module and not depend on triple-slash reference. There's a bunch of errors coming from usages of globalThis.<property> that could be resolved this way. There are also a few types that are completely missing and TS is complaining about them being any by default.

There's also this definition:

export const isMainThread = typeof DedicatedWorkerGlobalScope === "undefined" ||
  self instanceof DedicatedWorkerGlobalScope === false;

AFAIK DedicatedWorkerGlobalScope is just a type and cannot be used as a value. I suggest to check for existence of window instead (it is only defined in "main" worker in Deno).

Would be happy to land it for tomorrow's release if you manage to fix those errors.

@lucacasonato
Copy link
Copy Markdown
Contributor

AFAIK DedicatedWorkerGlobalScope is just a type and cannot be used as a value. I suggest to check for existence of window instead (it is only defined in "main" worker in Deno).

It's an exposed interface too: https://html.spec.whatwg.org/multipage/workers.html#dedicatedworkerglobalscope

@bartlomieju
Copy link
Copy Markdown
Member

AFAIK DedicatedWorkerGlobalScope is just a type and cannot be used as a value. I suggest to check for existence of window instead (it is only defined in "main" worker in Deno).

It's an exposed interface too: https://html.spec.whatwg.org/multipage/workers.html#dedicatedworkerglobalscope

Ooops, silly me, I checked it in Chrome's console without spawning a worker 🤦‍♂️

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Oct 18, 2021

CLA assistant check
All committers have signed the CLA.

@kitsonk
Copy link
Copy Markdown
Contributor

kitsonk commented Oct 18, 2021

I've amended the files so they type check now.

Side note, I know we went for declare class in our type definitions, but only things declared as var are available on globalThis, which means you've got to jump through some ugly hoops to get the type checking to work.

Comment thread node/events.ts Outdated

emitter.once(name, eventListener);
return;
emitter.once("error", errorListener);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This line is now failing "Can add once event listener to EventTarget via standalone function" test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay, i will fix this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I disabled this test for now and opened #1455

@guybedford
Copy link
Copy Markdown
Contributor

It's a shame this one got derailed, the PR seemed to be very close!

@bartlomieju
Copy link
Copy Markdown
Member

@guybedford this PR is more or less complete, however there's still problem with TS types. I wonder if we should maybe skip typings completely for the time being and just land it.

@kt3k
Copy link
Copy Markdown
Contributor

kt3k commented Feb 1, 2022

I think I managed to fix type errors. PTAL @bartlomieju @kitsonk

@bartlomieju
Copy link
Copy Markdown
Member

@kt3k this seems fine to me, but I will let @kitsonk take a look before landing.

@bartlomieju
Copy link
Copy Markdown
Member

This is a blocker for parcel, so I'm going to land this PR tonight.

Copy link
Copy Markdown
Member

@littledivy littledivy left a comment

Choose a reason for hiding this comment

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

require and compat APIs are not available to workers. Can we please add that? EDIT: Needs support in CLI

@Mesteery
Copy link
Copy Markdown
Contributor Author

Mesteery commented Feb 6, 2022

require and compat APIs are not available to workers. Can we please add that? EDIT: Needs support in CLI

Sorry but I am not familiar with Deno. What do you mean by that?

@bartlomieju
Copy link
Copy Markdown
Member

require and compat APIs are not available to workers. Can we please add that? EDIT: Needs support in CLI

Sorry but I am not familiar with Deno. What do you mean by that?

This is something I will work on, currently worker API doesn't respect "compat" mode and doesn't add required globals.

@piscisaureus piscisaureus assigned bartlomieju and unassigned kt3k Feb 8, 2022
Comment thread node/worker_threads.ts
@bartlomieju
Copy link
Copy Markdown
Member

After a lot back and forth I found the root problem of the implementation of this PR. Currently tests fails with "Module execution is still pending but there are no pending ops..." suggesting that there's a promise that never resolves. I found that this is caused by parentHandle (which is somewhat an alias of self) is not actually an EventEmitter instance and so events are not properly dispatched. I'm not sure how to solve this problem, if I understood correctly we would have to make self (in the worker context) an instance of EventEmitter but I'm unsure how this could be achieved. Any ideas are welcome.

Alternatively we would have apply a lot of code from EventEmitter manually to copy the behavior (though it still might not be enough due to instanceof checks)

@kt3k kt3k self-assigned this Mar 22, 2022
Copy link
Copy Markdown
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for finishing this @kt3k. And thank you to @Mesteery sorry it took so long to land this!

Copy link
Copy Markdown
Member

@littledivy littledivy left a comment

Choose a reason for hiding this comment

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

:shipit:

Copy link
Copy Markdown
Contributor

@kt3k kt3k left a comment

Choose a reason for hiding this comment

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

@Mesteery Thank you for your initial work!

@kt3k kt3k merged commit 13d9e91 into denoland:main Mar 22, 2022
@Mesteery Mesteery deleted the worker_threads branch March 23, 2022 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants