From 2836b0629c48f6ad26c6b5ab63282f09dc54489e Mon Sep 17 00:00:00 2001 From: insects Date: Mon, 31 Mar 2025 23:45:09 +0200 Subject: [PATCH] fix: prevent pop race conditions (hopefully) --- app/controllers/instance_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/instance_controller.rb b/app/controllers/instance_controller.rb index 42effa1..ef9e3f8 100644 --- a/app/controllers/instance_controller.rb +++ b/app/controllers/instance_controller.rb @@ -84,7 +84,9 @@ class InstanceController < ApplicationController def pop instance_id, nm, pwd = pop_instance_params parent_instance = Instance.find_by(public_id: instance_id) - if parent_instance.password == pwd + # Check if the same NM is already popped. Helps prevent potential desync race conditions. + potential_pop = Pop.find_by(instance_id: parent_instance.id, name: nm, created_at: 120.minutes.ago..Time.now) + if parent_instance.password == pwd and potential_pop.nil? pop = Pop.new(instance_id: parent_instance.id, name: nm) if pop.save @instance = Instance.includes(:pops, :fairies).find_by(public_id: instance_id)