duckdb-node icon indicating copy to clipboard operation
duckdb-node copied to clipboard

after duck db close,the db file still locked

Open superkidsXin opened this issue 1 year ago • 6 comments

What happens?

I am use duckdb in node enviroment.but I found a very serious bug. after I close duck db by api,I stilll can't remove db file,because the db file is locked. "Error: EBUSY: resource busy or locked, unlink"

I write a demo project to reproduce the problem : https://github.com/ftyszyx/test_duckdb

can anyone help me?

To Reproduce

import duckdb from "duckdb";
import fs from "fs";

let db = null;
// Open a new db file with RW permissions so it would create the file
async function openDb() {
  if (db) {
    console.log("db already opened");
    return;
  }
  return new Promise((resolve, reject) => {
    db = new duckdb.Database("duck.db", { access_mode: "READ_WRITE" }, (err) => {
      if (err) {
        console.log("open db error", err);
        db = null;
        reject(err);
        return;
      } else {
        console.log("db opened successfully");
        resolve();
      }
    });
  });
}

async function closeDb() {
  if (db === null) {
    console.log("db already closed");
    return;
  }
  return new Promise((resolve, reject) => {
    db.close((err) => {
      if (err) {
        console.log("close db error", err);
        reject(err);
        return;
      } else {
        console.log("db closed successfully");
        resolve();
      }
    });
  });
}

async function runsql(text) {
  return new Promise((resolve, reject) => {
    db.run(text, (error) => {
      if (error) {
        console.log("run sql err", error);
        reject(error);
      } else {
        console.log("runsql ok", text);
        resolve();
      }
    });
  });
}

await openDb();
await runsql("select 42 as fortytwo");
await closeDb();
fs.unlinkSync("duck.db");
console.log("del ok");

output err

db opened successfully
runsql ok select 42 as fortytwo
db closed successfully
node:fs:1877
  binding.unlink(path);
          ^

Error: EBUSY: resource busy or locked, unlink 'D:\mywork\learn_all\node\test_duckdb\duck.db'
    at Object.unlinkSync (node:fs:1877:11)
    at <anonymous> (D:\mywork\learn_all\node\test_duckdb\index.ts:62:4)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  errno: -4082,
  code: 'EBUSY',
  syscall: 'unlink',
  path: 'D:\\mywork\\learn_all\\node\\test_duckdb\\duck.db'
}

Node.js v20.14.0

OS:

windows

DuckDB Version:

1.0

DuckDB Client:

node.js

Full Name:

yuxin.zhang

Affiliation:

personal

What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.

I have not tested with any build

Did you include all relevant data sets for reproducing the issue?

No - Other reason (please specify in the issue body)

Did you include all code required to reproduce the issue?

  • [X] Yes, I have

Did you include all relevant configuration (e.g., CPU architecture, Python version, Linux distribution) to reproduce the issue?

  • [X] Yes, I have

superkidsXin avatar Aug 09 '24 10:08 superkidsXin

Since this is Windows, my first thought is that this isn't something we have control over, Windows file system locks are much less responsive than Linux/Mac

But perhaps we're not properly closing the file handles and this is a real issue, so take this with a grain of salt

Tishj avatar Aug 09 '24 10:08 Tishj

thank you for your reply @Tishj I suspect the problem is the file handles not be released. because if I delete the query line, do't do a sql query,the file will be del success!

await openDb();
await runsql("select 42 as fortytwo");
await closeDb();
fs.unlinkSync("duck.db");

superkidsXin avatar Aug 09 '24 12:08 superkidsXin

To confirm that this is still an issue, I just tried the demo after updating to 1.1.0, and it still produce the same error.

Is there an ETA on when this will be fixed? I'm willing to help with testing.

franksmithinnovasea avatar Sep 18 '24 16:09 franksmithinnovasea

I am hitting this issue also. Would love some update on it.

Draiken avatar Nov 20 '24 13:11 Draiken

there is no offical fix,so i use sqlite to instead

superkidsXin avatar Nov 24 '24 02:11 superkidsXin

I ~~solved~~ worked around the issue in my project by running DB interaction in child process. Though it may be not viable solution for everyone.

s-oravec avatar May 20 '25 07:05 s-oravec