Prevent deadlock during command deletion in MySQL #61
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
NanoMDM allows for a single command to be queued for multiple devices. Since #48 landed, both queued commands and the command data itself is deleted after a device in finished with a command. However, I have observed deadlocks which occur when multiple devices trigger deletion for a shared command at once.
This can be avoided by placing a record lock on the command row before attempting a delete, so that only one device's enrollment queue can be cleared at a time.
This should have minimal impacts on performance as it will only serialize deletes to a single command. Furthermore, as the lock on the command record must be taken anyway, this does not add any additional locking to the transaction.
Testing
I have tested this, and have confirmed that it prevents deadlocks in NanoMDM. Furthermore, I have tested that commands are still delivered and responded to.