Skip to content

Commit

Permalink
Bug Fix: Race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunKoyalwar committed May 25, 2022
1 parent bea078b commit 8aee6f5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
6 changes: 5 additions & 1 deletion cmd/talosplus/subcmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/spf13/cobra"
"github.com/tarunKoyalwar/talosplus/pkg/alerts"
"github.com/tarunKoyalwar/talosplus/pkg/core"
"github.com/tarunKoyalwar/talosplus/pkg/ioutils"
"github.com/tarunKoyalwar/talosplus/pkg/shared"
"github.com/tarunKoyalwar/talosplus/pkg/shell"
)
Expand All @@ -16,7 +17,7 @@ import (
var (
limit = 4 // max number of concurrent programs
Purge = false
Verbose = true
Verbose = false

CachedDir = os.TempDir()
DiscordId = ""
Expand Down Expand Up @@ -73,6 +74,9 @@ Settings Like Purge, CacheDIR , pname etc can also be set using "get|set"
shell.Settings.CacheDIR = cdirenv
}

//debug mode
ioutils.Cout.Verbose = Verbose

// Configure Notifications if given
SetupAlerts()

Expand Down
5 changes: 5 additions & 0 deletions pkg/core/crux.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ func (s *Scripter) Execute() {
s.Backup = append(s.Backup, b)

} else {
instance := s.IndexedCMDs[resp.Uid]
if instance != nil {
ioutils.Cout.Printf("[Failed] %v\n", strings.Join(instance.CMD.Cmdsplit, " "))
}

ioutils.Cout.Printf("[-] %v responded with error %v\n", resp.Uid, resp.Err)
}
}
Expand Down
14 changes: 5 additions & 9 deletions pkg/gopool/pool.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
package gopool

import (
"reflect"
"sync"

"github.com/tarunKoyalwar/talosplus/pkg/shell"
)

const mutexLocked = 1

func MutexLocked(m *sync.Mutex) bool {
state := reflect.ValueOf(m).Elem().FieldByName("state")
return state.Int()&mutexLocked == mutexLocked
}

// Pool : Goroutine Pool with Limit
type Pool struct {
Concurrency int
Expand All @@ -30,6 +22,7 @@ type Pool struct {
logic *sync.WaitGroup // Handles shared Working of Pool
Wg *sync.WaitGroup // Wait Group For Launched Jobs
JobMutex *sync.Mutex
JobSent int
}

// AddJob
Expand All @@ -40,6 +33,7 @@ func (p *Pool) AddJob(t *shell.CMDWrap) {
}

p.JobChannel <- j
p.JobSent += 1
}

// AddJobWithId
Expand All @@ -50,6 +44,7 @@ func (p *Pool) AddJobWithId(t *shell.CMDWrap, uid string) {
}

p.JobChannel <- j
p.JobSent += 1
}

// Launcher : (shared)Launches New Jobs assings workers etc
Expand Down Expand Up @@ -162,12 +157,13 @@ func (p *Pool) Wait() {

p.JobMutex.Lock()

if p.JobCount == 0 {
if p.JobCount == 0 && p.JobSent == 0 {
p.JobMutex.Unlock()
return
}
p.JobMutex.Unlock()
<-p.Completion
p.JobSent = 0
}

func NewPool(size int) *Pool {
Expand Down
7 changes: 5 additions & 2 deletions pkg/shell/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,12 @@ func (s *SimpleCMD) Run() error {
if err != nil {
ioutils.Cout.PrintWarning("[error] %v", err)
s.Failed = true
return err
}
io.WriteString(stdin, s.CPipeIn.String())
stdin.Close()
func() {
defer stdin.Close()
io.WriteString(stdin, s.CPipeIn.String())
}()
}

err := c.Run()
Expand Down

0 comments on commit 8aee6f5

Please sign in to comment.