-
Notifications
You must be signed in to change notification settings - Fork 31.3k
src: improve thread safety of TaskQueue
#57910
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
base: main
Are you sure you want to change the base?
Conversation
cfdeed7
to
6b85518
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #57910 +/- ##
========================================
Coverage 90.25% 90.25%
========================================
Files 630 630
Lines 185691 185944 +253
Branches 36407 36443 +36
========================================
+ Hits 167589 167827 +238
- Misses 10994 11012 +18
+ Partials 7108 7105 -3
🚀 New features to boost your workflow:
|
6b85518
to
0ffd2a4
Compare
0ffd2a4
to
f1b6765
Compare
@codebytere I wonder if this could also help with #54918. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with some suggestions.
std::vector<std::unique_ptr<Task>> tasks_to_run; | ||
{ | ||
auto locked = scheduler->tasks_.Lock(); | ||
std::unique_ptr<Task> task; | ||
while ((task = locked.Pop())) { | ||
tasks_to_run.push_back(std::move(task)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::vector<std::unique_ptr<Task>> tasks_to_run; | |
{ | |
auto locked = scheduler->tasks_.Lock(); | |
std::unique_ptr<Task> task; | |
while ((task = locked.Pop())) { | |
tasks_to_run.push_back(std::move(task)); | |
} | |
} | |
auto tasks_to_run = scheduler->tasks_.Lock().PopAll(); |
That's equivalent, right?
std::vector<std::unique_ptr<DelayedTask>> delayed_tasks_to_schedule; | ||
{ | ||
auto locked_tasks = foreground_delayed_tasks_.Lock(); | ||
std::unique_ptr<DelayedTask> delayed; | ||
while ((delayed = locked_tasks.Pop())) { | ||
did_work = true; | ||
delayed_tasks_to_schedule.push_back(std::move(delayed)); | ||
} | ||
} | ||
|
||
for (auto& delayed : delayed_tasks_to_schedule) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above:
std::vector<std::unique_ptr<DelayedTask>> delayed_tasks_to_schedule; | |
{ | |
auto locked_tasks = foreground_delayed_tasks_.Lock(); | |
std::unique_ptr<DelayedTask> delayed; | |
while ((delayed = locked_tasks.Pop())) { | |
did_work = true; | |
delayed_tasks_to_schedule.push_back(std::move(delayed)); | |
} | |
} | |
for (auto& delayed : delayed_tasks_to_schedule) { | |
auto delayed_tasks_to_schedule = foreground_delayed_tasks_.Lock().PopAll(); | |
for (auto& delayed : delayed_tasks_to_schedule) { | |
did_work = true; |
auto foreground_tasks_locked = foreground_tasks_.Lock(); | ||
auto foreground_delayed_tasks_locked = foreground_delayed_tasks_.Lock(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment to their definitions in node_platform.h explaining the locks should always be acquired in this order?
Initial effort to fix #56236.
Improve thread safety of TaskQueue by making locking of TaskQueue explicit and thus hopefully preventing a crash due to
uv_async_send
being called withnullptr
.Please let me know if i headed in the wrong direction with this and i'll rework it!