How to fix negative WaitGroup counter

Hello, please help me figure out the WaitGroup, I’m completely confused, the goroutine is executed once on the server and then crashes when you run the function again, displaying panic: sync: negative WaitGroup counter and the server crashes, I understand my function execution counter is 0 , but how to increase it, it does not react in any way, explain how to fix it, I have already tried adding WhiteGroup wherever possible, and creating a new one everywhere displays the same & {{} [0 0 0]} or & {{} [0 1 0]}

function itself

func Scan(cmdSc *pb.ScanCommandReq) []*models.Cmdrun {
wgAllScan := new(sync.WaitGroup)

var results []*models.Cmdrun

CheckFolder(cmdSc.TrgDst)

go cmdTicker()

ports := FPorts()

for _, port := range ports["segFir"] {
	scanFileRes := fmt.Sprint(cmdSc.TrgDst+"/"+cmdSc.TrgDst, time.Now().UnixNano()) + "def"
	wgAllScan.Add(1)

	fileXMLCreate(scanFileRes)

	go runScan(newScan, scanFileRes, port)
}

go func(wg *sync.WaitGroup) {
	for res := range resultsCh {
fmt.Println(wgAllScan)
// I output the WhiteGroup at the first execution it turns out as it 
// should be & {{} [1 8 0]} that is,  wait for 8 goroutines to 
//  complete, and the next time I press execute and outputs & {{} [0 0 0]} 

		if res.StateProc == 0 {
			host, err := ParseXMLResultFile(res.FileScan)
			if err != nil {
				log.Printf("Parsing file not done: %s", err)
			}

			results = append(results, host)

			err = os.Remove(res.FileScan)
			if err != nil {
				log.Fatal("Can't delete file", res.FileScan, err)
			}
		}
		wgAllScan.Done()
	}
}(wgAllScan)

wgAllScan.Wait()

return results
}

You’re adding to wgAllScan here:

What’s the len(ports) the 2nd time?

len(ports [“segFir”]) 8 like the first time

In general, if I use wgAllScan.Add (1) in

go func(wg *sync.WaitGroup) {
	for res := range resultsCh {
		wgAllScan.Add(1)
		fmt.Println(wgAllScan)

		if res.StateProc == 0 {
			host, err := ParseXMLResultFile(res.FileScan)
			if err != nil {
				log.Printf("Parsing file not done: %s", err)
			}

			results = append(results, host)

			err = os.Remove(res.FileScan)
			if err != nil {
				log.Fatal("Can't delete file", res.FileScan, err)
			}
		}

then the goroutine works out as much as needed, but then my main function will stop waiting for the goroutine and finish its execution

if I add wgAllScan.Add (1) to both the first loop and the second, then the function runs many times without completing but the counter never completes and leaks again.
I don’t understand how to proceed

I tried to put this together in the playground to see the channel behavior, but I cannot because there are some variables that aren’t shown in your code above: https://play.golang.org/p/HjusoualI8y

# play.ground
./prog.go:45:14: undefined: newScan
./prog.go:49:20: undefined: resultsCh
type StateScan struct {
StateProc int
FileScan  string
}
var resultsCh = make(chan *StateScan)
var chTicker = make(chan bool)

newScan := &ScanCmd{
	   SuScan:  models.SuScan,
	   ScanDef: models.ScanDef,
	   KeyFile: models.KeyFile,
	   KeyPort: models.KeyPort,
	   TrgDst:  cmdSc.TrgDst,
   }

Sorry, I thought it won’t come in handy, but you cannot execute the runScan command because it is accessing another program in the terminal

And function itself

func runScan(cmd *models.ScanCmd, fileRes, port string) {
newCmdName := cmd.SuScan + " " + cmd.ScanDef + " " + cmd.KeyPort + port + " " + cmd.KeyFile + " " + fileRes + " " + cmd.TrgDst

cmdScan := exec.Command("/bin/sh", "-c", newCmdName)

if err := cmdScan.Start(); err != nil {
	log.Fatalf("Not running command %s", err)
}

err := cmdScan.Wait()
if err != nil {
	log.Fatalf("Command finished with error: %v", err)
}

newStateScan := &models.StateScan{
	StateProc: cmdScan.ProcessState.ExitCode(),
	FileScan:  fileRes,
}

resultsCh <- newStateScan
}

Do you have a full repo that I can test with? It’s becoming a bit unwieldy in the playground.

Yes, one moment, send a pull request

I’m sorry I had to edit it a little if you suddenly start the server and use the program replaced it with the well-known NMAP

There is a lot of code, the file with which I have to solve the problem is the last scanning.go

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.