Skip to content

fix(cpp): return error when querying a closed TsFileReader - #1151

Merged
ColinLeeo merged 5 commits into
apache:developfrom
ColinLeeo:fix-170-close-crash
Nov 27, 2026
Merged

ColinLeeo merged 5 commits into
apache:developfrom
ColinLeeo:fix-170-close-crash

Conversation

@ColinLeeo

Copy link
Copy Markdown
Contributor

Problem

Calling query() (or queryByRow, query_prepared, prepare_series) on a
TsFileReader after close() dereferenced the already-deleted internal
executor (tsfile_executor_) and crashed with a segfault instead of returning
a clear error.

Fix

Guard every query/execute entry point against a null tsfile_executor_ and
return E_INVALID_ARG, matching the existing closed-state guards in
get_all_devices() and prepare_table_series().

Test

Added TsFileReaderTest.QueryAfterCloseReturnsError: open a reader, query
successfully, close it, then query again and assert E_INVALID_ARG (no crash).

Calling query() (or queryByRow, query_prepared, prepare_series) after
close() dereferenced the already-deleted internal executor and crashed
with SIGSEGV. Guard every query/execute entry point against a null
executor and return E_INVALID_ARG, matching the existing closed-state
guards in get_all_devices() and prepare_table_series().
The CI spotless/clang-format check flagged a line over 80 columns. Break
the ASSERT_EQ macro arguments onto separate lines.

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

An active finding identifies a result-set leak when cleanup occurs after reader closure, requiring deletion independent of executor lifetime and a regression test for that sequence.

1 open issue
What changed in this PR

This PR adds closed-state checks to the C++ TsFileReader query and preparation APIs so they return E_INVALID_ARG instead of accessing a deleted executor.

Changes:

  • Guard tree, table, and prepared-query entry points against a missing executor.
  • Make result-set cleanup return early when the executor is absent.
  • Add a regression test covering successful querying followed by post-close query errors.
File Description
cpp/​test/​reader/​tsfile_reader_test.cc Tests post-close errors for path-list query() and queryByRow().
cpp/​src/​reader/​tsfile_reader.cc Adds closed-state guards to query, preparation, and cleanup methods.

Review effort: Balanced

Comment on lines +572 to +574
if (tsfile_executor_ == nullptr) {
return;
}
destroy_query_data_set() early-returned when the executor was gone,
leaking the result set for the sequence result->close(); reader.close();
destroy_query_data_set(result). Delete it directly so cleanup no longer
depends on executor lifetime, and add a regression test.
Clarify that deleting a result set after reader close is only safe once
result->close() has released its borrows from the deleted io_reader_.
# Conflicts:
#	cpp/test/reader/tsfile_reader_test.cc
@ColinLeeo
ColinLeeo merged commit bd8292b into apache:develop Nov 27, 2026
49 checks passed
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