Node: Stores created by `pouchdb-adapter-leveldb` are not closed during `db.close()`
- Dominant language
- JavaScript
- Stars
- 17.6k
- Forks
- 1.5k
- PR merge metrics
- No merged PRs in 30d
Description
### Issue
Some context: https://github.com/pouchdb/pouchdb/issues/8574
When using Pouch with Node, `pouchdb-mapreduce`, and the `pouchdb-adapter-leveldb` adapter, the database files in the local `*-mrview-*` directories are not closed when the DB is closed. This means local DB files cannot be deleted even if we run `await db.close()` first.
This seems to happen only on Windows (maybe because of some OS-specific Node implementation detail).
### Info
- Environment: Node.js
- Platform: Windows 11
- Adapter: leveldb
- Server: none
### Reproduce
Minimal example:
```js
const fs = require("fs");
const PouchDB = require("pouchdb-core");
const PouchDBAdapterLevelDB = require("pouchdb-adapter-leveldb");
const PouchDBMapReduce = require("pouchdb-mapreduce");
PouchDB.plugin(PouchDBAdapterLevelDB).plugin(PouchDBMapReduce);
const db = new PouchDB("dev/pouch-test", { adapter: "leveldb", auto_compaction: true });
const index = {
_id: "_design/test",
views: {
test: {
map: function (obj) {
emit(obj._id);
}.toString(),
reduce: "_count",
},
},
};
db.put(index, () => {
db.put({ _id: "123" }, () => {
db.query("test", { limit: 0, reduce: false, stale: "update_after" }, () => {
db.close(() => {
fs.readdirSync("dev").filter((x) => x.startsWith("pouch-test"))
.forEach((dir) => fs.rmdirSync("dev/" +dir, { recursive: true }));
});
});
});
});
```
```bash
$ node index.js
(node:16984) [DEP0147] DeprecationWarning: In future versions of Node.js, fs.rmdir(path, { recursive: true }) will be removed. Use fs.rm(path, { recursive: true }) instead
(Use `node --trace-deprecation ...` to show where the warning was created)
node:fs:1780
handleErrorFromBinding(ctx);
^
Error: EBUSY: resource busy or locked, unlink '\\?\C:\Users\QasimAli\clio\launcher\dev\pouch-test-mrview-4a0c749d9aec5bd3a8306df822604e0f\000003.log'
at unlinkSync (node:fs:1780:3)
at _unlinkSync (node:internal/fs/rimraf:214:14)
at rimrafSync (node:internal/fs/rimraf:195:7)
at node:internal/fs/rimraf:253:9
at Array.forEach ()
at _rmdirSync (node:internal/fs/rimraf:250:7)
at rimrafSync (node:internal/fs/rimraf:193:7)
at Object.rmdirSync (node:fs:1218:14)
at C:\Users\QasimAli\clio\launcher\index.js:25:32
at Array.forEach () {
errno: -4082,
syscall: 'unlink',
code: 'EBUSY',
path: '\\\\?\\C:\\Users\\QasimAli\\clio\\launcher\\dev\\pouch-test-mrview-4a0c749d9aec5bd3a8306df822604e0f\\000003.log'
}
Node.js v18.12.1
```
Contributor guide
Assessment
This issue has not been assessed yet.