From 9c24e5bfc34f3a65516718b7dd611d63b10e6d3c Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Tue, 16 Sep 2025 12:27:07 +0200 Subject: [PATCH] fix: remove LXC working directory when it completes The working directory was not cleaned up upon completion of a LXC job because rc.stopJobContainer() -> rc.cleanUpJobContainer() -> rc.JobContainer.Remove() was never called for LXC containers. - stopContainer() and closeContainer() must not call rc.stopHostEnvironment(ctx) for LXC containers because - it will needlessly be called twice - it intercepts the call to - rc.stopJobContainer() - rc.JobContainer.Close() - rc.stopHostEnvironment(ctx) must be called in rc.cleanUpJobContainer which is indirectly called by rc.stopJobContainer() - since rc.JobContainer.Close() is a noop, not calling it for LXC containers had no consequence Resolves forgejo/runner#442 --- act/container/host_environment.go | 1 + act/runner/run_context.go | 16 +++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/act/container/host_environment.go b/act/container/host_environment.go index 2a8ee318..405862a9 100644 --- a/act/container/host_environment.go +++ b/act/container/host_environment.go @@ -407,6 +407,7 @@ func (e *HostEnvironment) Remove() common.Executor { if e.CleanUp != nil { e.CleanUp() } + common.Logger(ctx).Debugf("HostEnvironment.Remove %s", e.Path) return os.RemoveAll(e.Path) } } diff --git a/act/runner/run_context.go b/act/runner/run_context.go index 698fa0b4..bf74a92f 100644 --- a/act/runner/run_context.go +++ b/act/runner/run_context.go @@ -316,7 +316,15 @@ func (rc *RunContext) startHostEnvironment() common.Executor { StdOut: logWriter, LXC: rc.IsLXCHostEnv(ctx), } - rc.cleanUpJobContainer = rc.JobContainer.Remove() + rc.cleanUpJobContainer = func(ctx context.Context) error { + if err := rc.stopHostEnvironment(ctx); err != nil { + return err + } + if rc.JobContainer == nil { + return nil + } + return rc.JobContainer.Remove()(ctx) + } for k, v := range rc.JobContainer.GetRunnerContext(ctx) { if v, ok := v.(string); ok { rc.Env[fmt.Sprintf("RUNNER_%s", strings.ToUpper(k))] = v @@ -890,9 +898,6 @@ func (rc *RunContext) IsHostEnv(ctx context.Context) bool { func (rc *RunContext) stopContainer() common.Executor { return func(ctx context.Context) error { - if rc.IsLXCHostEnv(ctx) { - return rc.stopHostEnvironment(ctx) - } return rc.stopJobContainer()(ctx) } } @@ -900,9 +905,6 @@ func (rc *RunContext) stopContainer() common.Executor { func (rc *RunContext) closeContainer() common.Executor { return func(ctx context.Context) error { if rc.JobContainer != nil { - if rc.IsLXCHostEnv(ctx) { - return rc.stopHostEnvironment(ctx) - } return rc.JobContainer.Close()(ctx) } return nil