audiamus / BookLibConnect

A standalone Audible downloader and decrypter
GNU General Public License v3.0
672 stars 38 forks source link

UNIQUE constraint failed: ChapterInfos.Id #121

Closed rbielaws closed 9 months ago

rbielaws commented 9 months ago

I'm a new user and I've only downloaded a couple groups of 10-15 books each but each time I've gotten an error with the trace below on one or two of the books in the group.
Thus far I've hit ignore and nothing obviously terrible has happened although I have not taken a thorough look at the output of AAX Audio Converter to see if all the chapters were actually exported or the chapter data is correct.

Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 19: 'UNIQUE constraint failed: ChapterInfos.Id'. at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db) at Microsoft.Data.Sqlite.SqliteDataReader.NextResult() at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject) at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)

audiamus commented 9 months ago

Thank you for reporting this.

I have run a series of tests similar to yours and could reproduce the issue a few times. Looking into the logs I'd suspect a race condition. There could be more than a single DbContext open at the same time - which by itself would be harmless, but it may attach the same Conversion instance and that could cause problems. I have to investigate.

audiamus commented 9 months ago

Can you try this new executable? BookLibConnect-0.13.1.exe.zip

It appears that at the end of the decryption phase a very short-lived DbContext attached the same Conversion instance from a worker thread as the longer living DbContext in the main thread. Since SQLite and EntityFramework are set to automatic ID generation, we encountered a duplicate ID when saving the changes. Now, this short-lived DbContext is also run in the main thread, hereby channelling DB access into sequential order. My extension methods to the SynchronizationContext class came handy for this.

(Log level 4 will show all the method invocations affected, with the thread calling them.)

I did not find any more constraint violations with this fix, If it works for you, too, I'll publish it as version 0.13.1

rbielaws commented 9 months ago

It doesn’t appear to run at all. I even rebooted to insure nothing running could be conflicting.

audiamus commented 9 months ago

Sorry, I did not provide any instructions.

The exe also needs appsettings.json and e_sqlite3.dll from the original installation in C:\Program Files\audiamus\Book Lib Connect.

Or you put the new exe in C:\Program Files\audiamus\Book Lib Connect, temporarily replacing the current exe. But make sure to keep the original exe and restore it after the test because I erroneously already updated the new exe to version 0.13.1,

rbielaws commented 9 months ago

I pulled about 100 books down in groups of between 10 and 20 each time. No problems!

For whatever reason, I found it here rather than in Program Files "C:\Users\Rick\AppData\Local\Programs\audiamus\Book Lib Connect\BookLibConnect.exe"

Individual book sizes ranged from under 2 hours to over 50 hours.

audiamus commented 9 months ago

I pulled about 100 books down in groups of between 10 and 20 each time. No problems!

Thanks a lot!

audiamus commented 9 months ago

Version 0.13.1 released.