From 255f580cf68f710484f7a270dd28bae0759d1137 Mon Sep 17 00:00:00 2001 From: archie Date: Mon, 27 Jul 2026 03:01:56 +0000 Subject: [PATCH 1/2] test: failing repro for #43 --- internal/plugin/plugin_test.go | 92 ++++++++++++++++++++++++++++ internal/yaegiutil/yaegiutil_test.go | 33 ++++++++++ 2 files changed, 125 insertions(+) diff --git a/internal/plugin/plugin_test.go b/internal/plugin/plugin_test.go index 5da5dc8f5..5f8951ec9 100644 --- a/internal/plugin/plugin_test.go +++ b/internal/plugin/plugin_test.go @@ -4,6 +4,7 @@ import ( "os" "path/filepath" "testing" + "time" "github.com/samcharles93/archie-core/internal/plugin" "github.com/samcharles93/archie-core/internal/plugin/pluginextract" @@ -466,3 +467,94 @@ var Plugin = plugin._Plugin{ t.Errorf("LoadDir returned %d plugins from tagged file, want 1 (build tags are ignored in Yaegi)", len(plugins)) } } + +// ── Timeout tests (issue #43) ──────────────────────────────────────── +// +// LoadDir calls yaegiutil.Resolve which calls i.Eval(src) with no +// context, deadline, or goroutine+select guard. A plugin whose +// package-level init code never returns (e.g. select{} in init()) +// hangs the entire daemon startup forever. These tests prove the bug: +// they assert the CORRECT behaviour (timeout → skip the bad plugin) +// and FAIL today because LoadDir hangs before returning. + +func TestLoadDirTimeoutOnBlockingPlugin(t *testing.T) { + // A single plugin whose init() blocks forever must be skipped, + // not hang LoadDir indefinitely. The daemon must start with zero + // plugins and no error — the blocking file is logged and skipped. + + dir := t.TempDir() + if err := os.WriteFile(filepath.Join(dir, "blocking.go"), []byte(`package main + +func init() { select {} } +`), 0o644); err != nil { + t.Fatal(err) + } + + done := make(chan struct{}) + var plugins []plugin.Plugin + var loadErr error + go func() { + plugins, loadErr = plugin.LoadDir(dir, symbols) + close(done) + }() + + select { + case <-done: + if loadErr != nil { + t.Errorf("LoadDir returned error: %v", loadErr) + } + if len(plugins) != 0 { + t.Errorf("LoadDir returned %d plugins, want 0 (blocking plugin should be skipped)", len(plugins)) + } + case <-time.After(5 * time.Second): + t.Error("LoadDir did not return within 5s — plugin eval has no timeout (issue #43)") + } +} + +func TestLoadDirSkipsBlockingPluginButLoadsGoodOnes(t *testing.T) { + // A blocking plugin must not prevent good plugins from loading. + // LoadDir must return within the timeout with only the valid plugin. + + dir := t.TempDir() + // Blocking plugin first (sorted first by filename). + if err := os.WriteFile(filepath.Join(dir, "00-blocking.go"), []byte(`package main + +func init() { select {} } +`), 0o644); err != nil { + t.Fatal(err) + } + // Good plugin second. + if err := os.WriteFile(filepath.Join(dir, "01-good.go"), []byte(`package main + +import "github.com/samcharles93/archie-core/internal/plugin" + +var Plugin = plugin._Plugin{ + WName: func() string { return "good" }, + WVersion: func() string { return "1.0.0" }, +} +`), 0o644); err != nil { + t.Fatal(err) + } + + done := make(chan struct{}) + var plugins []plugin.Plugin + var loadErr error + go func() { + plugins, loadErr = plugin.LoadDir(dir, symbols) + close(done) + }() + + select { + case <-done: + if loadErr != nil { + t.Errorf("LoadDir returned error: %v", loadErr) + } + if len(plugins) != 1 { + t.Errorf("LoadDir returned %d plugins, want 1 (blocking plugin should be skipped, good plugin loaded)", len(plugins)) + } else if plugins[0].Name() != "good" { + t.Errorf("plugin.Name() = %q, want good", plugins[0].Name()) + } + case <-time.After(5 * time.Second): + t.Error("LoadDir did not return within 5s — blocking plugin hang prevents loading good plugins (issue #43)") + } +} diff --git a/internal/yaegiutil/yaegiutil_test.go b/internal/yaegiutil/yaegiutil_test.go index 3b8676b11..147ffd402 100644 --- a/internal/yaegiutil/yaegiutil_test.go +++ b/internal/yaegiutil/yaegiutil_test.go @@ -3,6 +3,7 @@ package yaegiutil_test import ( "strings" "testing" + "time" "github.com/traefik/yaegi/interp" @@ -71,3 +72,35 @@ func TestSafePassesThroughResult(t *testing.T) { t.Errorf("got %d, want 42", got) } } + +func TestResolveTimeoutOnBlockingEval(t *testing.T) { + // Resolve calls i.Eval(src) with no timeout. If src contains + // package-level code that blocks forever (e.g. init() { select{} }), + // Resolve hangs indefinitely. This test asserts the CORRECT behaviour + // (Resolve returns an error or the timeout fires) and FAILS today + // because Resolve hangs. + + i, err := yaegiutil.New(interp.Options{}) + if err != nil { + t.Fatal(err) + } + + done := make(chan struct{}) + go func() { + _, _ = yaegiutil.Resolve[func() string](i, `package main + +func init() { select {} } + +func Greet() string { return "hi" } +`, "main.Greet") + close(done) + }() + + select { + case <-done: + // Resolve returned — that is the desired behaviour after the fix. + // Today it hangs, so this branch is unreachable. + case <-time.After(5 * time.Second): + t.Error("Resolve did not return within 5s — Eval has no timeout (issue #43)") + } +} From 7c294f88f72a2aa021607c41415893ddf0e0fcd9 Mon Sep 17 00:00:00 2001 From: archie Date: Mon, 27 Jul 2026 03:04:28 +0000 Subject: [PATCH 2/2] fix: LoadDir: no timeout around Yaegi Eval can hang daemon startup (archie) Fixes #43 --- internal/yaegiutil/yaegiutil.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/internal/yaegiutil/yaegiutil.go b/internal/yaegiutil/yaegiutil.go index 5f43b3b41..2164c776f 100644 --- a/internal/yaegiutil/yaegiutil.go +++ b/internal/yaegiutil/yaegiutil.go @@ -12,6 +12,7 @@ package yaegiutil import ( "fmt" "reflect" + "time" "github.com/traefik/yaegi/interp" "github.com/traefik/yaegi/stdlib" @@ -35,15 +36,39 @@ func New(opts interp.Options, extraSymbols ...map[string]map[string]reflect.Valu return i, nil } +// evalTimeout bounds the Yaegi Eval(src) call. Yaegi's Eval is not +// cancellable mid-execution, so a plugin whose package-level init +// blocks forever would hang the daemon without this guard. On timeout +// the goroutine leaks (known limitation), but the daemon continues. +const evalTimeout = 3 * time.Second + // Resolve evaluates src in i and type-asserts exportPath's value // (e.g. "main.Stage", "gate.Check") to T. It does not itself recover // panics -- wrap the call that invokes the returned T in Safe, since a // panic can happen either during Eval (e.g. an init()) or during the // caller's later invocation of the resolved function. +// +// The Eval call is guarded by evalTimeout: if package-level init code +// blocks forever (e.g. select{} in init()), Resolve returns an error +// instead of hanging the caller indefinitely. The underlying Yaegi +// goroutine cannot be cancelled and will leak; this is an accepted +// trade-off for daemon availability. func Resolve[T any](i *interp.Interpreter, src, exportPath string) (result T, err error) { - if _, err := i.Eval(src); err != nil { - return result, fmt.Errorf("evaluate: %w", err) + done := make(chan error, 1) + go func() { + _, evalErr := i.Eval(src) + done <- evalErr + }() + + select { + case evalErr := <-done: + if evalErr != nil { + return result, fmt.Errorf("evaluate: %w", evalErr) + } + case <-time.After(evalTimeout): + return result, fmt.Errorf("evaluate: timed out after %v", evalTimeout) } + v, err := i.Eval(exportPath) if err != nil { return result, fmt.Errorf("does not export %s: %w", exportPath, err)