From 49a2fcc138da11cef927eecd590e4d876098fd67 Mon Sep 17 00:00:00 2001 From: caicandong Date: Mon, 24 Jul 2023 07:07:53 +0000 Subject: [PATCH] fix endless loop (#306) fix endless loop in poll relate #305 Co-authored-by: CaiCandong <1290147055@qq.com> Reviewed-on: https://gitea.com/gitea/act_runner/pulls/306 Co-authored-by: caicandong Co-committed-by: caicandong --- internal/app/poll/poller.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/app/poll/poller.go b/internal/app/poll/poller.go index 2c7a5e8..2ee2b8a 100644 --- a/internal/app/poll/poller.go +++ b/internal/app/poll/poller.go @@ -46,28 +46,28 @@ func (p *Poller) Poll(ctx context.Context) { func (p *Poller) poll(ctx context.Context, wg *sync.WaitGroup, limiter *rate.Limiter) { defer wg.Done() for { - p.pollTaskWithRateLimit(ctx, limiter) + if err := limiter.Wait(ctx); err != nil { + if ctx.Err() != nil { + log.WithError(err).Debug("limiter wait failed") + } + return + } + task, ok := p.fetchTask(ctx) + if !ok { + continue + } + p.runTaskWithRecover(ctx, task) } } -func (p *Poller) pollTaskWithRateLimit(ctx context.Context, limiter *rate.Limiter) { +func (p *Poller) runTaskWithRecover(ctx context.Context, task *runnerv1.Task) { defer func() { if r := recover(); r != nil { err := fmt.Errorf("panic: %v", r) - log.WithError(err).Error("panic in pollTaskWithRateLimit") + log.WithError(err).Error("panic in runTaskWithRecover") } }() - if err := limiter.Wait(ctx); err != nil { - if ctx.Err() != nil { - log.WithError(err).Debug("limiter wait failed") - } - return - } - task, ok := p.fetchTask(ctx) - if !ok { - return - } if err := p.runner.Run(ctx, task); err != nil { log.WithError(err).Error("failed to run task") }