Prevent duplicative merge attempts. #2036
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If you attempt to "publish" the same draft multiple times and make your requests quickly enough, multiple attempts may pass this test and proceed through the merging code.
That is never desirable.
And I have not yet definitively proved, but am quite convinced that this occasionally completely corrupts the casebook involved.
This PR uses
select_for_update
to ensure that only one publication attempt is ever in progress at once.The user experience could still use some polishing: right now, on prod, if something goes wrong with the "publish" modal (for instance, you open two tabs with a draft, and click publish in one, wait a few seconds, and then click publish in the other), the modal just stays open. Your request gets a 400... but unless the dev tools are open, you don't see any notification.
I tried to fix that in this PR too, so that I could add an error message for THAT situation AND for the situation this PR fixes. But I couldn't figure it out...
But, I think this is worth it even without that: a weirdly not-closing modal is better than a potentially corrupted book!