tndrle / node-sqlite3-wasm

WebAssembly port of SQLite3 for Node.js with file system access
MIT License
63 stars 9 forks source link

Issue with Electron and Windows 11 #53

Open dziudek opened 10 months ago

dziudek commented 10 months ago

Hi,

First of all thank you for this library - we had serious issues with better-sqlite3 on older linux machines and we have decided to use this one in Publii.

Unfortunately we have discovered one important issue on Windows 11 - it seems that your library doesn't properly close the database connection. In result our app is unable to remove website folder while restoring website from backup - the db.sqlite file cannot be removed (even by rimraf). The EPERM error is returned. File is removed after closing the app so definitely it is connected with some unclosed handles to the db.sqlite file.

The problem occurs only on Windows - we didn't observed it on mac OS or Linux. We have even tried to test it outside protected directories (on desktop) and with EV-signed app version, but the problem still occurs, so most probably it is not caused by Windows itself. Also app based on better-sqlite3 properly closes the DB connection and there is no issue with removing db.sqlite file.

I have even tried to create a custom build and use close() instead of close_v2() as I have seen that better-sqlite3 uses close(), but without success, so there must be some additional operations to do during closing the connection with DB.

tndrle commented 10 months ago

Hi, thanks for reporting this issue. Except for general advice, I cannot help much without a code example that reproduces the error, I'm afraid.

One thing you have to make sure though is that you need to finalize all prepared statements. Calling db.close() does not finalize pending prepared statements. To my understanding, neither sqlite3_close() nor sqlite3_close_v2() finalize pending statements (see Closing A Database Connection), so it makes no difference here. Notice that better-sqlite3 is garbage-collected and probably finalizes statements automatically, but node-sqlite3-wasm is not (at least not fully)!

In the library tests, I noticed that Windows (in contrast to macOS and Linux) is particularly picky about unfinalized statements. I don't know why. There is no Windows-specific code in node-sqlite3-wasm (except for the maximum path length).

Maybe that helps. If not, we really have to consider a code example, I guess.

dziudek commented 10 months ago

Hi,

Thank you for more details - in fact we are using mainly get, run and all methods, which should be as documentation states automatically finalized. We are not using prepare at all in this case.

We are also using exec but only for DELETE operations, because for some reason delete queries are not working in our app on any platform (macOS, linux, Windows). But during my tests I was not running exec. To be sure - I have changed exec call to no-op operation in our DB abstraction layer - unfortunately without success :/

Maybe you will have some more ideas. I will also try to prepare some simple example, but I afraid that there is some more complex case here as with the delete statements, which are perfectly working in simple case, but not working in my case inside the app

dziudek commented 10 months ago

Ok, finally I have found the issue :)

The problem is with the isOpen property - I have used this to check if I can safely close DB connection, because without it, when there is no connection, scripts throws an error.

When I have replaced my isOpen check with:

try {
   db.close();
} catch (e) {
   console.log('DB connection already closed');
} 

All is working fine :)

So I suppose that you should verify if the isOpen property works in all case. Unfortunately I cannot easily reproduce this issue on simple script - then isOpen seems to be working on Windows. But it is definitely a case, because after replacing all isOpen with try .. catch my problems are gone

tndrle commented 10 months ago

Hi, thank you for all the details. To understand possible issues in the library better, I have two follow-up questions:

  1. How did you use isOpen? Like if (db.isOpen) { db.close() }?
  2. What problems do you have with DELETE statements? Did you try the run method, like db.run("DELETE ...")?

Thank you!

dziudek commented 10 months ago

Hi,

  1. yes, exactly - in simple node.js script it was working, but in my app not

  2. yes - I have used db.run for DELETE statements and I always got 0 in changed rows amount. Similiar code on simple node.js script was working correctly. Changing db.run to db.exec solved the issue.

Maybe it is related somehow to used node.js version - electron uses v18, while my tests was on v16.

I am quite busy this week, but if I will find time, I will also try to reproduce this issues in simpler electron app to exclude other side effects