CBL-4428 : Fix crash when starting multiple live queries concurrently #3209
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background: Based on LiteCore’s thread safety doc, c4queryobs_* (except c4queryobs_create which is thread-safe) functions should be called under database-exclusive lock. Without doing that, when creating and starting multiple live queries concurrently, there could be a race condition as each live queries try to get the background database at the same time. The result of the race is that multiple background databases could be created but only the lastone stays and the rest will be freed. Therefore, some live queries will hold on to the bad background database which will cause the crash when the database is used.
Fixed CBLQueryObserver to follow thread-safety instruction by calling c4queryobs_* functions under the database exclusive lock.
Fixed Swift Query’s ListenerToken’s remove() function. After calling the objective-c CBLListenerToken’s remove() function, it will need to do some additional cleanup as well.
Removed CBLQuery and CBLQueryObserver’s description function to use the default description. Currently they are used for some logging but those excessive information is redundant with what LiteCore is currently logging. More importantly, they could cause some crashes as they don’t handle null objects properly.