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

feat: make permission prompter thread-local #27161

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
47 changes: 26 additions & 21 deletions runtime/permissions/prompter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,22 @@ pub enum PromptResponse {
AllowAll,
}

static PERMISSION_PROMPTER: Lazy<Mutex<Box<dyn PermissionPrompter>>> =
Lazy::new(|| Mutex::new(Box::new(TtyPrompter)));
thread_local! {
static PERMISSION_PROMPTER: Lazy<Mutex<Box<dyn PermissionPrompter>>> =
Lazy::new(|| Mutex::new(Box::new(TtyPrompter)));

static MAYBE_BEFORE_PROMPT_CALLBACK: Lazy<Mutex<Option<PromptCallback>>> =
Lazy::new(|| Mutex::new(None));
static MAYBE_BEFORE_PROMPT_CALLBACK: Lazy<Mutex<Option<PromptCallback>>> =
Lazy::new(|| Mutex::new(None));

static MAYBE_AFTER_PROMPT_CALLBACK: Lazy<Mutex<Option<PromptCallback>>> =
Lazy::new(|| Mutex::new(None));
static MAYBE_AFTER_PROMPT_CALLBACK: Lazy<Mutex<Option<PromptCallback>>> =
Lazy::new(|| Mutex::new(None));

static MAYBE_CURRENT_STACKTRACE: Lazy<Mutex<Option<Vec<JsStackFrame>>>> =
Lazy::new(|| Mutex::new(None));
static MAYBE_CURRENT_STACKTRACE: Lazy<Mutex<Option<Vec<JsStackFrame>>>> =
Lazy::new(|| Mutex::new(None));
}

pub fn set_current_stacktrace(trace: Vec<JsStackFrame>) {
*MAYBE_CURRENT_STACKTRACE.lock() = Some(trace);
MAYBE_CURRENT_STACKTRACE.with(|t| *t.lock() = Some(trace));
}

pub fn permission_prompt(
Expand All @@ -66,29 +68,32 @@ pub fn permission_prompt(
api_name: Option<&str>,
is_unary: bool,
) -> PromptResponse {
if let Some(before_callback) = MAYBE_BEFORE_PROMPT_CALLBACK.lock().as_mut() {
before_callback();
}
let stack = MAYBE_CURRENT_STACKTRACE.lock().take();
MAYBE_BEFORE_PROMPT_CALLBACK.with(|cb| {
if let Some(before_callback) = cb.lock().as_mut() {
before_callback();
}
});
let stack = MAYBE_CURRENT_STACKTRACE.with(|t| t.lock().take());
let r = PERMISSION_PROMPTER
.lock()
.prompt(message, flag, api_name, is_unary, stack);
if let Some(after_callback) = MAYBE_AFTER_PROMPT_CALLBACK.lock().as_mut() {
after_callback();
}
.with(|p| p.lock().prompt(message, flag, api_name, is_unary, stack));
MAYBE_AFTER_PROMPT_CALLBACK.with(|cb| {
if let Some(after_callback) = cb.lock().as_mut() {
after_callback();
}
});
r
}

pub fn set_prompt_callbacks(
before_callback: PromptCallback,
after_callback: PromptCallback,
) {
*MAYBE_BEFORE_PROMPT_CALLBACK.lock() = Some(before_callback);
*MAYBE_AFTER_PROMPT_CALLBACK.lock() = Some(after_callback);
MAYBE_BEFORE_PROMPT_CALLBACK.with(|cb| *cb.lock() = Some(before_callback));
MAYBE_AFTER_PROMPT_CALLBACK.with(|cb| *cb.lock() = Some(after_callback));
Comment on lines +91 to +92
Copy link
Member

Choose a reason for hiding this comment

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

This is now incorrect - if these are thread locals then set_prompt_callback needs to be called in each worker thread spawned - you'd need to update op_create_worker in runtime/ops/worker_host.rs

Copy link
Author

Choose a reason for hiding this comment

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

I see! That makes sense. Thanks @bartlomieju!

I'll work on updating worker_host.rs. I have some ideas and will test them to see what works best.

}

pub fn set_prompter(prompter: Box<dyn PermissionPrompter>) {
*PERMISSION_PROMPTER.lock() = prompter;
PERMISSION_PROMPTER.with(|p| *p.lock() = prompter);
}

pub type PromptCallback = Box<dyn FnMut() + Send + Sync>;
Expand Down
Loading