Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLiteAsyncConnection does not release database file #740

Closed
gentledepp opened this issue Aug 8, 2018 · 5 comments
Closed

SQLiteAsyncConnection does not release database file #740

gentledepp opened this issue Aug 8, 2018 · 5 comments
Assignees
Labels
Milestone

Comments

@gentledepp
Copy link

gentledepp commented Aug 8, 2018

Hi!

I made a PR to support storing guids as blobs #739

When running the tests, I noticed, that the async tests fail - except of the very first one.
The exception that causes the UTs to fail is

SetUp : System.IO.IOException : The process cannot access the file '......async.db' because it is being used by another process

Note, that I am running these tests with
USE_SQLITEPCL_RAW

it seems that everything is disposed correctly in the tests.
Then I found [this issue on stackoverflow] (https://stackoverflow.com/questions/8511901/system-data-sqlite-close-not-releasing-database-file) which sounded pretty similar.
Explanation:

What happens when you call SQLiteConnection.Close() is that (along with a number of checks and other things) the SQLiteConnectionHandle that points to the SQLite database instance is disposed. This is done through a call to SQLiteConnectionHandle.Dispose(), however this doesn't actually release the pointer until the CLR's Garbage Collector performs some garbage collection. Since SQLiteConnectionHandle overrides the CriticalHandle.ReleaseHandle() function to call sqlite3_close_interop() (through another function) this does not close the database.

So I tried out their solution, which is adding a call to "GC.Collect" in order to be able to delete the file.


        [SetUp]
    	public void SetUp()
    	{
    		SQLite.SQLiteConnectionPool.Shared.Reset ();
        #if NETFX_CORE
        //...
        #else
    		_connectionString = Path.Combine (Path.GetTempPath (), DatabaseName);
    		_path = _connectionString;
    		GC.Collect ();
    		GC.WaitForPendingFinalizers ();
    		System.IO.File.Delete (_path);
        #endif
    	}

This actually did the trick, however I am not sure in how to proceed...
@ericsink and @praeclarum:

  1. is this a known issue of SQLite.PCL.raw?
  2. Is it intended? If so, how do I fix it
  3. I tried to upgrade to SQLitePCLRaw v1.1.11 but that did not change anything
@praeclarum
Copy link
Owner

Sorry for the trouble. I haven't thought too hard around the semantics of Resetting the pool, but what you're trying to do look valid to me and I'd like to support it.

Will have to dive into @ericsink's code to find out why the GC needs to run...

@praeclarum praeclarum added the Bug label Sep 10, 2018
@Weldryn
Copy link

Weldryn commented Sep 18, 2018

I happen to run into this issue too. After resetting the pool, setting the connection instance to null and even forcing the GC to run like in @gentledepp example, I can't create a file stream (open mode, read only and read/write access) to the database (for copy/move). Even after 5 attempts with 200ms delay in between. I am running the version 1.3.2 though.
Any news on that bug @praeclarum? Is that fixed in the newest version?

@praeclarum
Copy link
Owner

I believe the cause was the read connection opened by the async connection. I have changed the code to fix it (tests pass). I will do a release today and ask that you test against that.

Thanks!

@praeclarum praeclarum modified the milestones: v1.5, v1.6 Jan 23, 2019
@praeclarum
Copy link
Owner

This has been fixed in 1.6.

@praeclarum praeclarum self-assigned this Jan 24, 2019
@gentledepp
Copy link
Author

@bruzkovsky - we need to have a look at this!
@praeclarum - thank you so much for your awesome work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants