[CP] [bugfix] fix the bug that the task be scheduled after being canceled

This commit is contained in:
obdev 2024-07-09 14:59:57 +00:00 committed by ob-robot
parent 1c005383a2
commit 301b1128d6
3 changed files with 33 additions and 0 deletions

View File

@ -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();

View File

@ -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;

View File

@ -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