Skip to content

Commit

Permalink
Fix tkn p start --showlog by handling informer ResourceEventHandler
Browse files Browse the repository at this point in the history
tkn p start --showlog command was failing as informer ResourceEventHandler
was calling UpdateFunc after closing channel so it was crashing and showing
error `sending data on closed channel`

so this patch handles informer ResourceEventHandler by synchronization and checks
if channel is closed then return and does nothing

Signed-off-by: ShiV Verma <[email protected]>
  • Loading branch information
pratap0007 committed Jun 1, 2023
1 parent c656a28 commit 0652c63
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions pkg/pipelinerun/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package pipelinerun

import (
"context"
"sync"
"time"

"github.com/tektoncd/cli/pkg/actions"
Expand Down Expand Up @@ -67,10 +68,9 @@ func (t *Tracker) Monitor(allowed []string) <-chan []taskrunpkg.Run {

genericInformer, _ := factory.ForResource(*gvr)
informer := genericInformer.Informer()

mu := &sync.Mutex{}
stopC := make(chan struct{})
trC := make(chan []taskrunpkg.Run)

go func() {
<-stopC
close(trC)
Expand All @@ -97,7 +97,6 @@ func (t *Tracker) Monitor(allowed []string) <-chan []taskrunpkg.Run {
return
}
pr.DeepCopyInto(&pipelinerunConverted)

trC <- t.findNewTaskruns(&pipelinerunConverted, allowed, trsMap)

if hasCompleted(&pipelinerunConverted) {
Expand All @@ -107,8 +106,38 @@ func (t *Tracker) Monitor(allowed []string) <-chan []taskrunpkg.Run {

informer.AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: eventHandler,
UpdateFunc: func(_, newObj interface{}) { eventHandler(newObj) },
AddFunc: func(obj interface{}) {
// To ensure synchonization and checks is the stopC channel has received a signal to stop
// If it receives a signal then return and does nothing
mu.Lock()
defer mu.Unlock()
select {
case <-stopC:
return
default:
eventHandler(obj)
}
},
UpdateFunc: func(_, newObj interface{}) {
mu.Lock()
defer mu.Unlock()
select {
case <-stopC:
return
default:
eventHandler(newObj)
}
},
DeleteFunc: func(obj interface{}) {
mu.Lock()
defer mu.Unlock()
select {
case <-stopC:
return
default:
eventHandler(obj)
}
},
},
)

Expand Down

0 comments on commit 0652c63

Please sign in to comment.