fix: prevent pop race conditions (hopefully)

This commit is contained in:
insects 2025-03-31 23:45:09 +02:00
parent abbbf42715
commit 2836b0629c

View file

@ -84,7 +84,9 @@ class InstanceController < ApplicationController
def pop def pop
instance_id, nm, pwd = pop_instance_params instance_id, nm, pwd = pop_instance_params
parent_instance = Instance.find_by(public_id: instance_id) 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) pop = Pop.new(instance_id: parent_instance.id, name: nm)
if pop.save if pop.save
@instance = Instance.includes(:pops, :fairies).find_by(public_id: instance_id) @instance = Instance.includes(:pops, :fairies).find_by(public_id: instance_id)