Repository navigation
Conversation
…e read `PostgreSQLSource::onCancel` runs on the KILL / timeout / partial-result thread. It called `conn().is_open()` (`PQstatus`) and `cancel_query()`, which builds its request with `PQgetCancel(m_conn)` and so reads `conn->sock`, `raddr`, the backend pid and the cancel key, while the pipeline thread was inside libpq on the same `PGconn` (`PQexec` of the COPY in `stream_from`, or `PQgetCopyData` in `read_row`). When that read fails, `pqReadData` drops the connection and writes `conn->sock`. libpq allows one thread per `PGconn`. Only `PQcancel` on a `PGcancel` built beforehand may run on another thread: it reads only that copy. Since ClickHouse#118995 shuts the socket down right after the cancel, a failed read is the normal outcome of a cancel. TSan saw the pair on master (`Integration tests (amd_tsan, 1/6)`, ea9f598) when the reader's `recv` returned ECONNRESET, which is not an acquire, so nothing ordered it after the cancelling thread's read. Build the `PGcancel` on the reading thread in `onStart`, after the transaction exists and before the COPY starts, and publish it under `tx_mutex` with the socket duplicate. `onCancel` and the destructor only call `PQcancel` on it. This is `cancel_query` split in two, so the request and the transport do not change. `cancel_mutex` is removed: `PQcancel` does not write the `PGcancel`, and `onCancel` cannot overlap the destructor. pqxx hands out the raw `PGconn` only through the protected static `largeobject::raw_connection` (also present in libpqxx 8.0.2), reached through a derived struct. A NULL from `PQgetCancel` (invalid socket or OOM) refuses the read before it starts, like a failed socket duplication. The new test puts a proxy between the source and PostgreSQL that resets the data connection when the cancel request arrives, before forwarding it. On a TSan build it reported the CI race in 8 of 10 runs without this change and in none with it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`CancelResettingProxy` decided whether a connection carries the cancel request from the first `recv` alone, which TCP may cut short of the 8-byte packet, and the test never checked that the reset happened, so a plain cancel without the reset also passed. Buffer the client's first 8 bytes before deciding, and assert `connection_was_reset()`. Reword the `onCancel` comment that said the cancel is sent first "while the connection can still address it": the cancel handle is now built in `onStart`, so that no longer holds. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 rounds: 1 finding, then 0 on the fixed head) plus my own review (5 nits over 2 rounds).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261003-133500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261003-132100 |
|
Workflow [PR], commit [a8ba928] Summary: ✅
AI ReviewSummaryThis PR fixes the PostgreSQL cancellation race by creating the Final Verdict✅ No findings. LLVM Coverage ReportMeasured on commit a8ba928.
Changed lines: Changed C/C++ lines covered: 40/43 (93.02%) · Uncovered code |
Build profile diff (arm_release)Commit See the job log for details. |
Picks up ClickHouse#123679, which fixes the 04896_orfill_merge_group_by_overflow_any failure in Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel).
CI finish ledger - 3f57103Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
None of these touches the PostgreSQL source this PR changes, and Session id: cron:our-pr-ci-monitor:20261003-183140 |
|
Fixing PR for 03100_lwu_41_bytes_limits (arm_binary, ParallelReplicas timeout) created: #123765 |
|
Fixing PR for 05237_with_expression_alias_scope_descendant (DBReplicated): #123774 |
CI finish ledger - cfee6b1Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
None of these touches the PostgreSQL source this PR changes, and Session id: cron:our-pr-ci-monitor:20261003-233424 |
|
@groeneai could you merge the fresh master to resolve the CI issues? |
|
Merged the current master in a8ba928. The diff against master is unchanged (the same three files), and |
Backport #123693 to 26.8: Do not read the PostgreSQL connection from the thread that cancels the read
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
Related: #118995, #113150
TSan data race on master,
Integration tests (amd_tsan, 1/6): report. The server aborted and took four tests oftest_postgresql_kill_querydown with it.PostgreSQLSource::onCancelruns on the KILL / timeout thread and calledconn().is_open()andconn().cancel_query(), i.e.PQstatusandPQgetCancelon thePGconn, while the pipeline thread was inside libpq on the same connection. When that read fails,pqDropConnectionwritesconn->sock. libpq allows one thread perPGconn; onlyPQcancelon aPGcancelbuilt beforehand may run on another thread.The
PGcancelis now built by the reading thread inonStart(before the COPY starts) and published undertx_mutexwith the socket duplicate.onCanceland the destructor only callPQcancelon it, socancel_mutexis gone. The request sent to the server is the same onecancel_querysent. pqxx exposes the raw connection only throughlargeobject::raw_connection(protected static, also in libpqxx 8.0.2); if a public accessor in the libpqxx fork is preferred, I can do that instead.New test
test_kill_query_when_the_connection_resets_during_the_cancel: a proxy resets the data connection when the cancel request arrives, so the reader drops the connection while the cancel is in flight. On a TSan build it failed 12/15 without the fix with the CI stack pair, and passed 10/10 with it; the whole module passed 3/3 on TSan and 5/5 on debug.Workflow [PR]
Sync PR [sync-upstream/pr/123693]
Version info
26.10.1.1861-master(included in26.10and later)26.8.43.13