From 303e80dee7b0bae1a3e03781dbee648baedf73ff Mon Sep 17 00:00:00 2001 From: James Shubin Date: Thu, 2 Oct 2025 23:28:51 -0400 Subject: [PATCH] engine, lang, lib, pgraph: Plumb through more ctx --- engine/resources/exec.go | 6 +++--- engine/resources/group.go | 2 +- engine/resources/resources_test.go | 2 +- lang/ast/structs.go | 2 +- lang/funcs/dage/dage.go | 4 ++-- lang/interpret_test.go | 10 +++++----- lib/main.go | 3 ++- pgraph/graphviz.go | 9 +++++---- pgraph/pgraph_test.go | 2 +- 9 files changed, 21 insertions(+), 19 deletions(-) diff --git a/engine/resources/exec.go b/engine/resources/exec.go index ab114cce..43cf59ee 100644 --- a/engine/resources/exec.go +++ b/engine/resources/exec.go @@ -482,7 +482,7 @@ func (obj *ExecRes) CheckApply(ctx context.Context, apply bool) (bool, error) { cmdName = obj.IfShell // usually bash, or sh cmdArgs = []string{"-c", obj.IfCmd} } - cmd := exec.Command(cmdName, cmdArgs...) + cmd := exec.CommandContext(ctx, cmdName, cmdArgs...) cmd.Dir = obj.IfCwd // run program in pwd if "" // ignore signals sent to parent process (we're in our own group) cmd.SysProcAttr = &syscall.SysProcAttr{ @@ -563,7 +563,7 @@ func (obj *ExecRes) CheckApply(ctx context.Context, apply bool) (bool, error) { cmdName = obj.NIfShell // usually bash, or sh cmdArgs = []string{"-c", obj.NIfCmd} } - cmd := exec.Command(cmdName, cmdArgs...) + cmd := exec.CommandContext(ctx, cmdName, cmdArgs...) cmd.Dir = obj.NIfCwd // run program in pwd if "" // ignore signals sent to parent process (we're in our own group) cmd.SysProcAttr = &syscall.SysProcAttr{ @@ -817,7 +817,7 @@ func (obj *ExecRes) CheckApply(ctx context.Context, apply bool) (bool, error) { cmdName = obj.DoneShell // usually bash, or sh cmdArgs = []string{"-c", obj.DoneCmd} } - cmd := exec.Command(cmdName, cmdArgs...) + cmd := exec.CommandContext(ctx, cmdName, cmdArgs...) cmd.Dir = obj.DoneCwd // run program in pwd if "" // ignore signals sent to parent process (we're in our own group) cmd.SysProcAttr = &syscall.SysProcAttr{ diff --git a/engine/resources/group.go b/engine/resources/group.go index 78e705af..cf1282c3 100644 --- a/engine/resources/group.go +++ b/engine/resources/group.go @@ -197,7 +197,7 @@ func (obj *GroupRes) CheckApply(ctx context.Context, apply bool) (bool, error) { cmdName = "groupdel" } - cmd := exec.Command(cmdName, args...) + cmd := exec.CommandContext(ctx, cmdName, args...) cmd.SysProcAttr = &syscall.SysProcAttr{ Setpgid: true, Pgid: 0, diff --git a/engine/resources/resources_test.go b/engine/resources/resources_test.go index 3b649dc1..85045933 100644 --- a/engine/resources/resources_test.go +++ b/engine/resources/resources_test.go @@ -661,7 +661,7 @@ func TestResources1(t *testing.T) { } t.Logf("test #%d: running CheckApply", index) - checkOK, err := res.CheckApply(doneCtx, true) // no noop! + checkOK, err := res.CheckApply(context.TODO(), true) // no noop! if err != nil { t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: CheckApply failed: %s", index, err.Error()) diff --git a/lang/ast/structs.go b/lang/ast/structs.go index 958c92f0..99c84738 100644 --- a/lang/ast/structs.go +++ b/lang/ast/structs.go @@ -5624,7 +5624,7 @@ func (obj *StmtProg) SetScope(scope *interfaces.Scope) error { // debugging visualizations if obj.data.Debug && orderingGraphSingleton { obj.data.Logf("running graphviz for ordering graph...") - if err := orderingGraph.ExecGraphviz("/tmp/graphviz-ordering.dot"); err != nil { + if err := orderingGraph.ExecGraphviz(context.TODO(), "/tmp/graphviz-ordering.dot"); err != nil { obj.data.Logf("graphviz: errored: %+v", err) } //if err := orderingGraphFiltered.ExecGraphviz("/tmp/graphviz-ordering-filtered.dot"); err != nil { diff --git a/lang/funcs/dage/dage.go b/lang/funcs/dage/dage.go index c2779ff5..141b3abb 100644 --- a/lang/funcs/dage/dage.go +++ b/lang/funcs/dage/dage.go @@ -961,7 +961,7 @@ func (obj *Engine) Graph() *pgraph.Graph { // ExecGraphviz writes out the diagram of a graph to be used for visualization // and debugging. You must not modify the graph (eg: during Lock) when calling // this method. -func (obj *Engine) ExecGraphviz(dir string) error { +func (obj *Engine) ExecGraphviz(ctx context.Context, dir string) error { // No mutex needed here since this func runs in a non-concurrent Txn. // No mutex is needed at this time because we only run this in txn's and @@ -1019,7 +1019,7 @@ func (obj *Engine) ExecGraphviz(dir string) error { }, } - if err := gv.Exec(); err != nil { + if err := gv.Exec(ctx); err != nil { return err } return nil diff --git a/lang/interpret_test.go b/lang/interpret_test.go index b15c68eb..c76ace33 100644 --- a/lang/interpret_test.go +++ b/lang/interpret_test.go @@ -567,7 +567,7 @@ func TestAstFunc1(t *testing.T) { } if runGraphviz { t.Logf("test #%d: Running graphviz...", index) - if err := fgraph.ExecGraphviz("/tmp/graphviz.dot"); err != nil { + if err := fgraph.ExecGraphviz(context.TODO(), "/tmp/graphviz.dot"); err != nil { t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: writing graph failed: %+v", index, err) return @@ -1120,7 +1120,7 @@ func TestAstFunc2(t *testing.T) { } ast.ScopeGraph(graph) - if err := graph.ExecGraphviz("/tmp/set-scope.dot"); err != nil { + if err := graph.ExecGraphviz(context.TODO(), "/tmp/set-scope.dot"); err != nil { t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: writing graph failed: %+v", index, err) return @@ -1233,7 +1233,7 @@ func TestAstFunc2(t *testing.T) { if runGraphviz { t.Logf("test #%d: Running graphviz...", index) - if err := fgraph.ExecGraphviz("/tmp/graphviz.dot"); err != nil { + if err := fgraph.ExecGraphviz(context.TODO(), "/tmp/graphviz.dot"); err != nil { t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: writing graph failed: %+v", index, err) return @@ -1996,7 +1996,7 @@ func TestAstFunc3(t *testing.T) { } ast.ScopeGraph(graph) - if err := graph.ExecGraphviz("/tmp/set-scope.dot"); err != nil { + if err := graph.ExecGraphviz(context.TODO(), "/tmp/set-scope.dot"); err != nil { t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: writing graph failed: %+v", index, err) return @@ -2088,7 +2088,7 @@ func TestAstFunc3(t *testing.T) { if runGraphviz { t.Logf("test #%d: Running graphviz...", index) - if err := fgraph.ExecGraphviz("/tmp/graphviz.dot"); err != nil { + if err := fgraph.ExecGraphviz(context.TODO(), "/tmp/graphviz.dot"); err != nil { t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: writing graph failed: %+v", index, err) return diff --git a/lib/main.go b/lib/main.go index f9cbe150..8a09e91d 100644 --- a/lib/main.go +++ b/lib/main.go @@ -1049,7 +1049,8 @@ func (obj *Main) Run() error { obj.ge.Graph(): nil, }, } - if err := gv.Exec(); err != nil { + // FIXME: is this the right ctx? + if err := gv.Exec(exitCtx); err != nil { Logf("graphviz: %+v", err) } else { Logf("graphviz: successfully generated graph!") diff --git a/pgraph/graphviz.go b/pgraph/graphviz.go index deb36d1a..f5fc07e3 100644 --- a/pgraph/graphviz.go +++ b/pgraph/graphviz.go @@ -30,6 +30,7 @@ package pgraph // TODO: this should be a subpackage import ( + "context" "fmt" "html" "os" @@ -153,7 +154,7 @@ func (obj *Graphviz) Text() string { // Exec writes out the graphviz data and runs the correct graphviz filter // command. -func (obj *Graphviz) Exec() error { +func (obj *Graphviz) Exec(ctx context.Context) error { filter := "" switch obj.Filter { case "": @@ -195,7 +196,7 @@ func (obj *Graphviz) Exec() error { } out := fmt.Sprintf("%s.png", filename) - cmd := exec.Command(path, "-Tpng", fmt.Sprintf("-o%s", out), filename) + cmd := exec.CommandContext(ctx, path, "-Tpng", fmt.Sprintf("-o%s", out), filename) if err1 == nil && err2 == nil { cmd.SysProcAttr = &syscall.SysProcAttr{} @@ -286,7 +287,7 @@ func (obj *Graph) Graphviz() string { // ExecGraphviz writes out the graphviz data and runs the correct graphviz // filter command. -func (obj *Graph) ExecGraphviz(filename string) error { +func (obj *Graph) ExecGraphviz(ctx context.Context, filename string) error { gv := &Graphviz{ Graphs: map[*Graph]*GraphvizOpts{ obj: nil, @@ -296,5 +297,5 @@ func (obj *Graph) ExecGraphviz(filename string) error { Filename: filename, //Hostname: hostname, } - return gv.Exec() + return gv.Exec(ctx) } diff --git a/pgraph/pgraph_test.go b/pgraph/pgraph_test.go index 349ae988..e52e1482 100644 --- a/pgraph/pgraph_test.go +++ b/pgraph/pgraph_test.go @@ -485,7 +485,7 @@ func TestTopoSort3(t *testing.T) { G.AddEdge(v5, v6, e5) G.AddEdge(v4, v2, e6) // cycle - G.ExecGraphviz("/tmp/g.dot") + //G.ExecGraphviz(context.TODO(), "/tmp/g.dot") _, err := G.TopologicalSort() if err == nil {