Skip to content

bugfix: Kill compile processes that generates too much output #2883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .licenses/go/github.com/arduino/go-paths-helper.dep.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: github.com/arduino/go-paths-helper
version: v1.12.1
version: v1.13.0
type: go
summary:
homepage: https://pkg.go.dev/github.com/arduino/go-paths-helper
Expand Down
6 changes: 3 additions & 3 deletions .licenses/go/golang.org/x/sys/execabs.dep.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
name: golang.org/x/sys/execabs
version: v0.31.0
version: v0.32.0
type: go
summary: Package execabs is a drop-in replacement for os/exec that requires PATH lookups
to find absolute paths.
homepage: https://pkg.go.dev/golang.org/x/sys/execabs
license: other
licenses:
- sources: sys@v0.31.0/LICENSE
- sources: sys@v0.32.0/LICENSE
text: |
Copyright 2009 The Go Authors.

Expand Down Expand Up @@ -36,7 +36,7 @@ licenses:
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- sources: sys@v0.31.0/PATENTS
- sources: sys@v0.32.0/PATENTS
text: |
Additional IP Rights Grant (Patents)

Expand Down
6 changes: 3 additions & 3 deletions .licenses/go/golang.org/x/sys/unix.dep.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
name: golang.org/x/sys/unix
version: v0.31.0
version: v0.32.0
type: go
summary: Package unix contains an interface to the low-level operating system primitives.
homepage: https://pkg.go.dev/golang.org/x/sys/unix
license: bsd-3-clause
licenses:
- sources: sys@v0.31.0/LICENSE
- sources: sys@v0.32.0/LICENSE
text: |
Copyright 2009 The Go Authors.

Expand Down Expand Up @@ -35,7 +35,7 @@ licenses:
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- sources: sys@v0.31.0/PATENTS
- sources: sys@v0.32.0/PATENTS
text: |
Additional IP Rights Grant (Patents)

Expand Down
2 changes: 1 addition & 1 deletion .licenses/go/golang.org/x/term.dep.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: golang.org/x/term
version: v0.30.0
version: v0.31.0
type: go
summary: Package term provides support functions for dealing with terminals, as commonly
found on UNIX systems.
Expand Down
6 changes: 3 additions & 3 deletions .licenses/go/golang.org/x/text/encoding.dep.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
name: golang.org/x/text/encoding
version: v0.23.0
version: v0.24.0
type: go
summary: Package encoding defines an interface for character encodings, such as Shift
JIS and Windows 1252, that can convert to and from UTF-8.
homepage: https://pkg.go.dev/golang.org/x/text/encoding
license: other
licenses:
- sources: text@v0.23.0/LICENSE
- sources: text@v0.24.0/LICENSE
text: |
Copyright 2009 The Go Authors.

Expand Down Expand Up @@ -36,7 +36,7 @@ licenses:
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- sources: text@v0.23.0/PATENTS
- sources: text@v0.24.0/PATENTS
text: |
Additional IP Rights Grant (Patents)

Expand Down
6 changes: 3 additions & 3 deletions .licenses/go/golang.org/x/text/encoding/internal.dep.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
name: golang.org/x/text/encoding/internal
version: v0.23.0
version: v0.24.0
type: go
summary: Package internal contains code that is shared among encoding implementations.
homepage: https://pkg.go.dev/golang.org/x/text/encoding/internal
license: other
licenses:
- sources: text@v0.23.0/LICENSE
- sources: text@v0.24.0/LICENSE
text: |
Copyright 2009 The Go Authors.

Expand Down Expand Up @@ -35,7 +35,7 @@ licenses:
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- sources: text@v0.23.0/PATENTS
- sources: text@v0.24.0/PATENTS
text: |
Additional IP Rights Grant (Patents)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: golang.org/x/text/encoding/internal/identifier
version: v0.23.0
version: v0.24.0
type: go
summary: Package identifier defines the contract between implementations of Encoding
and Index by defining identifiers that uniquely identify standardized coded character
Expand All @@ -10,7 +10,7 @@ summary: Package identifier defines the contract between implementations of Enco
homepage: https://pkg.go.dev/golang.org/x/text/encoding/internal/identifier
license: other
licenses:
- sources: text@v0.23.0/LICENSE
- sources: text@v0.24.0/LICENSE
text: |
Copyright 2009 The Go Authors.

Expand Down Expand Up @@ -39,7 +39,7 @@ licenses:
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- sources: text@v0.23.0/PATENTS
- sources: text@v0.24.0/PATENTS
text: |
Additional IP Rights Grant (Patents)

Expand Down
6 changes: 3 additions & 3 deletions .licenses/go/golang.org/x/text/encoding/unicode.dep.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
name: golang.org/x/text/encoding/unicode
version: v0.23.0
version: v0.24.0
type: go
summary: Package unicode provides Unicode encodings such as UTF-16.
homepage: https://pkg.go.dev/golang.org/x/text/encoding/unicode
license: other
licenses:
- sources: text@v0.23.0/LICENSE
- sources: text@v0.24.0/LICENSE
text: |
Copyright 2009 The Go Authors.

Expand Down Expand Up @@ -35,7 +35,7 @@ licenses:
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- sources: text@v0.23.0/PATENTS
- sources: text@v0.24.0/PATENTS
text: |
Additional IP Rights Grant (Patents)

Expand Down
6 changes: 3 additions & 3 deletions .licenses/go/golang.org/x/text/internal/utf8internal.dep.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
name: golang.org/x/text/internal/utf8internal
version: v0.23.0
version: v0.24.0
type: go
summary: Package utf8internal contains low-level utf8-related constants, tables, etc.
homepage: https://pkg.go.dev/golang.org/x/text/internal/utf8internal
license: other
licenses:
- sources: text@v0.23.0/LICENSE
- sources: text@v0.24.0/LICENSE
text: |
Copyright 2009 The Go Authors.

Expand Down Expand Up @@ -35,7 +35,7 @@ licenses:
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- sources: text@v0.23.0/PATENTS
- sources: text@v0.24.0/PATENTS
text: |
Additional IP Rights Grant (Patents)

Expand Down
6 changes: 3 additions & 3 deletions .licenses/go/golang.org/x/text/runes.dep.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
name: golang.org/x/text/runes
version: v0.23.0
version: v0.24.0
type: go
summary: Package runes provide transforms for UTF-8 encoded text.
homepage: https://pkg.go.dev/golang.org/x/text/runes
license: bsd-3-clause
licenses:
- sources: text@v0.23.0/LICENSE
- sources: text@v0.24.0/LICENSE
text: |
Copyright 2009 The Go Authors.

Expand Down Expand Up @@ -35,7 +35,7 @@ licenses:
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- sources: text@v0.23.0/PATENTS
- sources: text@v0.24.0/PATENTS
text: |
Additional IP Rights Grant (Patents)

Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ replace github.com/mailru/easyjson => github.com/cmaglie/easyjson v0.8.1
require (
fortio.org/safecast v1.0.0
github.com/ProtonMail/go-crypto v1.1.6
github.com/arduino/go-paths-helper v1.12.1
github.com/arduino/go-paths-helper v1.13.0
github.com/arduino/go-properties-orderedmap v1.8.1
github.com/arduino/go-serial-utils v0.1.2
github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b
Expand Down Expand Up @@ -40,9 +40,9 @@ require (
go.bug.st/f v0.4.0
go.bug.st/relaxed-semver v0.15.0
go.bug.st/testifyjson v1.3.0
golang.org/x/sys v0.31.0
golang.org/x/term v0.30.0
golang.org/x/text v0.23.0
golang.org/x/sys v0.32.0
golang.org/x/term v0.31.0
golang.org/x/text v0.24.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f
google.golang.org/grpc v1.71.1
google.golang.org/protobuf v1.36.6
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/ProtonMail/go-crypto v1.1.6/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQA
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
github.com/arduino/go-paths-helper v1.0.1/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck=
github.com/arduino/go-paths-helper v1.12.1 h1:WkxiVUxBjKWlLMiMuYy8DcmVrkxdP7aKxQOAq7r2lVM=
github.com/arduino/go-paths-helper v1.12.1/go.mod h1:jcpW4wr0u69GlXhTYydsdsqAjLaYK5n7oWHfKqOG6LM=
github.com/arduino/go-paths-helper v1.13.0 h1:HIkgg8ChPw1QPNHkB5bQSs+geTj74vf6TFgVhm/9mmw=
github.com/arduino/go-paths-helper v1.13.0/go.mod h1:jcpW4wr0u69GlXhTYydsdsqAjLaYK5n7oWHfKqOG6LM=
github.com/arduino/go-properties-orderedmap v1.8.1 h1:nU5S6cXPwMoxZs4ORw61wPTALNfriIduvNB4cxTmNYM=
github.com/arduino/go-properties-orderedmap v1.8.1/go.mod h1:DKjD2VXY/NZmlingh4lSFMEYCVubfeArCsGPGDwb2yk=
github.com/arduino/go-serial-utils v0.1.2 h1:MRFwME4w/uaVkJ1R+wzz4KSbI9cF9IDVrYorazvjpTk=
Expand Down Expand Up @@ -235,14 +235,14 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y=
golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g=
golang.org/x/term v0.31.0 h1:erwDkOK1Msy6offm1mOgvspSkslFnIGsFnxOKoufg3o=
golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f h1:OxYkA3wjPsZyBylwymxSHa7ViiW1Sml4ToBrncvFehI=
Expand Down
41 changes: 37 additions & 4 deletions internal/arduino/builder/internal/preprocessor/gcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package preprocessor

import (
"bytes"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -77,10 +78,42 @@ func GCC(
if err != nil {
return Result{}, err
}
stdout, stderr, err := proc.RunAndCaptureOutput(ctx)

// Append gcc arguments to stdout
stdout = append([]byte(fmt.Sprintln(strings.Join(args, " "))), stdout...)
stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)

return Result{args: proc.GetArgs(), stdout: stdout, stderr: stderr}, err
ctx, cancel := context.WithCancel(ctx)
defer cancel()
count := 0
stderrLimited := writerFunc(func(p []byte) (int, error) {
// Limit the size of the stderr buffer to 100KB
n, err := stderr.Write(p)
count += n
if count > 100*1024 {
cancel()
fmt.Fprintln(stderr, i18n.Tr("Compiler error output has been truncated."))
Comment on lines +93 to +94
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
cancel()
fmt.Fprintln(stderr, i18n.Tr("Compiler error output has been truncated."))
fmt.Fprintln(stderr, i18n.Tr("Compiler error output has been truncated."))
cancel()

Probably I'd swap just in case the context cancellation unblocks the WaitWithinContext and returns the stderr bytes, before we appended the error string.

}
return n, err
})

proc.RedirectStdoutTo(stdout)
proc.RedirectStderrTo(stderrLimited)

// Append gcc arguments to stdout before running the command
fmt.Fprintln(stdout, strings.Join(args, " "))

if err := proc.Start(); err != nil {
return Result{}, err
}

// Wait for the process to finish
err = proc.WaitWithinContext(ctx)

return Result{args: proc.GetArgs(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
}

type writerFunc func(p []byte) (n int, err error)

func (f writerFunc) Write(p []byte) (n int, err error) {
return f(p)
}
24 changes: 17 additions & 7 deletions internal/integrationtest/arduino-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"io"
"maps"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -190,12 +191,16 @@ func (cli *ArduinoCLI) Run(args ...string) ([]byte, []byte, error) {
return cli.RunWithCustomEnv(cli.cliEnvVars, args...)
}

// RunWithContext executes the given arduino-cli command with the given context and returns the output.
// If the context is canceled, the command is killed.
func (cli *ArduinoCLI) RunWithContext(ctx context.Context, args ...string) ([]byte, []byte, error) {
return cli.RunWithCustomEnvContext(ctx, cli.cliEnvVars, args...)
}

// GetDefaultEnv returns a copy of the default execution env used with the Run method.
func (cli *ArduinoCLI) GetDefaultEnv() map[string]string {
res := map[string]string{}
for k, v := range cli.cliEnvVars {
res[k] = v
}
maps.Copy(cli.cliEnvVars, res)
return res
}

Expand Down Expand Up @@ -324,8 +329,13 @@ func (cli *ArduinoCLI) InstallMockedAvrdude(t *testing.T) {

// RunWithCustomEnv executes the given arduino-cli command with the given custom env and returns the output.
func (cli *ArduinoCLI) RunWithCustomEnv(env map[string]string, args ...string) ([]byte, []byte, error) {
return cli.RunWithCustomEnvContext(context.Background(), env, args...)
}

// RunWithCustomEnv executes the given arduino-cli command with the given custom env and returns the output.
func (cli *ArduinoCLI) RunWithCustomEnvContext(ctx context.Context, env map[string]string, args ...string) ([]byte, []byte, error) {
var stdoutBuf, stderrBuf bytes.Buffer
err := cli.run(&stdoutBuf, &stderrBuf, nil, env, args...)
err := cli.run(ctx, &stdoutBuf, &stderrBuf, nil, env, args...)

errBuf := stderrBuf.Bytes()
cli.t.NotContains(string(errBuf), "panic: runtime error:", "arduino-cli panicked")
Expand All @@ -336,15 +346,15 @@ func (cli *ArduinoCLI) RunWithCustomEnv(env map[string]string, args ...string) (
// RunWithCustomInput executes the given arduino-cli command pushing the given input stream and returns the output.
func (cli *ArduinoCLI) RunWithCustomInput(in io.Reader, args ...string) ([]byte, []byte, error) {
var stdoutBuf, stderrBuf bytes.Buffer
err := cli.run(&stdoutBuf, &stderrBuf, in, cli.cliEnvVars, args...)
err := cli.run(context.Background(), &stdoutBuf, &stderrBuf, in, cli.cliEnvVars, args...)

errBuf := stderrBuf.Bytes()
cli.t.NotContains(string(errBuf), "panic: runtime error:", "arduino-cli panicked")

return stdoutBuf.Bytes(), errBuf, err
}

func (cli *ArduinoCLI) run(stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader, env map[string]string, args ...string) error {
func (cli *ArduinoCLI) run(ctx context.Context, stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader, env map[string]string, args ...string) error {
if cli.cliConfigPath != nil {
args = append([]string{"--config-file", cli.cliConfigPath.String()}, args...)
}
Expand Down Expand Up @@ -402,8 +412,8 @@ func (cli *ArduinoCLI) run(stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader
}
}()
}
cliErr := cliProc.WaitWithinContext(ctx)
wg.Wait()
cliErr := cliProc.Wait()
fmt.Fprintln(terminalOut, color.HiBlackString("<<< Run completed (err = %v)", cliErr))

return cliErr
Expand Down
Loading
Loading