gf icon indicating copy to clipboard operation
gf copied to clipboard

gtimer close function is invalid

Open crisis111 opened this issue 8 months ago • 3 comments

Go version

go1.23

GoFrame version

2.9.0

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

tm := gtimer.New() tm.AddOnce(ctx, time.Millisecond*9, func(ctx context.Context) { fmt.Println("hello") }) time.Sleep(time.Second * 2) tm.Close() time.Sleep(time.Second * 10)

What did you see happen?

tm的tinker 还在跑,没有退出

What did you expect to see?

tm.Close() 应该退出loop 协程吧 ,但是没有退出 。

crisis111 avatar Mar 21 '25 09:03 crisis111

没有复现你说的情况

func TestIssue4216(t *testing.T) {
	ctx := t.Context()

	tm := New()
	tm.AddOnce(ctx, 9*time.Millisecond, func(ctx context.Context) {
		fmt.Println("hello")
	})
	time.Sleep(2 * time.Second)
	tm.Close()
	time.Sleep(10 * time.Second)
}
=== RUN   TestIssue4216
hello
--- PASS: TestIssue4216 (12.00s)
PASS

y1jiong avatar Mar 24 '25 05:03 y1jiong

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


No recurring what you said

func TestIssue4216(t *testing.T) {
	ctx := t.Context()

	tm := New()
	tm.AddOnce(ctx, 9*time.Millisecond, func(ctx context.Context) {
		fmt.Println("hello")
	})
	time.Sleep(2 * time.Second)
	tm.Close()
	time.Sleep(10 * time.Second)
}
=== RUN TestIssue4216
hello
--- PASS: TestIssue4216 (12.00s)
PASS

Issues-translate-bot avatar Mar 24 '25 05:03 Issues-translate-bot

抱歉上次没有说清楚 ,Timer.loop方法,在loop协程退出后,还是会打印"测试退出" 这个字符串,timker 没有退出,我没找到不退出的原因 。

// loop starts the ticker using a standalone goroutine.
func (t *Timer) loop() {
	var (
		currentTimerTicks   int64
		timerIntervalTicker = time.NewTicker(t.options.Interval)
	)
	defer timerIntervalTicker.Stop()
	for {
		select {
		case <-timerIntervalTicker.C:
			// Check the timer status.
			fmt.Println("测试退出")
			switch t.status.Val() {
			case StatusRunning:
				// Timer proceeding.
				if currentTimerTicks = t.ticks.Add(1); currentTimerTicks >= t.queue.NextPriority() {
					t.proceed(currentTimerTicks)
				}

			case StatusStopped:
				// Do nothing.

			case StatusClosed:
				// Timer exits.
				return
			}
		}
	}
}

func TestHel(t *testing.T) {
	fmt.Println("hello")
	timer := gtimer.New()

	//fmt.Println("start", time.Now())
	timer.Add(ctx, 200*time.Millisecond, func(ctx context.Context) {
		fmt.Println("job1", time.Now())
	})
	timer.Close()
	time.Sleep(time.Second * 100)
}

crisis111 avatar Mar 31 '25 07:03 crisis111