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
  • Loading branch information
gabivlj authored Jan 27, 2025
1 parent 7106ac1 commit e107d84
Show file tree
Hide file tree
Showing 2 changed files with 31 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 @@ -59,6 +59,7 @@ func (h *dynamicUsersHook) Prestart(_ context.Context, request *interfaces.TaskP
return nil
}

response.State = make(map[string]string, 1)
// if this is the restart case, the UGID will already be acquired and we
// just need to read it back out of the hook's state
if request.PreviousState != nil {
Expand Down Expand Up @@ -86,7 +87,6 @@ func (h *dynamicUsersHook) Prestart(_ context.Context, request *interfaces.TaskP
request.Task.User = dynamic.String(ugid)

// set the user on the hook so we may release it later
response.State = make(map[string]string, 1)
response.State[dynamicUsersStateKey] = request.Task.User

return nil
Expand Down
30 changes: 30 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,36 @@ 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)

const capable = false
ctx := context.Background()
logger := testlog.HCLogger(t)

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)

// mark as usable
h.usable = true

must.NoError(t, h.Prestart(ctx, request, response))

// make sure the user exists in the state
// by the dynamic user key
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 e107d84

Please sign in to comment.