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.
PULL REQUEST
Overview
This PR fixes a problem caused by a race condition.
The race condition happens when we try to fetch a skylink from the database, we don't find it, so we decide to insert it. When inserting it, we use
upsert
in order to avoid overwriting the ID if it just so happens that another server creates that exact skylink in that very moment. The issue is that in that case the upsert operation does not do anything and it does not return anUpsertID
. This leaves the skylink record returned by the function with a valid hash but an empty ID. This empty ID later on causes this skylink to be evaluated as invalid.This PR makes two changes:
we specifically check whether theUpsertID
we get is empty and if that's the case we fetch the skylink record from the databasefind and if it's not there upsert
flow with a single call tofindOneAndUpdate
that will only perform an update if if doesn't find the record in the DB (thanks to$setOnInsert
), thus giving us an atomic way to either find or insert the record.Example for Visual Changes
Checklist
Review and complete the checklist to ensure that the PR is complete before assigned to an approver.
Issues Closed