Skip to content

Commit f60af19

Browse files
authored
Fix bthread_usleep return unexpected ESTOP (#2511)
1 parent 09b9600 commit f60af19

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

config_brpc.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ if [ "$SYSTEM" = "Darwin" ]; then
196196
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_MallocExtension_ReleaseFreeMemory"
197197
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_ProfilerStart"
198198
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_ProfilerStop"
199-
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,__Z13GetStackTracePPvii"
199+
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,__Z13GetStackTracePPvii"
200200
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_RegisterThriftProtocol"
201201
fi
202202
append_linking() {

src/bthread/task_group.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ int TaskGroup::init(size_t runqueue_capacity) {
229229
LOG(FATAL) << "Fail to get TaskMeta";
230230
return -1;
231231
}
232+
m->sleep_failed = false;
232233
m->stop = false;
233234
m->interrupted = false;
234235
m->about_to_quit = false;
@@ -372,6 +373,7 @@ int TaskGroup::start_foreground(TaskGroup** pg,
372373
return ENOMEM;
373374
}
374375
CHECK(m->current_waiter.load(butil::memory_order_relaxed) == NULL);
376+
m->sleep_failed = false;
375377
m->stop = false;
376378
m->interrupted = false;
377379
m->about_to_quit = false;
@@ -431,6 +433,7 @@ int TaskGroup::start_background(bthread_t* __restrict th,
431433
return ENOMEM;
432434
}
433435
CHECK(m->current_waiter.load(butil::memory_order_relaxed) == NULL);
436+
m->sleep_failed = false;
434437
m->stop = false;
435438
m->interrupted = false;
436439
m->about_to_quit = false;
@@ -759,7 +762,8 @@ void TaskGroup::_add_sleep_event(void* void_args) {
759762
butil::microseconds_from_now(e.timeout_us));
760763

761764
if (!sleep_id) {
762-
// fail to schedule timer, go back to previous thread.
765+
e.meta->sleep_failed = true;
766+
// Fail to schedule timer, go back to previous thread.
763767
g->ready_to_run(e.tid);
764768
return;
765769
}
@@ -801,8 +805,9 @@ int TaskGroup::usleep(TaskGroup** pg, uint64_t timeout_us) {
801805
g->set_remained(_add_sleep_event, &e);
802806
sched(pg);
803807
g = *pg;
804-
if (e.meta->current_sleep == 0 && !e.meta->interrupted) {
805-
// Fail to `_add_sleep_event'.
808+
if (e.meta->sleep_failed) {
809+
// Fail to schedule timer, return error.
810+
e.meta->sleep_failed = false;
806811
errno = ESTOP;
807812
return -1;
808813
}

src/bthread/task_meta.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ struct TaskMeta {
5353
butil::atomic<ButexWaiter*> current_waiter;
5454
uint64_t current_sleep;
5555

56+
// A flag to mark if the Timer scheduling failed.
57+
bool sleep_failed;
58+
5659
// A builtin flag to mark if the thread is stopping.
5760
bool stop;
5861

0 commit comments

Comments
 (0)