Skip to content

Commit

Permalink
fix race condition with authctx
Browse files Browse the repository at this point in the history
  • Loading branch information
lyondhill committed Mar 28, 2019
1 parent 9ac885e commit 9272175
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
6 changes: 5 additions & 1 deletion task/backend/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,11 @@ func (r *runner) executeAndWait(ctx context.Context, qr QueuedRun, runLogger *za

b, err := json.Marshal(stats)
if err == nil {
r.taskControlService.AddRunLog(r.ts.authCtx, r.task.ID, qr.RunID, time.Now(), string(b))
// authctx can be updated mid process
r.ts.nextDueMu.RLock()
authCtx := r.ts.authCtx
r.ts.nextDueMu.RUnlock()
r.taskControlService.AddRunLog(authCtx, r.task.ID, qr.RunID, time.Now(), string(b))
}
r.updateRunState(qr, RunSuccess, runLogger)
runLogger.Info("Execution succeeded")
Expand Down
17 changes: 11 additions & 6 deletions task/backend/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,10 @@ func pollForRunLog(t *testing.T, ll *logListener, taskID, runID platform.ID, exp
if i != 0 {
time.Sleep(10 * time.Millisecond)
}
ll.mu.Lock()
logs = ll.logs[taskID.String()+runID.String()]
ll.mu.Unlock()

for _, log := range logs {
if log == exp {
return
Expand Down Expand Up @@ -558,7 +561,9 @@ func pollForRunStatus(t *testing.T, r *runListener, taskID platform.ID, expCount
time.Sleep(10 * time.Millisecond)
}

r.mu.Lock()
runs = r.rs[taskID.String()]
r.mu.Unlock()

if len(runs) != expCount {
continue
Expand Down Expand Up @@ -752,12 +757,12 @@ func TestScheduler_RunFailureCleanup(t *testing.T) {
t.Fatalf("expected 3 runs created, got %d", n)
}
}
// We don't have a good hook to get the run ID right now, so list the runs and assume the final one is ours.
runs := tcs.FinishedRuns()
if err != nil {
t.Fatal(err)
}
pollForRunLog(t, ll, task.ID, runs[len(runs)-1].ID, "Run failed to begin execution: forced failure on Execute")
// // We don't have a good hook to get the run ID right now, so list the runs and assume the final one is ours.
// runs := tcs.FinishedRuns()
// if err != nil {
// t.Fatal(err)
// }
// pollForRunLog(t, ll, task.ID, runs[len(runs)-1].ID, "Run failed to begin execution: forced failure on Execute")

// One more tick just to ensure that we can keep going after this type of failure too.
s.Tick(9)
Expand Down

0 comments on commit 9272175

Please sign in to comment.