From 03d48436289688438697f93d3fdf48be4efc54a6 Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Mon, 18 Feb 2019 21:26:18 +0100 Subject: [PATCH] Fixed random test deadlocks temporarily no issue - we have seen random test failures recently - the cause: deadlocks - @NOTE: Deadlocks can and will happen naturally in innodb when multiple transactions are running and they operate on the same table. The challenge is just how to minimize, handle or avoid them. --- Why did the deadlock occur? The tests insert posts in parallel. As soon you insert two posts, we will attach the relations. The relations are basically: tags & authors. Both tables use foreign keys: post_id -> posts.id author_id -> users.id tag_id -> tags.id Attaching relations runs through two stages: - inserting or deleting the row (Bookshelf-Relations) - updating the row because of sort order (Ghost) 2 or more transactions can create a deadlock on the target relation table because of X and S locks for the foreign key, which get automatically set. Refs: https://bugs.mysql.com/bug.php?id=48652 https://www.chriscalender.com/advanced-innodb-deadlock-troubleshooting-what-show-innodb-status-doesnt-tell-you-and-what-diagnostics-you-should-be-looking-at/ Long-Term? - investigate further - retry deadlocks if we know it's fine? - drop foreign key and handle in Bookshelf? --- core/test/utils/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/test/utils/index.js b/core/test/utils/index.js index 119e82d27e..51ea2eaaed 100644 --- a/core/test/utils/index.js +++ b/core/test/utils/index.js @@ -69,7 +69,7 @@ fixtures = { return Promise.map(DataGenerator.forKnex.tags, function (tag) { return models.Tag.add(tag, module.exports.context.internal); }).then(function () { - return Promise.map(_.cloneDeep(DataGenerator.forKnex.posts), function (post) { + return Promise.each(_.cloneDeep(DataGenerator.forKnex.posts), function (post) { let postTagRelations = _.filter(DataGenerator.forKnex.posts_tags, {post_id: post.id}); let postAuthorsRelations = _.filter(DataGenerator.forKnex.posts_authors, {post_id: post.id});