diff --git a/deps/oblib/src/lib/task/ob_timer.cpp b/deps/oblib/src/lib/task/ob_timer.cpp index bed0f2979..d41e73559 100644 --- a/deps/oblib/src/lib/task/ob_timer.cpp +++ b/deps/oblib/src/lib/task/ob_timer.cpp @@ -445,6 +445,13 @@ void ObTimer::run1() } } + if (NULL == running_task_) + { + // If running_task_ is NULL, the token is not associated with any task, + // so we reset it to avoid the task being scheduled unexpectedly. + token.reset(); + } + if (token.task != NULL && running_task_ != NULL && !is_destroyed_ && !is_stopped_) { bool timeout_check = token.task->timeout_check(); const int64_t start_time = ::oceanbase::common::ObTimeUtility::current_time(); diff --git a/deps/oblib/src/lib/task/ob_timer.h b/deps/oblib/src/lib/task/ob_timer.h index 7a9dde6b9..c966eae1a 100644 --- a/deps/oblib/src/lib/task/ob_timer.h +++ b/deps/oblib/src/lib/task/ob_timer.h @@ -97,6 +97,12 @@ private: Token(): scheduled_time(0), delay(0), task(NULL) {} Token(const int64_t st, const int64_t dt, ObTimerTask *task) : scheduled_time(st), delay(dt), task(task) {} + void reset() + { + scheduled_time = 0; + delay = 0; + task = NULL; + } TO_STRING_KV(K(scheduled_time), K(delay), KP(task), KPC(task)); int64_t scheduled_time; int64_t delay; diff --git a/deps/oblib/unittest/lib/task/test_cancel_task.cpp b/deps/oblib/unittest/lib/task/test_cancel_task.cpp index cbd5b7a36..d63480191 100644 --- a/deps/oblib/unittest/lib/task/test_cancel_task.cpp +++ b/deps/oblib/unittest/lib/task/test_cancel_task.cpp @@ -184,6 +184,26 @@ TEST(TestCancelTask, reschedule_self_and_cancel) timer.destroy(); } +// case6: cancel-->wait_task +TEST(TestCancelTask, cancel_and_wait_task) +{ + ObTimer timer; + ASSERT_EQ(OB_SUCCESS, timer.init()); + ASSERT_TRUE(timer.inited()); + ASSERT_EQ(OB_SUCCESS, timer.start()); + + TaskCommon task; + task.exec_time_ = 10000; // 10ms + ASSERT_EQ(OB_SUCCESS, timer.schedule(task, 500000, true)); // 500ms true + ::usleep(800000); // 800ms + ASSERT_EQ(1, task.task_run_count_); + timer.cancel_task(task); + timer.wait_task(task); + ::usleep(1000000); // 1s + ASSERT_EQ(1, task.task_run_count_); + timer.destroy(); +} + } // end namespace common } // end namespace oceanbase