Fixув SQlite duplicate telemetry race condition#2099
Merged
samson0v merged 2 commits intothingsboard:masterfrom Mar 10, 2026
Merged
Conversation
The test demonstrates a race condition in the SQLite event storage where
the Database background thread pre-fetches records into __next_batch cache
BEFORE event_pack_processing_done() deletes them from the database. This
causes get_event_pack() to return the same records twice — producing
duplicate telemetry on ThingsBoard.
The test inserts 20 messages, reads the first batch of 10, waits for the
pre-fetch, deletes the batch, then reads again. The second batch returns
the same 10 messages instead of the next 10 — confirming the bug.
A control test with MemoryEventStorage proves the issue is SQLite-specific
(Queue.get_nowait() is a destructive read, immune to this race condition).
Expected result: test_sqlite_prefetch_returns_stale_data_after_deletion FAILS
test_memory_storage_no_duplicates PASSES
Move can_prepare_new_batch() from get_event_pack() to event_pack_processing_done(), after delete_data(). This ensures the Database thread only pre-fetches the next batch AFTER the current batch has been deleted from the database, preventing stale cache from being served as duplicate telemetry.
|
Good fix, very needed to use SQLite |
|
Thanks, I was facing the same problem and I tested your solution. It solved my issue. |
Contributor
|
@lucas-souza-enerlab, many thanks for your contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fix SQLite storage duplicate telemetry race condition
Summary
When using SQLite event storage (
"type": "sqlite"), the gateway sends duplicate telemetry to ThingsBoard. Every reading appears twice with timestamps 1–10 ms apart. Switching to"type": "memory"eliminates the issue.Root cause
The
Databasebackground thread pre-fetches the next batch of records into an in-memory cache (__next_batch). The pre-fetch is unlocked bycan_prepare_new_batch(), which is called insideget_event_pack()— beforeevent_pack_processing_done()deletes the records from the database.This creates a race window:
Memory storage is not affected because
Queue.get_nowait()is a destructive read — once consumed, data cannot be re-read.Fix
Move
can_prepare_new_batch()fromget_event_pack()toevent_pack_processing_done(), afterdelete_data(). This ensures the Database thread only pre-fetches the next batch after the current batch has been deleted.Before (buggy):
After (fixed):
Commits
2b057d8bddb4ae76Reviewers can checkout commit 1 and run the test to reproduce the bug independently:
git checkout 2b057d8b python -m pytest tests/unit/service/test_sqlite_duplicate_race_condition.py -v -s # → SQLite test FAILS (duplicates detected), memory test PASSESThen checkout commit 2 to verify the fix:
git checkout ddb4ae76 python -m pytest tests/unit/service/test_sqlite_duplicate_race_condition.py -v -s # → Both tests PASSTest plan
test_sqlite_storageintests/unit/service/test_storage.pystill passesEnvironment