Skip to content

fix(cpp): reject concurrent use of a shared TsFileReader - #1148

Open
ColinLeeo wants to merge 1 commit into
apache:developfrom
ColinLeeo:fix-shared-reader
Open

ColinLeeo wants to merge 1 commit into
apache:developfrom
ColinLeeo:fix-shared-reader

Conversation

@ColinLeeo

Copy link
Copy Markdown
Contributor

Problem

Sharing a single TsFileReader across threads intermittently crashes with
free(): invalid pointer / SIGSEGV (roughly 13-53% of runs). Concurrent
queries race on shared reader state:

  • the lazily-loaded TsFileMeta cache inside TsFileIOReader,
  • the TsFileExecutor query context,
  • the tsfile_reader_meta_pa_ PageArena used by get_timeseries_metadata,
  • ResultSet lifetime (result sets hold a raw pointer to the shared
    TsFileIOReader and read from it lazily in next()).

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:

  • Document on TsFileReader that it is not thread-safe and must not be
    shared across threads.
  • Add a single in-flight-operation slot (std::atomic<bool>, lock-free and
    never blocking). Every public operation claims it on entry; a query holds
    it until its ResultSet is destroyed via destroy_query_data_set(). A
    second, overlapping operation from another thread returns the new error
    code E_READER_BUSY instead of racing.
  • Add E_READER_BUSY (57) to the common error codes.

Callers that need concurrent reads should create one TsFileReader per
thread.

Verification

  • A standalone repro (8 threads sharing one reader, 2000 queries each,
    tree-model query(path_list, ...)) crashed on the first run before the
    change (free(): invalid size).
  • After the change, 15 consecutive runs completed with 0 crashes, 0
    unexpected errors and 0 data mismatches; overlapping queries are rejected
    with E_READER_BUSY.

@ColinLeeo
ColinLeeo force-pushed the fix-shared-reader branch 3 times, most recently from 28b5fa6 to 8e2729e Compare November 26, 2026 05:44
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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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_;

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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

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