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

fix: op_set_exit_code #13034

Merged
merged 5 commits into from
Dec 11, 2021
Merged

fix: op_set_exit_code #13034

merged 5 commits into from
Dec 11, 2021

Conversation

bartlomieju
Copy link
Member

@bartlomieju bartlomieju commented Dec 9, 2021

Fixes "op_set_exit_code" by sharing a single "Arc" between
all workers (via "op state") instead of having a "global" value stored in
"deno_runtime" crate. As a consequence setting an exit code is always
scoped to a tree of workers, instead of being overridable if there are
multiple worker tree (like in "deno test --jobs" subcommand).

Refactored "cli/main.rs" functions to return "Result<i32, AnyError>" instead
of "Result<(), AnyError>" so they can return exit code.

Ref #12938

@bartlomieju bartlomieju changed the title [WIP] fix: set_exit_code op fix: set_exit_code op Dec 10, 2021
@bartlomieju
Copy link
Member Author

@bnoordhuis please take a look

@bartlomieju bartlomieju changed the title fix: set_exit_code op fix: op_set_exit_code Dec 10, 2021

let code = deno_runtime::EXIT_CODE.load(Relaxed);
std::process::exit(code);
std::process::exit(exit_code);
Copy link
Member Author

Choose a reason for hiding this comment

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

I think we should only call exit if exit_code is not equal to 0. Otherwise drop handlers will not be called which might lead to zombie subprocesses.

Copy link
Contributor

Choose a reason for hiding this comment

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

What drop handlers are you worried about? The real work is scoped to run_basic(), right?

Copy link
Member Author

Choose a reason for hiding this comment

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

All drop handlers - IIRC if you call std::process:exit the process immediately terminates and no drop handlers are run; when we run a subprocess using Deno.run() we got close_on_drop set to true, and when you are existing using exit() these drop handlers will not run and subprocesses will not close becoming zombies.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm still confused. Are you talking about lexically scoped drop handlers? main() doesn't have any that matter, does it?

(I'm also confused by your close_on_drop comment - where is that a thing inside the source tree?)

Copy link
Member Author

Choose a reason for hiding this comment

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

Please scratch that, I should have commented before the first coffee :) of course drop handlers run because they are scoped to run_basic(). And in case of close_on_drop I actually meant kill_on_drop

Comment on lines +1007 to +1009
// TODO(bartlomieju): this function is not handling `exit_code` set by the runtime
// code properly.
async fn run_with_watch(flags: Flags, script: String) -> Result<i32, AnyError> {
Copy link
Member Author

Choose a reason for hiding this comment

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

Should be addressed in tandem with #7590

Copy link
Contributor

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

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

Nice work.


let code = deno_runtime::EXIT_CODE.load(Relaxed);
std::process::exit(code);
std::process::exit(exit_code);
Copy link
Contributor

Choose a reason for hiding this comment

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

What drop handlers are you worried about? The real work is scoped to run_basic(), right?


pub fn init() -> Extension {
pub fn init(maybe_exit_code: Option<Arc<AtomicI32>>) -> Extension {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it have to be an Arc? It's only accessed from the same thread, I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

It has to, it's shares between all created threads (ie. web workers).

@bartlomieju
Copy link
Member Author

I'm going to land this PR and we can address other questions at a later point.

@bartlomieju bartlomieju merged commit 0dec9b4 into denoland:main Dec 11, 2021
@bartlomieju bartlomieju deleted the set_exit_code branch December 11, 2021 14:56
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