Repository navigation
Fix server abort when cancelling postgresql table function queries - #105949
tiandiwonder merged 4 commits into
Conversation
|
Hello! Could you create an issue with a reproducible if possible, so we can investigate further? |
|
Hey I created an issue here And a repo is available here |
|
The fix seems correct so far. Can you import the test? We have a few .sh tests, and it should be easy to add one for this case. |
|
Hey i added a test in the existing postgres test suite |
|
Workflow [PR], commit [47fb31d] Summary: ✅
AI ReviewSummaryThis PR catches exceptions from Final VerdictStatus: ✅ Approve |
|
Better to update change log entry: |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 7/7 (100.00%) | Lost baseline coverage: none · Uncovered code |
|
📊 Cloud Performance Report ✅ AI verdict: This PR only wraps the PostgreSQL source's query-cancellation call in a try/catch and adds an integration test — it does not touch any code path the ClickBench SELECT queries execute. The three flagged improvements (Q4 -15.49%, Q15 -13.6%, Q32 -5.96%) are therefore off-path and cannot plausibly be caused by this change, so all three are downgraded to not-sure as run-to-run variance. No real performance impact is expected from these queries. clickbenchFlagged queries (3 of 43)
q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
|
Hi @rorylshanks @tiandiwonder — a couple of suggestions to make this PR easier for reviewers and the changelog reader. Description (currently
Ignore this if the current title/body is intentional. |
Cherry pick #105949 to 26.3: Fix server abort when cancelling postgresql table function queries
… table function queries
Cherry pick #105949 to 26.4: Fix server abort when cancelling postgresql table function queries
… table function queries
Cherry pick #105949 to 26.5: Fix server abort when cancelling postgresql table function queries
… table function queries
Backport #105949 to 26.5: Fix server abort when cancelling postgresql table function queries
Backport #105949 to 26.4: Fix server abort when cancelling postgresql table function queries
Cherry pick #105949 to 25.8: Fix server abort when cancelling postgresql table function queries
… table function queries
Backport #105949 to 26.3: Fix server abort when cancelling postgresql table function queries
ThreadSanitizer reports an 8-byte read/write race on PostgreSQLSource<T>::tx:
read onCancel, PostgreSQLSource.cpp:201, cancelling thread,
via ISource::cancel <- PipelineExecutor::cancel
previous write onStart, PostgreSQLSource.cpp:88, executor pool thread,
via prepare <- PipelineExecutor::initializeExecution
The two threads hold different mutexes, so nothing orders the accesses.
tx is a plain std::shared_ptr, while the started/is_completed flags declared
beside it are atomic. The guard "if (!started.load() && tx && tx->conn().is_open())"
loads a different object, so it establishes no happens-before edge with the
"tx = std::make_shared<T>(...)" store. This is onCancel's intended operating
window rather than a rare interleaving: onCancel exists precisely for the case
where onStart is still blocked in pqxx::stream_from(..., from_query, ...). With
an instrumented build the racing branch was taken on 40 of 40 cancellations.
Publish tx under a new dedicated std::mutex tx_mutex, and have onCancel take a
shared_ptr snapshot under that lock and use the snapshot with the lock released.
The snapshot copy also removes a lifetime hazard: the cancelling thread no longer
dereferences a pointer another thread may reset underneath it.
The lock must not cover either pqxx call. The transaction constructor issues
BEGIN (contrib/libpqxx/src/transaction.cxx) and stream_from issues COPY; holding
tx_mutex across the latter would make onCancel block on the lock while onStart
waits for PostgreSQL, turning the hang onCancel is meant to interrupt into a
permanent one. A deliberately mis-scoped variant was built to confirm this is
observable: it makes test_kill_query_when_postgresql_cancel_connection_fails
fail, so that suite is a real oracle for a wrong lock scope.
The lock is acquired inside a try/catch because onCancel is noexcept and
std::lock_guard can throw std::system_error, which would otherwise reach
std::terminate.
PostgreSQLSource is the only carrier among the sources: SQLiteSource never
reassigns its pointer, MySQLWithFailoverSource::onCancel touches only atomics,
RemoteSource's executor is const after construction, ReadFromDistributedPlanSource
already guards its executor with a mutex (the pattern followed here), and
MongoDBSource has no onCancel.
The unguarded read is pre-existing: it came in with the "Use atomics" change,
which turned "if (!started)" into the current guard and added the tx read while
leaving the pointer unsynchronized. PR ClickHouse#105949 only wrapped cancel_query in
try/catch.
The report itself does not reproduce locally: the branch arms every time, but
KILL QUERY arrives over a docker round trip about 100 ms after the store, so
TSan's shadow cell for that word is evicted before the read. CI hits it because
the AST fuzzer cancels in process. Forcing it locally would need failpoint
scaffolding in product code, so the evidence is the CI report plus the analysis
above.
Validated under a TSan build: test_postgresql_kill_query 30/30 (5 tests x 6,
random order), test_storage_postgresql and test_dictionaries_postgresql 39/39.
Found by Stress test (amd_tsan) and Stress test (arm_tsan), STID 1855-2bf1.
…postgresql table function queries
…5949 Backport ClickHouse#105949 to 26.4: Fix server abort when cancelling postgresql table function queries
…postgresql table function queries
Changelog category (leave one):
Closes #105998
Our CH clusters are crashing when the postgresql() function is cancelled with the below crash_log
PostgreSQLSource::onCancel is noexcept, but it called pqxx::connection::cancel_query directly. libpqxx can throw pqxx::sql_error when cancelling the PostgreSQL query fails, which then escapes the noexcept cancellation path and causes std::terminate.
This wraps the cancellation call, logs the failure, and still marks the PostgreSQL connection as broken. Query cleanup can now continue without aborting the server.
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a server crash that could occur when a query reading from PostgreSQL — via the postgresql table function, the PostgreSQL table engine, or a dictionary with a PostgreSQL source — was cancelled (for example with KILL QUERY) and cancelling the remote PostgreSQL query failed.
Version info
26.6.1.416(included in26.6and later)26.5.2.37,26.4.4.35,26.3.17.33