Fixed SQL Error When Following User


Well-known member
It's really rare (maybe like once every few days), but I see duplicate entry errors show up in our server log once in awhile when someone tries to follow someone.

Not really sure how to replicate it... I've tried double clicking follow really fast, etc. and I know there's code that checks if they are already following before it tries to follow them, but for whatever reason there's a case where that check fails when it shouldn't somehow.

We aren't using any custom database adapter, no master/slave setup, no custom datawriters or models that would affect it.

Maybe a good idea for it to be an INSERT IGNORE in this case?



XenForo developer
Staff member
It's generally a race condition.

As we're using the datawriter here, it's not trivial to flip to an insert ignore. This isn't something I'd want to do as part of 1.2 as it changes APIs a bit, but it may be an option to choose an insert/update ignore in the DW and detect it in the save (stopping post save/returning false). I think we might need to explicitly opt into it as it could make the insert fail even without any errors after preSave() (unless we then log it as a duplicate record error).


XenForo developer
Staff member
So when using entities (data writers in XF1), you don't really want to use INSERT IGNORE or similar because it doesn't necessarily make it clear how to handle things after the fact. Thus, that isn't something we're likely to do.

However, XF2 does now throw a XF\Db\DuplicateKeyException rather than an XF\Db\Exception for that particular error. This means it can be caught specifically. I've done this for both the follow and ignore cases.

Saying that, XF2 likely partially mitigated this issue before with things in the JS framework which would prevent two requests from normally happening simultaneously.