Fixed DuplicateKeyException: Duplicate entry 'thread-123-456' for key 'draft_key_user'

Steffen

Well-known member
Affected version
2.0.9
This issue has so far happend only once since we migrated to XenForo about 3 months ago (so it's not a high-priority issue). But maybe it can be easily fixed by saving drafts using a REPLACE query? This would avoid the race condition that currently seems to exist if (for whatever reason) there are two concurrent "save draft" requests.

Code:
XF\Db\DuplicateKeyException: MySQL query error [1062]: Duplicate entry 'thread-123-456' for key 'draft_key_user' src/XF/Db/AbstractStatement.php:212
Generated by: SomeUsername Aug 13, 2018 at 8:19 AM
Stack trace
INSERT  INTO `xf_draft` (`user_id`, `draft_key`, `message`, `extra_data`, `draft_id`, `last_update`) VALUES (?, ?, ?, ?, ?, ?)
------------

#0 src/XF/Db/Mysqli/Statement.php(196): XF\Db\AbstractStatement->getException('MySQL query err...', 1062, '23000')
#1 src/XF/Db/Mysqli/Statement.php(77): XF\Db\Mysqli\Statement->getException('MySQL query err...', 1062, '23000')
#2 src/XF/Db/AbstractAdapter.php(79): XF\Db\Mysqli\Statement->execute()
#3 src/XF/Db/AbstractAdapter.php(160): XF\Db\AbstractAdapter->query('INSERT  INTO `x...', Array)
#4 src/XF/Mvc/Entity/Entity.php(1389): XF\Db\AbstractAdapter->insert('xf_draft', Array, false)
#5 src/XF/Mvc/Entity/Entity.php(1121): XF\Mvc\Entity\Entity->_saveToSource()
#6 src/XF/Draft.php(42): XF\Mvc\Entity\Entity->save()
#7 src/XF/ControllerPlugin/Draft.php(29): XF\Draft->save()
#8 src/XF/Pub/Controller/Thread.php(306): XF\ControllerPlugin\Draft->updateMessageDraft(Object(XF\Draft), Array)
#9 src/XF/Mvc/Dispatcher.php(249): XF\Pub\Controller\Thread->actionDraft(Object(XF\Mvc\ParameterBag))
#10 src/XF/Mvc/Dispatcher.php(88): XF\Mvc\Dispatcher->dispatchClass('XF:Thread', 'Draft', 'json', Object(XF\Mvc\ParameterBag), 'forums', Object(KL\AutoMergeDoublePost\Pub\Controller\Thread), NULL)
#11 src/XF/Mvc/Dispatcher.php(41): XF\Mvc\Dispatcher->dispatchLoop(Object(XF\Mvc\RouteMatch))
#12 src/XF/App.php(1945): XF\Mvc\Dispatcher->run()
#13 src/XF.php(328): XF\App->run()
#14 index.php(13): XF::runApp('XF\\Pub\\App')
#15 {main}
 
Top Bottom