Repository navigation
Conversation
ColinLeeo
force-pushed
the
fix-shared-reader
branch
3 times, most recently
from
November 26, 2026 05:44
28b5fa6 to
8e2729e
Compare
Sharing a single TsFileReader across threads intermittently crashed with "free(): invalid pointer" / SIGSEGV. The reader is designed to be shared across queries (TsFileIOReader's lazily-loaded file metadata is reused), but two concurrent first queries both saw tsfile_meta_ready_ == false and deserialized the metadata into the same PageArena, corrupting the heap. Fix the specific races instead of serializing the whole reader: - Guard TsFileIOReader's lazy tsfile_meta_ load with a mutex (one-time initialization) so concurrent first queries no longer both parse metadata. - Make TsFileExecutor::query_exprs_ a local variable; it was a transient member written and read within execute() and raced across queries. - Guard TsFileReader::ensure_table_query_executor() and the tsfile_reader_meta_pa_ arena used by get_timeseries_metadata() with their own mutexes. The reader's node caches already carry their own mutexes and read_file_ uses pread, so concurrent queries otherwise proceed in parallel.
ColinLeeo
force-pushed
the
fix-shared-reader
branch
from
November 26, 2026 06:31
8e2729e to
daf119f
Compare
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Concurrent queries can replace an executor still used by callers or result sets, causing use-after-free, and the promised nonblocking reader-busy guard is missing.
2 open issues
What changed in this PR
This PR targets crashes caused by concurrent use of a shared C++ TsFileReader. The submitted changes add targeted locking and remove shared query-expression state.
Changes:
- Serialize lazy file-metadata loading.
- Guard table-executor creation/replacement and metadata-arena reuse.
- Use each call’s query expression directly instead of storing it on the executor.
| File | Description |
|---|---|
cpp/src/reader/tsfile_reader.h |
Adds mutexes for table-executor management and metadata-arena access. |
cpp/src/reader/tsfile_reader.cc |
Applies locks to executor setup and metadata retrieval. |
cpp/src/reader/tsfile_executor.h |
Removes the shared query-expression member. |
cpp/src/reader/tsfile_executor.cc |
Uses the query-expression argument directly. |
cpp/src/file/tsfile_io_reader.h |
Adds a mutex for lazy file-metadata loading. |
cpp/src/file/tsfile_io_reader.cc |
Locks metadata initialization and readiness checks. |
Review effort: Balanced
| unsigned char TsFileReader::get_file_version() const { return file_version_; } | ||
|
|
||
| int TsFileReader::ensure_table_query_executor(int batch_size) { | ||
| std::lock_guard<std::mutex> lock(table_executor_mutex_); |
Comment on lines
+364
to
+367
| std::mutex table_executor_mutex_; | ||
| // Guards tsfile_reader_meta_pa_, which get_timeseries_metadata() resets and | ||
| // rewrites on every call. | ||
| std::mutex meta_arena_mutex_; |
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
Executor replacement can free resources still used by concurrent queries or active result sets because locking does not cover their full lifetime and the reader-wide rejection guard is missing.
2 open issues
Review effort: Balanced
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.


Problem
Sharing a single
TsFileReaderacross threads intermittently crashes withfree(): invalid pointer/ SIGSEGV (roughly 13-53% of runs). Concurrentqueries race on shared reader state:
TsFileMetacache insideTsFileIOReader,TsFileExecutorquery context,tsfile_reader_meta_pa_PageArenaused byget_timeseries_metadata,ResultSetlifetime (result sets hold a raw pointer to the sharedTsFileIOReaderand read from it lazily innext()).The SDK does not document shared-reader thread safety, so sharing one reader
was never a supported pattern.
Fix
Rather than serialize access with a lock, make the existing (implicit)
contract explicit and detect violations:
TsFileReaderthat it is not thread-safe and must not beshared across threads.
std::atomic<bool>, lock-free andnever blocking). Every public operation claims it on entry; a query holds
it until its
ResultSetis destroyed viadestroy_query_data_set(). Asecond, overlapping operation from another thread returns the new error
code
E_READER_BUSYinstead of racing.E_READER_BUSY(57) to the common error codes.Callers that need concurrent reads should create one
TsFileReaderperthread.
Verification
tree-model
query(path_list, ...)) crashed on the first run before thechange (
free(): invalid size).unexpected errors and 0 data mismatches; overlapping queries are rejected
with
E_READER_BUSY.