diff --git a/.changelog/19972.txt b/.changelog/19972.txt new file mode 100644 index 000000000..92c9a7fd2 --- /dev/null +++ b/.changelog/19972.txt @@ -0,0 +1,3 @@ +```release-note:bug +client: Fixed a bug where corrupt client state could panic the client +``` diff --git a/client/client_test.go b/client/client_test.go index 0fcb0c98a..d001dde32 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1876,6 +1876,14 @@ func TestClient_hasLocalState(t *testing.T) { c.stateDB = cstate.NewMemDB(c.logger) + t.Run("nil Job", func(t *testing.T) { + alloc := mock.BatchAlloc() + alloc.Job = nil + c.stateDB.PutAllocation(alloc) + + must.False(t, c.hasLocalState(alloc)) + }) + t.Run("plain alloc", func(t *testing.T) { alloc := mock.BatchAlloc() c.stateDB.PutAllocation(alloc) diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index b64961fcb..971356050 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -4770,6 +4770,9 @@ func (j *Job) Warnings() error { // LookupTaskGroup finds a task group by name func (j *Job) LookupTaskGroup(name string) *TaskGroup { + if j == nil { + return nil + } for _, tg := range j.TaskGroups { if tg.Name == name { return tg