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

CBL-4428 : Fix crash when starting multiple live queries concurrently #3209

Merged
merged 1 commit into from
Dec 18, 2023

Conversation

pasin
Copy link
Contributor

@pasin pasin commented Dec 17, 2023

  • 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.

@pasin pasin force-pushed the fix/CBL-4428 branch 2 times, most recently from f683109 to 32cfc08 Compare December 17, 2023 04:40
@pasin pasin changed the title CBL-4428 : Fix crash when creating multiple live queries concurrently CBL-4428 : Fix crash when starting multiple live queries concurrently Dec 17, 2023
* 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.
@pasin pasin merged commit dceea1a into release/3.1 Dec 18, 2023
5 checks passed
@pasin pasin deleted the fix/CBL-4428 branch December 18, 2023 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants