Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gtimer close function is invalid #4216

Open
crisis111 opened this issue Mar 21, 2025 · 3 comments
Open

gtimer close function is invalid #4216

crisis111 opened this issue Mar 21, 2025 · 3 comments
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@crisis111
Copy link

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 crisis111 added the bug It is confirmed a bug, but don't worry, we'll handle it. label Mar 21, 2025
@crisis111 crisis111 changed the title The call to the close function is invalid gtimer close function is invalid Mar 21, 2025
@y1jiong
Copy link
Contributor

y1jiong commented Mar 24, 2025

没有复现你说的情况

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
Copy link

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

@crisis111
Copy link
Author

crisis111 commented Mar 31, 2025

抱歉上次没有说清楚 ,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)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
Development

No branches or pull requests

3 participants