Skip to content

fix(cpp): make shared TsFileReader safe for concurrent queries - #1168

Open
ColinLeeo wants to merge 3 commits into
apache:developfrom
ColinLeeo:colin/fix-shared-reader-concurrency
Open

ColinLeeo wants to merge 3 commits into
apache:developfrom
ColinLeeo:colin/fix-shared-reader-concurrency

Conversation

@ColinLeeo

Copy link
Copy Markdown
Contributor

Summary

Concurrent queries on one shared TsFileReader could crash because the reader, its executors and the file handle were shared mutable state with no synchronization.

Changes

  • Add a per-reader recursive mutex; all public reader entry points, result-set next()/close() and destruction serialize on it, so concurrent queries and result-set iteration no longer race on the shared file handle/executors.
  • Hold the reader lock across C-API metadata conversion, fixing a heap-use-after-free found during review (metadata pointers into the shared arena were read after the lock had been released while another thread destroyed the arena).
  • Keep the table query executor alive via shared_ptr for open result sets; fixes a single-threaded UAF when batch_size changes while a result set is still being consumed.
  • Document the concurrency semantics in the C/C++/Python docs: a reader is safe to share across threads; a single result set must be advanced by one thread.

Verification

  • 12 new concurrency/lifecycle tests; Debug full reader suites pass.
  • TSAN: 0 ThreadSanitizer warnings / data races across the new tests and surrounding reader suites.
  • Independent ASan rerun (6 scenarios × 3 rounds) reports 0 issues after the delta fix; the baseline reproduced heap corruption/SIGSEGV before the fix.

Review

Self-reviewed, then an independent read-only review. The first review found a BLOCKER (C metadata conversion outside the reader lock) with an ASan repro; it was fixed in a3025e98 and re-verified to closure, with documentation nits addressed in c15d328c. Commits: 7c505e9e, a3025e98, c15d328c.

Multiple threads sharing one TsFileReader could corrupt the heap or
segfault: tree queries raced on TsFileExecutor::query_exprs_, the first
concurrent metadata load tore TsFileIOReader's tsfile_meta_, table
queries with different batch sizes deleted the executor that live
TableResultSets borrow (a single-threaded use-after-free as well), and
get_timeseries_metadata() callers reset a shared metadata arena.

Give each TsFileReader a recursive per-reader mutex. Every public reader
method takes it, and every result set the reader returns is bound to the
same lock so next()/get_next_tsblock()/close() are serialized with each
other and with new queries on that reader. The table executor is now a
shared_ptr that is replaced rather than deleted, and result sets keep it
alive, so an earlier result set survives a later batch-size change.

Concurrent use is now safe but serialized; the contract is documented in
tsfile_reader.h, result_set.h, the C wrapper header and the C++ skill
reference. Add a six-case concurrency/lifetime stress test that aborted
with heap corruption or SIGSEGV before the fix and passes, together with
the neighboring reader suites, under ThreadSanitizer afterwards.
The C wrapper's tsfile_reader_get_timeseries_metadata_all() and
tsfile_reader_get_timeseries_metadata_for_devices() copied the C++
metadata after the reader call had returned and its lock was released.
The returned timeseries indexes live in a per-reader arena that the next
metadata call - from any thread - resets, so a concurrent caller freed
them while another thread was still converting (heap-use-after-free in
populate_c_metadata_map_from_cpp, reproduced under ASan).

Add TsFileReader::with_reader_lock() and run the metadata call and the
C conversion inside it, so the copy completes before any other thread
can reset the arena.  Document the helper and the C-wrapper guarantee in
the headers and the C++/C skill references.

Also harden result-set lifetime: the tree executor is now a shared_ptr
and every result set keeps the executor it borrows alive, which makes
destroying an unclosed result set after close() safe (destroy_query_
data_set() no longer has to dereference an executor that close() may
have deleted).  Iterating after close() remains invalid and documented.

Tests: add a C-wrapper concurrency test that reads every converted field
(device path, measurement name, row count), which reproduced the ASan
use-after-free without the wrapper fix and passes with it; add
QDSWithTimeGenerator, table queryByRow, prepared-query and
destroy-after-close concurrency cases; use ASSERT_NO_FATAL_FAILURE for
the fixture writers.  Verified with the full Debug suite (1928 tests),
the reader suites under ASan (81 tests, no reports) and under TSan (81
tests, no warnings).
Address the documentation nits from the review of a3025e9:

- with_reader_lock(): state that every other operation on the reader is
  blocked for the duration, and that fn must not wait for another thread
  or for a different reader (long serialization / cross-reader lock
  cycles);
- TsFileReader thread-safety notes: point metadata callers at
  with_reader_lock(), and state consistently that destroying a result
  set after close() is safe (memory only) while iterating it is not;
- TsFileTreeReader: document that the facade inherits the reader's
  serialized concurrency and metadata lifetime contract;
- cwrapper concurrency test: include <cstdio> for remove() instead of
  relying on a transitive include.
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.

1 participant