Skip to content

Avoid iteration for stopping race condition#516

Merged
etiennebarrie merged 2 commits intomainfrom
avoid-iteration-for-stopping-race-condition
Nov 3, 2021
Merged

Avoid iteration for stopping race condition#516
etiennebarrie merged 2 commits intomainfrom
avoid-iteration-for-stopping-race-condition

Conversation

@etiennebarrie
Copy link
Member

Follow-up to #515

First, we can entirely avoid the "optimistic update" if the run is already stopping, like we were doing before in the non-race-condition case.
Also, since we know whether or not the record was updated, we know it was indeed stopping (but not its actual status), so we can prevent even a single iteration from happening (but we have to reload to get the actual status to either moved to paused or cancelled).

I tested the first point by re-using count_uncached_queries.
For the second point, I tested it by simulating a race condition for the model, but I also added a test at the job level for good measure. It also causes the race condition by running in a before_perform callback. This runs after Active Job deserializing the Run, but since it's prepended, before TaskJobConcern#before_perform, when we do the optimistic update.

@etiennebarrie etiennebarrie requested review from adrianna-chang-shopify and nvasilevski and removed request for nvasilevski November 2, 2021 16:12
assert_no_enqueued_jobs
end

test ".perform_now skips iterations when Run is cancelled even when a race condition happens" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test name is a little unclear, maybe:

Suggested change
test ".perform_now skips iterations when Run is cancelled even when a race condition happens" do
test ".perform_now avoids iterating and cancels Run when race occurs between starting and cancelling a Run" do

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I used that with symmetry with the previous test. Updated both.

Comment on lines +68 to +70
run = @run
CustomTaskJob.race_condition_hook = -> do
Run.find(run.id).update(status: :cancelling)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to assign run = @run? Can't we just use

CustomTaskJob.race_condition_hook = -> do
  Run.find(@run.id).update(status: :cancelling)
end

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I needed that before because I used before_perform directly.

@etiennebarrie etiennebarrie force-pushed the avoid-iteration-for-stopping-race-condition branch from e2309e7 to a71a3db Compare November 3, 2021 08:36
@etiennebarrie etiennebarrie merged commit 5178fcd into main Nov 3, 2021
@etiennebarrie etiennebarrie deleted the avoid-iteration-for-stopping-race-condition branch November 3, 2021 10:28
@shopify-shipit shopify-shipit bot temporarily deployed to rubygems November 5, 2021 21:07 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants