Skip to content

Don't execute the proxy queue while adding to it from same thread. #24565

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
32 changes: 24 additions & 8 deletions system/lib/pthread/proxying.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ static em_proxying_queue system_proxying_queue = {
.capacity = 0,
};

static _Thread_local int system_queue_in_use = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

We could use bool here along with true/false but maybe thats a bigger/unnecessary change.

Copy link
Member

Choose a reason for hiding this comment

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

For whatever reason I decided to use int instead of bool when I wrote all this, so this is at least consistent with the rest of the file.


em_proxying_queue* emscripten_proxy_get_system_queue(void) {
return &system_proxying_queue;
}
Expand Down Expand Up @@ -110,17 +112,24 @@ void emscripten_proxy_execute_queue(em_proxying_queue* q) {
assert(q != NULL);
assert(pthread_self());

// Recursion guard to avoid infinite recursion when we arrive here from the
// pthread_lock call below that executes the system queue. The per-task_queue
// recursion lock can't catch these recursions because it can only be checked
// after the lock has been acquired.
static _Thread_local int executing_system_queue = 0;
// Below is a recursion and deadlock guard:
// The recursion guard is to avoid infinite recursion when we arrive here from
// the pthread_lock call below that executes the system queue. The
// per-task_queue recursion lock can't catch these recursions because it can
// only be checked after the lock has been acquired.
//
// This also guards against deadlocks when adding to the system queue. When
// the current thread is adding tasks, it locks the queue, but we can
// potentially try to execute the queue during the add (from
// emscripten_yield). This will deadlock the thread, so only try to take the
Comment on lines +123 to +124
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// potentially try to execute the queue during the add (from
// emscripten_yield). This will deadlock the thread, so only try to take the
// potentially try to execute the queue during the add (from
// emscripten_yield when malloc takes a lock). This will deadlock the thread, so only try to take the

// lock if the current thread is not using the queue. We then hope the
// queue is executed later when it is unlocked.
int is_system_queue = q == &system_proxying_queue;
if (is_system_queue) {
if (executing_system_queue) {
if (system_queue_in_use) {
return;
}
executing_system_queue = 1;
system_queue_in_use = 1;
}

pthread_mutex_lock(&q->mutex);
Expand All @@ -133,14 +142,21 @@ void emscripten_proxy_execute_queue(em_proxying_queue* q) {
}

if (is_system_queue) {
executing_system_queue = 0;
system_queue_in_use = 0;
}
}

static int do_proxy(em_proxying_queue* q, pthread_t target_thread, task t) {
assert(q != NULL);
pthread_mutex_lock(&q->mutex);
int is_system_queue = q == &system_proxying_queue;
if (is_system_queue) {
system_queue_in_use = 1;
}
em_task_queue* tasks = get_or_add_tasks_for_thread(q, target_thread);
if (is_system_queue) {
system_queue_in_use = 0;
}
pthread_mutex_unlock(&q->mutex);
if (tasks == NULL) {
return 0;
Expand Down
1 change: 1 addition & 0 deletions test/other/codesize/test_codesize_minimal_pthreads.funcs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ $emscripten_futex_wake
$emscripten_stack_get_current
$emscripten_stack_set_limits
$free_ctx
$get_or_add_tasks_for_thread
Copy link
Member

Choose a reason for hiding this comment

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

I wonder what happened here.

$get_tasks_for_thread
$init_active_ctxs
$init_file_lock
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_pthreads.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19396
19446
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ $emscripten_futex_wake
$emscripten_stack_get_current
$emscripten_stack_set_limits
$free_ctx
$get_or_add_tasks_for_thread
$get_tasks_for_thread
$init_active_ctxs
$init_file_lock
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19397
19447