Crash when starting multiple live queries concurrently

Description

Based on https://github.com/couchbase/couchbase-lite-core/wiki/Thread-Safety, c4queryobs_* (except c4queryobs_create which is thread-safe) is under database-exclusive section. 

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 last one 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.

LiteCore Code Reference: 
https://github.com/couchbase/couchbase-lite-core/blob/release/3.1/LiteCore/Database/LiveQuerier.cc#L43
https://github.com/couchbase/couchbase-lite-core/blob/release/3.1/LiteCore/Database/DatabaseImpl.cc#L410-L412

To fix this issue, the platform code needs to follow the thread safety instruction when using c4queryobs_* functions by making sure to call them under database-execlusive lock.

Activity

Show:

CB robot March 12, 2024 at 10:07 AM

Build couchbase-lite-ios-3.2.0-89 contains couchbase-lite-ios commit aa59d1d with commit message:
: Fix crash when creating multiple live queries concurrently (#3256)

Fixed
Pinned fields
Click on the next to a field label to start pinning.

Details

Assignee

Reporter

Story Points

Components

Sprint

Fix versions

Affects versions

Priority

Instabug

Open Instabug

PagerDuty

Sentry

Zendesk Support

Created April 14, 2023 at 1:10 AM
Updated August 31, 2024 at 10:56 AM
Resolved March 18, 2024 at 4:42 PM
Instabug