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

DB Error: database is locked #16

Closed
cbenhagen opened this issue Sep 11, 2018 · 7 comments
Closed

DB Error: database is locked #16

cbenhagen opened this issue Sep 11, 2018 · 7 comments
Labels
bug Something isn't working

Comments

@cbenhagen
Copy link

When one download finishes while others are being created it can happen that the DB is locked.

@hnvn
Copy link
Member

hnvn commented Sep 11, 2018

I think this case only happens on the Android side because I already queued all write to DB actions on iOS. My main idea is: lock for write and allow read always. By the way, I forgot running a transaction when I insert a new task into DB. That would account for this bug.

@hnvn hnvn added the bug Something isn't working label Sep 11, 2018
@cbenhagen
Copy link
Author

Sorry I forgot to mention that I only tried this on iOS so far. This is where the bug occurs.

@hnvn
Copy link
Member

hnvn commented Sep 11, 2018

Is there any log for this case?

@cbenhagen
Copy link
Author

methodCallHandler: enqueue
execute query: INSERT INTO task (task_id,url,status,progress,file_name,saved_dir,headers,resumable,show_notification,open_file_from_notification,time_created) VALUES ("com.example.test.download.background.1536665611.774887.14","https://example.com/A001C015_180901_A4R4-thumb.jpg?mtime=1536384218.54",1,0,"A001C015_180901_A4R4-thumb.jpg","/Users/ben/Library/Developer/CoreSimulator/Devices/2370C450-CC60-41A5-912A-E0E583C20889/data/Containers/Data/Application/8800C18D-EAFB-4670-B856-A5E9EF543B36/Documents","{authorization: Basic aabbcc=}",0,0,0,1536665630531)
open DB successfully
Query was executed successfully. Affected rows = 1
methodCallHandler: enqueue
execute query: INSERT INTO task (task_id,url,status,progress,file_name,saved_dir,headers,resumable,show_notification,open_file_from_notification,time_created) VALUES ("com.example.test.download.background.1536665611.774887.15","https://example.com/A001C016_180901_A4R4-thumb.jpg?mtime=1536384218.57",1,0,"A001C016_180901_A4R4-thumb.jpg","/Users/ben/Library/Developer/CoreSimulator/Devices/2370C450-CC60-41A5-912A-E0E583C20889/data/Containers/Data/Application/8800C18D-EAFB-4670-B856-A5E9EF543B36/Documents","{authorization: Basic aabbcc=}",0,0,0,1536665630542)
open DB successfully
Query was executed successfully. Affected rows = 1
methodCallHandler: enqueue
execute query: INSERT INTO task (task_id,url,status,progress,file_name,saved_dir,headers,resumable,show_notification,open_file_from_notification,time_created) VALUES ("com.example.test.download.background.1536665611.774887.16","https://example.com/A001C017_180901_A4R4-thumb.jpg?mtime=1536384218.59",1,0,"A001C017_180901_A4R4-thumb.jpg","/Users/ben/Library/Developer/CoreSimulator/Devices/2370C450-CC60-41A5-912A-E0E583C20889/data/Containers/Data/Application/8800C18D-EAFB-4670-B856-A5E9EF543B36/Documents","{authorization: Basic aabbcc=}",0,0,0,1536665630550)
open DB successfully
Query was executed successfully. Affected rows = 1
methodCallHandler: enqueue
execute query: INSERT INTO task (task_id,url,status,progress,file_name,saved_dir,headers,resumable,show_notification,open_file_from_notification,time_created) VALUES ("com.example.test.download.background.1536665611.774887.17","https://example.com/A001C018_180901_A4R4-thumb.jpg?mtime=1536384218.61",1,0,"A001C018_180901_A4R4-thumb.jpg","/Users/ben/Library/Developer/CoreSimulator/Devices/2370C450-CC60-41A5-912A-E0E583C20889/data/Containers/Data/Application/8800C18D-EAFB-4670-B856-A5E9EF543B36/Documents","{authorization: Basic aabbcc=}",0,0,0,1536665630560)
execute query: SELECT * FROM task WHERE task_id = "com.example.test.download.background.1536665611.774887.1" ORDER BY id DESC LIMIT 1
open DB successfully
open DB successfully
Load task successfully
Query was executed successfully. Affected rows = 1
methodCallHandler: enqueue
execute query: UPDATE task SET status=3, progress=100 WHERE task_id="com.example.test.download.background.1536665611.774887.1"
open DB successfully
Query was executed successfully. Affected rows = 1
execute query: INSERT INTO task (task_id,url,status,progress,file_name,saved_dir,headers,resumable,show_notification,open_file_from_notification,time_created) VALUES ("com.example.test.download.background.1536665611.774887.18","https://example.com/A001C019_180901_A4R4-thumb.jpg?mtime=1536384218.72",1,0,"A001C019_180901_A4R4-thumb.jpg","/Users/ben/Library/Developer/CoreSimulator/Devices/2370C450-CC60-41A5-912A-E0E583C20889/data/Containers/Data/Application/8800C18D-EAFB-4670-B856-A5E9EF543B36/Documents","{authorization: Basic aabbcc=}",0,0,0,1536665630572)
execute query: SELECT * FROM task WHERE task_id = "com.example.test.download.background.1536665611.774887.2" ORDER BY id DESC LIMIT 1
open DB successfully
open DB successfully
database is locked
Load task successfully
Query was executed successfully. Affected rows = 1
methodCallHandler: enqueue
Lost connection to device.
*** First throw call stack:
(
	0   CoreFoundation                      0x0000000109dbb7f6 __exceptionPreprocess + 294
	1   libobjc.A.dylib                     0x0000000108f35035 objc_exception_throw + 48
	2   CoreFoundation                      0x0000000109dbb675 +[NSException raise:format:] + 197
	3   Foundation                          0x00000001089f6422 -[NSURL(NSURL) initFileURLWithPath:] + 127
	4   Foundation                          0x00000001089f6c8e +[NSURL(NSURL) fileURLWithPath:] + 45
	5   Runner                              0x0000000106f48ff7 -[FlutterDownloaderPlugin URLSession:downloadTask:didFinishDownloadingToURL:] + 503
	6   CFNetwork                           0x000000010ebcf5f8 __82-[NSURLSession delegate_downloadTask:didFinishDownloadingToURL:completionHandler:]_block_invoke + 38
	7   Foundation                          0x00000001089a32b7 __NS<…>

@hnvn
Copy link
Member

hnvn commented Sep 11, 2018

That would be the problem of multiple threads. Two threads access to DB at the moment and as far as the logs run, the later action was failed by DB locked then it returns an empty object, the saved file path can't be loaded, so application crashed because the function [NSURL initFileURLWithPath:] doesn't accept a nil value. That's the case I read from your logs.
Multiple threads in objective-c is quite dark for me. I think I should keep the information of running task in memory instead of DB, so I can get rid of loading tasks from DB too frequently and prevent this matter.

@hnvn
Copy link
Member

hnvn commented Sep 11, 2018

By the way, I am quite busy during the week. There can be time at the weekend. So I am willing if you can contribute to this repository.

@cbenhagen
Copy link
Author

I tried implementing a retry loop but it seemed quite ugly and didn't fully work. But I'd give it another shot. Objc is pretty new for me though. We could also have the DB handler in one thread and give it the data through a queue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants