Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent bulk operations from being executed multiple times #2658

Merged
merged 1 commit into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1201,16 +1201,15 @@ export abstract class BulkOperationBase {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (this.s.executed) {
return handleEarlyError(new MongoError('Batch cannot be re-executed'), callback);
}

const writeConcern = WriteConcern.fromOptions(options);
if (writeConcern) {
this.s.writeConcern = writeConcern;
}

if (this.s.executed) {
const executedError = new MongoError('batch cannot be re-executed');
return handleEarlyError(executedError, callback);
}

// If we have current batch
if (this.isOrdered) {
if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch);
Expand All @@ -1225,6 +1224,7 @@ export abstract class BulkOperationBase {
return handleEarlyError(emptyBatchError, callback);
}

this.s.executed = true;
return executeLegacyOperation(this.s.topology, executeCommands, [this, options, callback]);
}

Expand Down
18 changes: 18 additions & 0 deletions test/functional/bulk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1759,4 +1759,22 @@ describe('Bulk', function () {
});
})
);

it(
'should throw an error if bulk execute is called more than once',
withClientV2(function (client, done) {
const bulk = client.db().collection('coll').initializeUnorderedBulkOp();
bulk.insert({});

bulk.execute((err, result) => {
expect(err).to.not.exist;
expect(result).to.exist;

bulk.execute(err => {
expect(err).to.match(/Batch cannot be re-executed/);
done();
});
});
})
);
});