fix: Prevent crashes in multithreaded environments with sqlite. #495
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.
Summary
We've seen some reported crashes in sqlite internals coming from our SDK. I suspect this is a threading issue as most of the crashes occur on background threads and the customer is using multiple instances of Amplitude.
Our current threading strategy is to dispatch all db operations to a single queue, and to recreate a connection per operation. This should work with the default multithread threading strategy, where sqlite is thread safe if connections aren't shared between threads.
But, dispatching to a single queue doesn't mean that we always run on the same thread, and we have no synchronization mechanism between instances of the SDK. This means that a single thread may run connections to two different databases, though none should simultaneously be open.
A possible fix is to opt us in to a more strict treading mode for sqlite - the FULL_MUTEX mode linearizes calls between threads and enables full multithreaded access to sqlite objects. There is a tradeoff in performance, though this should be minimal.
Checklist