Skip to content

Conversation

@PaulMest
Copy link
Contributor

This is a self-contained repro for a problem discovered in PouchDB 7.3.0 where 2 databases using the memory adapter could impact each other and ultimately become non-responsive.

This was originally discovered in the RxDB test suite. Discussion here: pubkey/rxdb#3807.

  134 passing (2s)
  3 pending
  1 failing

  1) test.memory-adapter.js Race condition initially discovered with PouchDB in-memory-adapter 7.3.0:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
      at listOnTimeout (node:internal/timers:559:17)
      at processTimers (node:internal/timers:502:7)

This is a simple repro for a problem discovered in PouchDB 7.3.0. This was originally discovered in RxDB. Discussion here: pubkey/rxdb#3807.

```
  134 passing (2s)
  3 pending
  1 failing

  1) test.memory-adapter.js Race condition initially discovered with PouchDB in-memory-adapter 7.3.0:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
      at listOnTimeout (node:internal/timers:559:17)
      at processTimers (node:internal/timers:502:7)
```
@PaulMest PaulMest force-pushed the repro/race-condition-with-memory-adapter branch from dfd0e49 to d04021f Compare December 7, 2022 18:19
@PaulMest
Copy link
Contributor Author

PaulMest commented Dec 7, 2022

I've updated my PR:

  1. Rebased this branch on top of the latest from master.
  2. Addressed ESLint warning
  3. Pulled in fix from Fix the race condition when closing one of multiple databases. #8515 to pair with my repro from 6 months ago.

Hope this gets included into a future release! 🤞🏼

@PaulMest PaulMest changed the title repro: Race condition for pouchdb-memory-adapter hanging indefinitely fix: Race condition for pouchdb hanging indefinitely when multiple instances are used Dec 7, 2022
Copy link
Contributor

@garethbowen garethbowen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the test, that's really helpful. I've added a suggestion.

var adapterName = functionName(leveldown);
var adapterStore = dbStores.get(adapterName);
var keys = [...adapterStore.keys()].filter(k => k.includes("-mrview-"));
var keys = [...adapterStore.keys()].filter(k => k.includes(name)).filter(k => k.includes("-mrview-"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a small issue here if you have two dbs with the same prefix, for example, "test-db" and "test-db-2". If you close "test-db" the same race condition will occur. I think we can make the matching string more restrictive to avoid these edge cases, eg:

Suggested change
var keys = [...adapterStore.keys()].filter(k => k.includes(name)).filter(k => k.includes("-mrview-"));
var viewNamePrefix = PouchDB.prefix + name + "-mrview-";
var keys = [...adapterStore.keys()].filter(k => k.includes(viewNamePrefix));

You'll have to import PouchDB too, and I haven't actually run this code, but I think it'll work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking this over! Ran this locally and it seemed to work. Updated the tests and used the code snippet you provided. Just pushed the commit. CI should complete in about 10 minutes to verify.

@PaulMest PaulMest requested a review from garethbowen December 7, 2022 23:42
Copy link
Contributor

@garethbowen garethbowen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@garethbowen garethbowen merged commit 24157fc into apache:master Dec 8, 2022
@PaulMest PaulMest deleted the repro/race-condition-with-memory-adapter branch December 8, 2022 03:00
maximerety added a commit to TankerHQ/pouchdb that referenced this pull request Dec 13, 2022
…ly when multiple instances are used"

This reverts commit 24157fc.
CaptainAchab pushed a commit to TankerHQ/sdk-js that referenced this pull request Jan 3, 2023
Release notes: https://github.com/pouchdb/pouchdb/releases/tag/8.0.0

It includes a fix for our issue with multiple PouchDB instances used
concurrently, see: apache/pouchdb#8513.

Also note that we are waiting for a clarification about the way
dependent databases are handled in PouchDB. This could lead to new
changes, see: apache/pouchdb#8574.

We still allow compat with versions >= 7.2.2 < 7.3.0 for ease of use
alongside previous Tanker SDK versions that required these versions.
We ban the 7.3.x versions though since they are affected by the
aforementioned bug.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants