From 3ccb1c8c778449c4e0d101da945bb7bde3af3514 Mon Sep 17 00:00:00 2001 From: Philip Hayes Date: Mon, 27 Aug 2018 14:43:06 -0400 Subject: [PATCH 1/2] Fix to cope with race condition with multiple refs for one sync record --- lib/storage/dataset-clients.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/storage/dataset-clients.js b/lib/storage/dataset-clients.js index 2c5b0ae..4e5eefe 100644 --- a/lib/storage/dataset-clients.js +++ b/lib/storage/dataset-clients.js @@ -154,6 +154,8 @@ function upsertOrDeleteDatasetRecords(datasetId, datasetClientId, records, cb) { } else if (op === 'delete') { //remove the ref update['$pull'] = {'refs': datasetClientId}; + // also set the hash to null to handle race condition when multiple refs associated with single record + update['$set']['hash'] = null; } datasetRecordsCol.findOneAndUpdate({uid: record.uid}, update, {upsert: true, returnOriginal: false}, function(err, updated) { if (err) { From 33a7936d475b7c423d6a3c4e4894356f03054d71 Mon Sep 17 00:00:00 2001 From: Wojciech Trocki Date: Thu, 19 Dec 2019 11:42:19 +0000 Subject: [PATCH 2/2] fix: Do not delete data when there are still refs pointing to it --- lib/storage/dataset-clients.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/storage/dataset-clients.js b/lib/storage/dataset-clients.js index 4e5eefe..04e9159 100644 --- a/lib/storage/dataset-clients.js +++ b/lib/storage/dataset-clients.js @@ -154,8 +154,10 @@ function upsertOrDeleteDatasetRecords(datasetId, datasetClientId, records, cb) { } else if (op === 'delete') { //remove the ref update['$pull'] = {'refs': datasetClientId}; - // also set the hash to null to handle race condition when multiple refs associated with single record - update['$set']['hash'] = null; + if(datasetClientId && datasetClientId.length !==0){ + // Remove set data to null as there are other refs already linked + delete update['$set'].data; + } } datasetRecordsCol.findOneAndUpdate({uid: record.uid}, update, {upsert: true, returnOriginal: false}, function(err, updated) { if (err) {