How to handle “ERROR: IOError: closed stream” in Golang scan function?

I am using knife bootstrap command in Golang code to bootstrap nodes. Some times there is wait in recipe and it waits for more than 10 minutes. In such cases I am getting an error “Error: IOStream Closed”. Is there any way I can increase the time limit? Also I am continuously reading the logs from output and error stream of cmd. Any help would be highly appreciated.

func executeCMDWorkstation(cmd *exec.Cmd, projectId, cr_id string) bool {

stdout, err3 := cmd.StdoutPipe()
if err3 != nil {
    utils.Info.Println(err3)
    return false
}
defer stdout.Close()
stderr, err4 := cmd.StderrPipe()
if err4 != nil {
    utils.Info.Println(err4)
    return false
}
defer stderr.Close()

//cmd.Stdout = os.Stdout
//cmd.Stderr = os.Stderr
multi := io.MultiReader(stdout, stderr)
inp := bufio.NewScanner(multi)
err := cmd.Start()
if err != nil {
    utils.Info.Println("Error while creating client", err)
    return false
}
fmt.Println("Generating logs**************")
for inp.Scan() {

    line := inp.Text()
    if cr_id == "" {
        lg.SendDebugLogs(line, "projects/logs", projectId, "chef")
    } else {
        lg.SendDebugLogs(line, "changeRequests/logs", cr_id, "chef")
    }
}
exitStatus := cmd.Wait()
if exiterr, ok := exitStatus.(*exec.ExitError); ok {
    utils.Info.Println("************ Exit Status is:", exiterr, "************")
    return false
}

return true

After looking into some forums, Go Docs and trying some code changes, finally replacing the scan method in bufio to ReadLine of textproto worked for me.

I replaced the following code:

inp := bufio.NewScanner(multi)
err := cmd.Start()
if err != nil {
    utils.Info.Println("Error while creating client", err)
    return false
}
fmt.Println("Generating logs**************")
for inp.Scan() {
    line := inp.Text()
    if cr_id == "" {
        lg.SendDebugLogs(line, "projects/logs", projectId, "chef")
    } else {
        lg.SendDebugLogs(line, "changeRequests/logs", cr_id, "chef")
    }
}

With the following code:

    inp1 := bufio.NewReader(multi)

	tp := textproto.NewReader( inp1 )

	err := cmd.Start()
	if err != nil {
		utils.Info.Println("Error while creating client", err)
		return false
	}
	fmt.Println("Generating logs**************")

	/*
	New code for logs starts here
	 */


	for {
		line, readErr := tp.ReadLine()

		if readErr != nil {
			if line != "Closed IO Stream Error"{
				break
			}
		}

		if cr_id == "" {
			lg.SendDebugLogs(line, "projects/logs", projectId, "chef")
		} else {
			lg.SendDebugLogs(line, "changeRequests/logs", cr_id, "chef")
		}
	}

At last, this modification worked perfectly fine for me by handling immediate restarts as well as long sleeps/waits in the chef recipe or scripts. Basically I changed the stream reader I hope this will be helpful for anyone else facing the same problem :slight_smile:

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