Repository navigation
Conversation
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.
Contributor
Author
|
Superseded by #1168 after the branch was renamed. |
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.
Summary
Concurrent queries on one shared
TsFileReadercould crash because the reader, its executors and the file handle were shared mutable state with no synchronization.Changes
next()/close()and destruction serialize on it, so concurrent queries and result-set iteration no longer race on the shared file handle/executors.shared_ptrfor open result sets; fixes a single-threaded UAF whenbatch_sizechanges while a result set is still being consumed.Verification
ThreadSanitizerwarnings / data races across the new tests and surrounding reader suites.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
a3025e98and re-verified to closure, with documentation nits addressed inc15d328c. Commits:7c505e9e,a3025e98,c15d328c.