Skip to content

Commit

Permalink
docs(common-issues): add file
Browse files Browse the repository at this point in the history
  • Loading branch information
hasezoey committed Mar 18, 2023
1 parent f42c452 commit f57ac28
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
43 changes: 43 additions & 0 deletions docs/guides/common-issues.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
id: common-issues
title: 'Common Issues'
---

There are some common issues you may encounter with mongodb-memory-server (or also manually handling mongod instances), this guide will try to explain why they happen how to fix those issues.

## MongoWriteConcernError: operation was interrupted

The Error `MongoWriteConcernError: operation was interrupted` happens when a operation which is not retryable fails, which includes for example `db.dropDatabase`.
This Error happens because mongodb firstly starts all instances, says they are okay and has a primary (which are all events mongodb-memory-server listens for before resolving `.start`), but the shortly after the `.start` is resolved, the instace that is primary decides to step-down due to whatever reason.

The fix is to manually re-try those operations, like:

```js
// original:
async function setup(db) {
await db.dropDatabase();
}

// fix
async function setup(db) {
let retries = 5;
while (retries > 0) {
retries -= 1;
try {
await _setup(db);
} catch (err) {
if (err instanceof mongodb.MongoWriteConcernError && /operation was interrupted/.test(err.message)) {
continue;
}

throw err;
}
}
}

async function _setup(db) {
await db.dropDatabase();
}
```

See [Operations which are retry-able](https://www.mongodb.com/docs/manual/core/retryable-writes/#retryable-write-operations).
1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = {
'guides/known-issues',
'guides/error-warning-details',
'guides/mongodb-server-versions',
'guides/common-issues',
],
Migration: ['guides/migration/migrate8', 'guides/migration/migrate7'],
},
Expand Down

0 comments on commit f57ac28

Please sign in to comment.