Skip to content

Commit

Permalink
taskrunner: fix panic when a task that has a dynamic user is recovered
Browse files Browse the repository at this point in the history
Before this, the nomad client would crash with:
```
panic: assignment to entry in nil map
```
  • Loading branch information
gabivlj committed Dec 20, 2024
1 parent c3ac9c1 commit 7f9f391
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion client/allocrunner/taskrunner/dynamic_users_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (h *dynamicUsersHook) Prestart(_ context.Context, request *interfaces.TaskP
if request.PreviousState != nil {
ugid, exists := request.PreviousState[dynamicUsersStateKey]
if exists {
response.State[dynamicUsersStateKey] = ugid
response.State = map[string]string{dynamicUsersStateKey: ugid}
return nil
}
}
Expand Down
28 changes: 28 additions & 0 deletions client/allocrunner/taskrunner/dynamic_users_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,34 @@ func TestTaskRunner_DynamicUsersHook_Prestart_unusable(t *testing.T) {
must.NoError(t, h.Prestart(ctx, request, response))
}

func TestTaskRunner_DynamicUsersHook_Prestart_State(t *testing.T) {
ci.Parallel(t)

// task driver does not indicate DynamicWorkloadUsers capability
const capable = false
ctx := context.Background()
logger := testlog.HCLogger(t)

// if the driver does not indicate the DynamicWorkloadUsers capability,
// none of the pool, request, or response are touched - so using nil
// for each of them shows we are exiting the hook immediatly
var pool dynamic.Pool = nil
request := &interfaces.TaskPrestartRequest{
Task: &structs.Task{},
PreviousState: map[string]string{
dynamicUsersStateKey: "1",
},
}

response := &interfaces.TaskPrestartResponse{}
h := newDynamicUsersHook(ctx, capable, logger, pool)
h.usable = true
must.NoError(t, h.Prestart(ctx, request, response))
user, ok := response.State[dynamicUsersStateKey]
must.True(t, ok)
must.Eq(t, "1", user)
}

func TestTaskRunner_DynamicUsersHook_Prestart_unnecessary(t *testing.T) {
ci.Parallel(t)

Expand Down

0 comments on commit 7f9f391

Please sign in to comment.