Skip to content

Panic during build now prints stack trace

Arran Walker requested to merge ajwalker/panic-stack into main

What does this MR do?

Previously panics during a build would only print the panic and not also the stack trace. This now adds a stack trace to the Runner log (not job log).

Why was this MR needed?

Just printing the panic (panic: runtime error: invalid memory address or nil pointer dereference) isn't useful without the stack trace. Now you also get:

ERROR: goroutine 23 [running]:
runtime/debug.Stack()
        /Users/arranwalker/.asdf/installs/golang/1.20.5/go/src/runtime/debug/stack.go:24 +0x64
gitlab.com/gitlab-org/gitlab-runner/common.(*Build).Run.func2()
        /Users/arranwalker/repositories/work/gitlab-gitlab-runner/common/build.go:879 +0x1c4
panic({0x1040e7440, 0x10555c1a0})
        /Users/arranwalker/.asdf/installs/golang/1.20.5/go/src/runtime/panic.go:890 +0x258
gitlab.com/gitlab-org/gitlab-runner/common.(*Build).Run(0x14000c36000, 0x14000462f00, {0x104485600?, 0x14000c0e300})
        /Users/arranwalker/repositories/work/gitlab-gitlab-runner/common/build.go:889 +0x158
gitlab.com/gitlab-org/gitlab-runner/commands.(*RunCommand).processBuildOnRunner(0x14000466d00, 0x14000889680?, 0x14000a25270?, {0x10447f320?, 0x140000e57c0?}, {0x104192220, 0x1400047c240})
        /Users/arranwalker/repositories/work/gitlab-gitlab-runner/commands/multi.go:806 +0x460
gitlab.com/gitlab-org/gitlab-runner/commands.(*RunCommand).processRunner(0x14000466d00, 0x1400064d998?, 0x14000889680, 0x0?)
        /Users/arranwalker/repositories/work/gitlab-gitlab-runner/commands/multi.go:754 +0x608
gitlab.com/gitlab-org/gitlab-runner/commands.(*RunCommand).processRunners(0x14000466d00, 0x1?, 0x14000511620, 0x140004b82a0)
        /Users/arranwalker/repositories/work/gitlab-gitlab-runner/commands/multi.go:681 +0x1c0
created by gitlab.com/gitlab-org/gitlab-runner/commands.(*RunCommand).startWorkers
        /Users/arranwalker/repositories/work/gitlab-gitlab-runner/commands/multi.go:664 +0x30
  error=panic: runtime error: invalid memory address or nil pointer dereference job=4840582769 project=37053689 runner=3_e8T5Wc

What's the best way to test this MR?

We tend not to have obvious places that cause panics, but I manually tested this by modifying build.go and then running a job:

diff --git a/common/build.go b/common/build.go
index 9de186a93..427cffc8f 100644
--- a/common/build.go
+++ b/common/build.go
@@ -887,6 +887,9 @@ func (b *Build) Run(globalConfig *Config, trace JobTrace) (err error) {
                return err
        }
 
+       var a *int
+       *a++
+
        b.expandContainerOptions()
 
        ctx, cancel := context.WithTimeout(context.Background(), b.GetBuildTimeout())

What are the relevant issue numbers?

Edited by Arran Walker

Merge request reports