Bugfix: bthread_interrupt was scheduled to the wrong tag group#3351
Open
chenBright wants to merge 1 commit into
Open
Bugfix: bthread_interrupt was scheduled to the wrong tag group#3351chenBright wants to merge 1 commit into
chenBright wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes incorrect tag-group scheduling when interrupting/stopping bthreads from non-worker threads (e.g., Server::~Server()->Join()), aiming to preserve tag isolation by selecting the target TaskGroup based on the interrupted bthread’s tag.
Changes:
- Removes the
tagargument fromTaskGroup::interrupt()and switches remote re-scheduling to use the target task’s recorded tag. - Keeps
bthread_interrupt(bthread_t, bthread_tag_t)for API/ABI compatibility but ignores thetagparameter internally. - Removes
ButexBthreadWaiter::tagand routes butex wake/requeue scheduling viatask_meta->attr.tag.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| src/bthread/task_group.h | Updates TaskGroup::interrupt signature to remove the tag parameter. |
| src/bthread/task_group.cpp | Chooses remote task group for interrupted sleepers based on the target task’s tag. |
| src/bthread/butex.cpp | Removes stored waiter tag and attempts to schedule wakeups using task_meta->attr.tag. |
| src/bthread/bthread.cpp | Keeps bthread_interrupt signature but forwards to the new TaskGroup::interrupt overload. |
Comments suppressed due to low confidence (1)
src/bthread/task_group.cpp:1096
- The change routes interruption/wakeup scheduling based on a tag, but there’s no unit test covering the reported regression scenario (non-worker thread stops a sleeping bthread with tag>0 and it must be rescheduled within the same tag group). Adding a test would help prevent future regressions in tag isolation for
bthread_stop()/bthread_interrupt()and the usleep/butex wake paths.
int TaskGroup::interrupt(bthread_t tid, TaskControl* c) {
// Consume current_waiter in the TaskMeta, wake it up then set it back.
ButexWaiter* w = NULL;
uint64_t sleep_id = 0;
int rc = interrupt_and_consume_waiters(tid, &w, &sleep_id);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
dac6757 to
24306f3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
Issue Number: resolve #3349
Problem Summary:
bthread_stop()callsbthread_interrupt(tid), which uses the defaultargument
BTHREAD_TAG_DEFAULT = 0. When a non-worker thread (e.g. themain thread during
Server::~Server()->Join()) stops a bthread withtag > 0,TaskGroup::interrupt()takes thec->choose_one_group(tag)->ready_to_run_remote(m)path with
tag == 0. As a result, the interrupted bthread is re-scheduled onto aworker of tag 0 instead of its own tag group, breaking tag isolation.
What is changed and the side effects?
Changed:
The tag of a bthread is already recorded in its
TaskMeta(
m->attr.tag), so passing the tag explicitly is unnecessary anderror-prone. Instead of forwarding the (default) tag argument, the code
now reads the tag from the target
TaskMeta:bthread_interrupt(bthread_t, bthread_tag_t): thetagparameter isnow ignored (kept only for API/ABI compatibility) and the call
delegates to
TaskGroup::interrupt(tid, control).TaskGroup::interrupt(): thetagparameter is removed; the targetgroup is chosen via
c->choose_one_group(m->attr.tag).butex.cpp: the redundantButexBthreadWaiter::tagfield is removed;butex_wake,butex_wake_n,butex_wake_except,butex_requeueanderase_from_butexnow obtain the tag fromtask_meta->attr.tag.Side effects:
Performance effects:
Breaking backward compatibility:
Check List: