Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Reduce likelihood of race condition in Matching #1157

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions common/match/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ const logger = getLogger('Match');
export async function createMatch(pupil: Pupil, student: Student, pool: ConcreteMatchPool) {
const uuid = generateUUID();

if (pupil.openMatchRequestCount < 1) {
// Refetch match request count to reduce the likelihood of race conditions
// (does not prevent it though, we would actually need a SELECT FOR UPDATE)
const freshPupil = await prisma.pupil.findUniqueOrThrow({ where: { id: pupil.id }, select: { openMatchRequestCount: true } });
const freshStudent = await prisma.student.findUniqueOrThrow({ where: { id: student.id }, select: { openMatchRequestCount: true } });

if (freshPupil.openMatchRequestCount < 1) {
throw new PrerequisiteError(`Cannot create Match for Pupil without open match requests`);
}

if (student.openMatchRequestCount < 1) {
if (freshStudent.openMatchRequestCount < 1) {
throw new PrerequisiteError(`Cannot create Match for Student without open match request count`);
}

Expand Down
Loading