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

Web workers #1993

Merged
merged 27 commits into from Apr 1, 2019
Merged

Web workers #1993

merged 27 commits into from Apr 1, 2019

Conversation

afinch7
Copy link
Contributor

@afinch7 afinch7 commented Mar 23, 2019

part of #1955 fixes

  • tests/error_004_missing_module.test
  • tests/error_005_missing_dynamic_import.test
  • tests/error_006_import_ext_failure.test

part of #1222

part of #1047

cli/workers.rs Outdated Show resolved Hide resolved
cli/msg.fbs Show resolved Hide resolved
cli/web_worker_behavior.rs Outdated Show resolved Hide resolved
@ry
Copy link
Member

ry commented Mar 24, 2019

@afinch7 Have a look at the following disabled tests:

tests/error_004_missing_module.disabled
tests/error_005_missing_dynamic_import.disabled
tests/error_006_import_ext_failure.disabled

They involve the compiler worker. The problem, I think, is the exit(1) here:

deno/cli/workers.rs

Lines 88 to 91 in 129eae0

if let Err(err) = r {
eprintln!("{}", JSErrorColor(&err).to_string());
std::process::exit(1);
}

@afinch7 afinch7 marked this pull request as ready for review March 26, 2019 18:15
@afinch7
Copy link
Contributor Author

afinch7 commented Mar 26, 2019

@afinch7 Have a look at the following disabled tests:

tests/error_004_missing_module.disabled
tests/error_005_missing_dynamic_import.disabled
tests/error_006_import_ext_failure.disabled

They involve the compiler worker. The problem, I think, is the exit(1) here:
deno/cli/workers.rs

Lines 88 to 91 in 129eae0

if let Err(err) = r {
eprintln!("{}", JSErrorColor(&err).to_string());
std::process::exit(1);
}

Maybe we need a second resource for worker like a "stderr" for each worker.

@afinch7 afinch7 closed this Mar 26, 2019
@afinch7 afinch7 reopened this Mar 26, 2019
@ry
Copy link
Member

ry commented Mar 26, 2019

@afinch7 My inclination is that any JSError hit inside the worker should be set back to the caller. In the case of compile_sync() it would be good if it returned Result<ModuleMetaData, JSError>

deno/cli/compiler.rs

Lines 126 to 131 in c43cfed

pub fn compile_sync(
parent_state: Arc<IsolateState>,
specifier: &str,
referrer: &str,
module_meta_data: &ModuleMetaData,
) -> ModuleMetaData {

In the case of the compiler, the caller is clear. But in general it is not... so not sure how to handle that.

@afinch7
Copy link
Contributor Author

afinch7 commented Mar 26, 2019

I will at least give it a try for sure before I finish this commit. Maybe poll after each call or use futures::future::select_all?

@ry
Copy link
Member

ry commented Mar 26, 2019

select_all is a very interesting idea!

You'll have to change it so both the worker and main isolate are running in the "main" tokio executor. That is, change the thread spawn into a tokio::spawn

@afinch7
Copy link
Contributor Author

afinch7 commented Mar 27, 2019

It took me way too long to figure out that tokio was waiting on the compiler worker future to complete after the main isolate finished. I ended up having to use a second tokio runtime for the compiler worker. I also decided against compile_sync returning a result for now. It might make sense to wrap compile() in compiler.ts with a try catch and post back a object with a success field, so we can have compiler failures and report them without causing the compiler worker isolate to exit.

cli/compiler.rs Show resolved Hide resolved
cli/compiler.rs Outdated Show resolved Hide resolved
cli/isolate_state.rs Outdated Show resolved Hide resolved
cli/isolate_state.rs Outdated Show resolved Hide resolved
@ry
Copy link
Member

ry commented Mar 29, 2019

It seems to be green now. Nice! What was the unit_test hanging problem?

@afinch7
Copy link
Contributor Author

afinch7 commented Mar 29, 2019

Tokio scheduling. Had to use the same runtime for both spawns. e6a3bab

BUILD.gn Outdated Show resolved Hide resolved
static ref C_SHARED: Mutex<Option<CompilerShared>> = Mutex::new(None);
// tokio runtime specifically for spawning logic that is dependent on
// completetion of the compiler worker future
static ref C_RUNTIME: Mutex<Runtime> = Mutex::new(Runtime::new().unwrap());
Copy link
Member

@ry ry Mar 30, 2019

Choose a reason for hiding this comment

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

This runtime is different than the tokio runtime? (defined by crate::tokio_util::run)
Ideally we could get all isolates using the same runtime.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried that, but this leads to tokio waiting indefinitely for the compiler worker to exit. The compiler worker only has two ways to exit right now: calling workerClose as the worker or throwing a uncaught error. Maybe work on a better way to terminate the compiler worker later?

let union =
futures::future::select_all(vec![worker_receiver, local_receiver.shared()]);

match union.wait() {
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to refactor this into a compile_async and compile_sync which calls the async version? I'd like to experiment with parallel compilation at some point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe in another PR? This would require a major rework of communication with the compiler worker. There is no way to relate responses directly to requests currently, and also no way to relate errors back to requests or even handle those errors without the compiler exiting(permanently).

cli/workers.rs Outdated Show resolved Hide resolved
cli/workers.rs Outdated Show resolved Hide resolved
Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

I'd like to land this PR soon - it's getting quite big and I think it's already an improvement over the existing code base. I'm worried it's going to get difficult to rebase.

My main concern now is removing the worker specific declaration/bundle/snapshot - I think it's too much complexity. Sharing the main isolate's tokio runtime is my other concern - but that can be done later if it's too much to do now.

@afinch7 afinch7 changed the title [WIP] Web workers Web workers Mar 31, 2019
Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

LGTM!

This is a massive improvement - thank you very much! I guess we'll have to iterate a bit more on workers - mostly I'd like to get them working in the same Tokio runtime.

@ry ry merged commit b0a23be into denoland:master Apr 1, 2019
@ry ry mentioned this pull request Apr 1, 2019
justjavac pushed a commit to justjavac/deno that referenced this pull request Apr 2, 2019
* Refactored the way worker polling is scheduled and errors are handled.
* Share the worker future as a Shared
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.

None yet

2 participants