Skip to content

Commit

Permalink
Fix bthread_usleep return unexpected ESTOP (apache#2511)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenBright authored and jiangdongzi committed Jan 31, 2024
1 parent f832d5d commit e80088a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config_brpc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ if [ "$SYSTEM" = "Darwin" ]; then
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_MallocExtension_ReleaseFreeMemory"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_ProfilerStart"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_ProfilerStop"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,__Z13GetStackTracePPvii"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,__Z13GetStackTracePPvii"
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_RegisterThriftProtocol"
fi
append_linking() {
Expand Down
11 changes: 8 additions & 3 deletions src/bthread/task_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ int TaskGroup::init(size_t runqueue_capacity) {
LOG(FATAL) << "Fail to get TaskMeta";
return -1;
}
m->sleep_failed = false;
m->stop = false;
m->interrupted = false;
m->about_to_quit = false;
Expand Down Expand Up @@ -372,6 +373,7 @@ int TaskGroup::start_foreground(TaskGroup** pg,
return ENOMEM;
}
CHECK(m->current_waiter.load(butil::memory_order_relaxed) == NULL);
m->sleep_failed = false;
m->stop = false;
m->interrupted = false;
m->about_to_quit = false;
Expand Down Expand Up @@ -431,6 +433,7 @@ int TaskGroup::start_background(bthread_t* __restrict th,
return ENOMEM;
}
CHECK(m->current_waiter.load(butil::memory_order_relaxed) == NULL);
m->sleep_failed = false;
m->stop = false;
m->interrupted = false;
m->about_to_quit = false;
Expand Down Expand Up @@ -759,7 +762,8 @@ void TaskGroup::_add_sleep_event(void* void_args) {
butil::microseconds_from_now(e.timeout_us));

if (!sleep_id) {
// fail to schedule timer, go back to previous thread.
e.meta->sleep_failed = true;
// Fail to schedule timer, go back to previous thread.
g->ready_to_run(e.tid);
return;
}
Expand Down Expand Up @@ -801,8 +805,9 @@ int TaskGroup::usleep(TaskGroup** pg, uint64_t timeout_us) {
g->set_remained(_add_sleep_event, &e);
sched(pg);
g = *pg;
if (e.meta->current_sleep == 0 && !e.meta->interrupted) {
// Fail to `_add_sleep_event'.
if (e.meta->sleep_failed) {
// Fail to schedule timer, return error.
e.meta->sleep_failed = false;
errno = ESTOP;
return -1;
}
Expand Down
3 changes: 3 additions & 0 deletions src/bthread/task_meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ struct TaskMeta {
butil::atomic<ButexWaiter*> current_waiter;
uint64_t current_sleep;

// A flag to mark if the Timer scheduling failed.
bool sleep_failed;

// A builtin flag to mark if the thread is stopping.
bool stop;

Expand Down

0 comments on commit e80088a

Please sign in to comment.