Merge remote-tracking branch 'origin/fix/45-skillscriptrun-ignores-context-cannot-be-canceledtimed-out'

This commit is contained in:
sam committed 2026-07-29 23:15:58 +10:00
commit 366e8f412b
4 files changed
+140 -17

No files matched your search

+59 -15
View File
@@ -7,8 +7,10 @@ package skillscript
import (
"bytes"
"context"
"fmt"
"os"
"time"
"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib/unrestricted"
@@ -16,31 +18,73 @@ import (
"github.com/samcharles93/archie-core/internal/yaegiutil"
)
// defaultTimeout protects callers that use Run (the legacy, context-free
// signature) from a script that blocks forever. Callers that have a
// context should use RunContext instead.
const defaultTimeout = 1 * time.Second
// Run interprets the Go script at path and executes its main function,
// returning everything it wrote to stdout and stderr. The script runs
// in-process (interpreted, not sandboxed) with unrestricted symbols
// (os/exec included -- these scripts wrap external tools the way a shell
// script would), so a panic inside it is recovered and returned as an
// error rather than taking down the daemon.
//
// Run is a convenience wrapper around RunContext with a short built-in
// timeout. Callers that need precise cancellation or a different deadline
// should use RunContext directly.
func Run(path string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
return RunContext(ctx, path)
}
// RunContext interprets the Go script at path and executes its main
// function, returning everything it wrote to stdout and stderr.
// The script is executed in a goroutine; if ctx is cancelled or exceeds
// its deadline before the script finishes, RunContext returns the
// context error and abandons the goroutine (yaegi does not support
// external cancellation of an in-progress eval).
func RunContext(ctx context.Context, path string) (string, error) {
if _, statErr := os.Stat(path); statErr != nil {
return "", statErr
}
return yaegiutil.Safe(path, func() (string, error) {
var buf bytes.Buffer
i, err := yaegiutil.New(interp.Options{Stdout: &buf, Stderr: &buf}, unrestricted.Symbols)
if err != nil {
return "", err
}
// Check context before spawning the goroutine.
if err := ctx.Err(); err != nil {
return "", err
}
// EvalPath treats a package-main source file the way `go run` does:
// evaluating it executes its main() directly. Unlike `go build`, it
// does not complain about a missing main() -- such a script simply
// produces no output.
if _, err := i.EvalPath(path); err != nil {
return "", fmt.Errorf("yaegi: run %s: %w", path, err)
}
return buf.String(), nil
})
type result struct {
output string
err error
}
ch := make(chan result, 1)
go func() {
out, err := yaegiutil.Safe(path, func() (string, error) {
var buf bytes.Buffer
i, err := yaegiutil.New(interp.Options{Stdout: &buf, Stderr: &buf}, unrestricted.Symbols)
if err != nil {
return "", err
}
// EvalPath treats a package-main source file the way `go run` does:
// evaluating it executes its main() directly. Unlike `go build`, it
// does not complain about a missing main() -- such a script simply
// produces no output.
if _, err := i.EvalPath(path); err != nil {
return "", fmt.Errorf("yaegi: run %s: %w", path, err)
}
return buf.String(), nil
})
ch <- result{out, err}
}()
select {
case r := <-ch:
return r.output, r.err
case <-ctx.Done():
return "", ctx.Err()
}
}
+34
View File
@@ -4,6 +4,7 @@ import (
"os"
"path/filepath"
"testing"
"time"
)
func writeScript(t *testing.T, src string) string {
@@ -92,3 +93,36 @@ func TestRunMissingFile(t *testing.T) {
t.Fatal("Run() error = nil, want an error for a missing file")
}
}
// TestRunBlocksIndefinitelyOnBlockingScript proves that skillscript.Run
// has no cancellation mechanism. A skill script that blocks forever
// (select{}) wedges the calling goroutine permanently. Every other
// execution path in archie-core supports context cancellation
// (exec.CommandContext in SubprocessRunner, context.Cause checks in
// InProcessRunner), but Run cannot be canceled or timed out because it
// takes no context.Context and calls i.EvalPath synchronously.
func TestRunBlocksIndefinitelyOnBlockingScript(t *testing.T) {
path := writeScript(t, `package main
func main() {
select {}
}
`)
done := make(chan struct{})
go func() {
Run(path)
close(done)
}()
select {
case <-done:
// Run returned from a script that should block forever.
// This is unexpected — the script contains select{}.
t.Log("Run returned unexpectedly from a select{} script")
case <-time.After(2 * time.Second):
t.Error("skillscript.Run blocks forever on a select{} script: " +
"Run needs a context.Context parameter for cancellation " +
"(issue #45)")
}
}