Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Fix for issue #1167, added handling of errors which are returned by scaner #1253

Merged
merged 1 commit into from
Oct 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 58 additions & 19 deletions control/plugin/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,41 @@ func (e *ExecutablePlugin) Run(timeout time.Duration) (Response, error) {
e.cmd.Start()
e.captureStderr()
go func() {
for stdOutScanner.Scan() {
// The first chunk from the scanner is the plugin's response to the
// handshake. Once we've received that, we can begin to forward
// logs on to snapd's log.
if !respReceived {
respBytes := stdOutScanner.Bytes()
err = json.Unmarshal(respBytes, &resp)
respReceived = true
close(doneChan)
} else {
execLogger.WithFields(log.Fields{
"plugin": path.Base(e.cmd.Path()),
"io": "stdout",
}).Debug(stdOutScanner.Text())
for {
for stdOutScanner.Scan() {
// The first chunk from the scanner is the plugin's response to the
// handshake. Once we've received that, we can begin to forward
// logs on to snapd's log.
if !respReceived {
respBytes := stdOutScanner.Bytes()
err = json.Unmarshal(respBytes, &resp)
respReceived = true
close(doneChan)
} else {
execLogger.WithFields(log.Fields{
"plugin": path.Base(e.cmd.Path()),
"io": "stdout",
}).Debug(stdOutScanner.Text())
}
}

if errScanner := stdOutScanner.Err(); errScanner != nil {
reader := bufio.NewReader(e.stdout)
log, errRead := reader.ReadString('\n')
if errRead == io.EOF {
break
}

execLogger.
WithField("plugin", path.Base(e.cmd.Path())).
WithField("io", "stdout").
WithField("scanner_err", errScanner).
WithField("read_string_err", errRead).
Warn(log)

continue //scanner finished with errors so try to scan once again
}
break //scanner finished scanning without errors so break the loop
}
}()

Expand Down Expand Up @@ -167,12 +187,31 @@ func (e *ExecutablePlugin) Kill() error {
func (e *ExecutablePlugin) captureStderr() {
stdErrScanner := bufio.NewScanner(e.stderr)
go func() {
for stdErrScanner.Scan() {
execLogger.
WithField("io", "stderr").
WithField("plugin", path.Base(e.cmd.Path())).
Debug(stdErrScanner.Text())
for {
for stdErrScanner.Scan() {
execLogger.
WithField("plugin", path.Base(e.cmd.Path())).
WithField("io", "stderr").
Debug(stdErrScanner.Text())
}

if errScanner := stdErrScanner.Err(); errScanner != nil {
reader := bufio.NewReader(e.stderr)
log, errRead := reader.ReadString('\n')
if errRead == io.EOF {
break
}

execLogger.
WithField("plugin", path.Base(e.cmd.Path())).
WithField("io", "stderr").
WithField("scanner_err", errScanner).
WithField("read_string_err", errRead).
Warn(log)

continue //scanner finished with errors so try to scan once again
}
break //scanner finished scanning without errors so break the loop
}
}()
}
18 changes: 15 additions & 3 deletions plugin/collector/snap-plugin-collector-mock2/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ limitations under the License.
package mock

import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"strings"
"time"

Expand Down Expand Up @@ -57,14 +59,24 @@ func (f *Mock) CollectMetrics(mts []plugin.MetricType) ([]plugin.MetricType, err
rand.Seed(time.Now().UTC().UnixNano())
metrics := []plugin.MetricType{}
for i := range mts {
if c, ok := mts[i].Config().Table()["long_print"]; ok && c.(ctypes.ConfigValueBool).Value {
if c, ok := mts[i].Config().Table()["long_stdout_log"]; ok && c.(ctypes.ConfigValueBool).Value {
letterBytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
longLine := []byte{}
for i := 0; i < 8193; i++ {
for i := 0; i < bufio.MaxScanTokenSize; i++ {
longLine = append(longLine, letterBytes[rand.Intn(len(letterBytes))])
}
fmt.Println(string(longLine))
fmt.Fprintln(os.Stdout, string(longLine))
}

if c, ok := mts[i].Config().Table()["long_stderr_log"]; ok && c.(ctypes.ConfigValueBool).Value {
letterBytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
longLine := []byte{}
for i := 0; i < bufio.MaxScanTokenSize; i++ {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the goal here to append to longline until it is larger than the MaxScanTokenSize?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the goal is to have longline with length equal to MaxScanTokenSize. The condition in Scan() method has following form:

if len(s.buf) >= s.maxTokenSize || len(s.buf) > maxInt/2 {
                s.setErr(ErrTooLong)
                return false
}

longLine = append(longLine, letterBytes[rand.Intn(len(letterBytes))])
}
fmt.Fprintln(os.Stderr, string(longLine))
}

if c, ok := mts[i].Config().Table()["panic"]; ok && c.(ctypes.ConfigValueBool).Value {
panic("Oops!")
}
Expand Down