diff --git a/.github/workflows/publish-manual-docker.yml b/.github/workflows/publish-manual-docker.yml index 16db384e..7e5a2261 100644 --- a/.github/workflows/publish-manual-docker.yml +++ b/.github/workflows/publish-manual-docker.yml @@ -20,6 +20,9 @@ on: permissions: contents: read +# Not the certified publisher's group: a queued manual run would replace a +# pending certified publication. The latest-release check right before +# moving :latest keeps the two from overwriting each other's image. concurrency: group: manual-release-docker cancel-in-progress: false @@ -70,6 +73,7 @@ jobs: password: ${{ github.token }} - name: Publish version and latest tags env: + GH_TOKEN: ${{ github.token }} TAG: ${{ inputs.tag }} LINUXSERVER_AMD64: ${{ steps.source.outputs.linuxserver_amd64 }} LINUXSERVER_ARM64: ${{ steps.source.outputs.linuxserver_arm64 }} @@ -77,6 +81,20 @@ jobs: PLEXINC_ARM64: ${{ steps.source.outputs.plexinc_arm64 }} run: | set -eu + # :latest follows the repository's latest GitHub release, so a + # manual publication of an older or newer-but-unreleased tag never + # replaces the image of the current (possibly certified) release. + # Decided before any tag is written (an API error fails the run + # while it can still be rerun), and only while no certified + # publication is running, which would set :latest itself. + latest_release=$(gh release list --repo "$GITHUB_REPOSITORY" --exclude-drafts --exclude-pre-releases \ + --limit 20 --json tagName,isLatest --jq '[.[] | select(.isLatest)][0].tagName // ""') + certified_publications=$(gh run list --repo "$GITHUB_REPOSITORY" --workflow publish-certified-release.yml \ + --limit 10 --json status --jq '[.[] | select(.status != "completed")] | length') + move_latest=false + if [[ "$latest_release" == "$TAG" && "$certified_publications" == 0 ]]; then + move_latest=true + fi publish() { local repository=$1 amd64=$2 arm64=$3 if docker buildx imagetools inspect "$repository:$TAG" >/dev/null 2>&1; then @@ -86,8 +104,12 @@ jobs: docker buildx imagetools create --tag "$repository:$TAG" "$amd64" "$arm64" digest=$(docker buildx imagetools inspect "$repository:$TAG" | awk '/^Digest:/ {print $2; exit}') [[ "$digest" =~ ^sha256:[0-9a-f]{64}$ ]] - docker buildx imagetools create --tag "$repository:latest" "$repository@$digest" - echo "$repository:$TAG -> $digest" >> "$GITHUB_STEP_SUMMARY" + if [[ "$move_latest" == true ]]; then + docker buildx imagetools create --tag "$repository:latest" "$repository@$digest" + echo "$repository:$TAG and :latest -> $digest" >> "$GITHUB_STEP_SUMMARY" + else + echo "$repository:$TAG -> $digest (:latest unchanged; latest release ${latest_release:-none})" >> "$GITHUB_STEP_SUMMARY" + fi } publish ghcr.io/cgnl/plex-postgresql-linuxserver "$LINUXSERVER_AMD64" "$LINUXSERVER_ARM64" publish ghcr.io/cgnl/plex-postgresql-plexinc "$PLEXINC_AMD64" "$PLEXINC_ARM64" diff --git a/CHANGELOG.md b/CHANGELOG.md index 13914a8d..d36ef0d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- A read that fails while PostgreSQL executes it now reports the error. Reads that do not stream (for example `LIMIT 1` queries) returned an empty result instead, with only a log line. +- `ROLLBACK` after the PostgreSQL session was lost mid-transaction clears the transaction; the handle could not start a new transaction until it was closed. It succeeds, unless the session broke during `COMMIT`: then it reports that the commit's outcome is unknown. Closed or destroyed connections no longer leave transaction state behind for a later connection at the same address. +- A connection error noted during one step no longer makes the next failing statement on the same thread retry as if its connection had failed. +- The result-description cache tracks uncommitted DDL per session: it resumes after DDL that ran outside a transaction, and another session's commit or autocommit write (or an inner `RELEASE SAVEPOINT`) no longer ends the window early. +- A statement whose stream was read ahead is no longer re-run from the first row when it is stepped from another connection. +- Manual image publication only moves `:latest` when it publishes the repository's latest release, checked right before promotion. + ## [1.3.26] - 2026-11-06 ### Fixed diff --git a/rust/plex-pg-core/src/bin/runtime_e2e.rs b/rust/plex-pg-core/src/bin/runtime_e2e.rs index 8f889faa..0511d141 100644 --- a/rust/plex-pg-core/src/bin/runtime_e2e.rs +++ b/rust/plex-pg-core/src/bin/runtime_e2e.rs @@ -399,7 +399,7 @@ fn run() -> Result<()> { &'static str, fn(&Api, &Db<'_>, &Db<'_>, &mut Client) -> Result<()>, ); - let cases: [Case; 21] = [ + let cases: [Case; 22] = [ ( "native_uuid_binding_ownership", |api, first, _, observer| { @@ -1153,6 +1153,19 @@ fn run() -> Result<()> { ) }, ), + ( + // `LIMIT 1` reads take the eager (non-streaming) path; a query that + // fails while executing must report an error, not an empty result. + "eager_query_errors", + |api, first, _, _| { + let stmt = first.prepare("SELECT CAST(1 AS INTEGER) / CAST(abs(random()) * 0 AS INTEGER) AS boom LIMIT 1")?; + let code = unsafe { (api.step)(stmt.handle) }; + require(code != ROW && code != DONE, "failed eager query reported success")?; + require(!api.error(first.handle).is_empty(), "failed eager query lacks diagnostics")?; + require(unsafe { (api.step)(stmt.handle) } != ROW, "failed eager query produced rows")?; + Ok(()) + }, + ), ( // Plex 1.43.5 migration 202609141200 records a preference this way; // a failure aborted the migration on a fresh database. @@ -1274,6 +1287,12 @@ fn run() -> Result<()> { .is_err(), "interrupted transaction silently resumed in autocommit", )?; + // The server discarded the transaction with the session; ROLLBACK + // must clear it so the handle can start new transactions. + first.exec("ROLLBACK")?; + first.exec("BEGIN")?; + first.exec("INSERT INTO runtime_items (unique_value) VALUES (603)")?; + first.exec("COMMIT")?; require( observer.query_one("SELECT COUNT(*) FROM runtime_e2e.runtime_items WHERE unique_value IN (601,602)", &[]).map_err(|error| error.to_string())?.get::<_, i64>(0) == 0, "interrupted transaction persisted partial writes", diff --git a/rust/plex-pg-core/src/db_interpose_exec/pg_path.rs b/rust/plex-pg-core/src/db_interpose_exec/pg_path.rs index a1685306..6e2e06a1 100644 --- a/rust/plex-pg-core/src/db_interpose_exec/pg_path.rs +++ b/rust/plex-pg-core/src/db_interpose_exec/pg_path.rs @@ -260,6 +260,9 @@ pub(crate) fn exec_via_postgres( } let status = crate::libpq_helpers::rust_pq_result_status(res); + // Also on failure: a batch can commit DDL before a later + // statement fails. + note_write_done(pg_conn, CStr::from_ptr(sql).to_bytes(), true); if status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK { clear_pg_last_error(pg_conn); let cmd_tuples = crate::libpq_helpers::rust_pq_cmd_tuples(res); @@ -365,3 +368,22 @@ pub(crate) fn exec_via_postgres( SQLITE_OK } } + +/// A write ran on `conn`. DDL opens a description-cache window that ends once +/// the session is idle; outside a transaction that is right away. +pub(crate) fn note_write_done(conn: *mut crate::ffi_types::PgConnection, sql: &[u8], batch: bool) { + if conn.is_null() { + return; + } + let ddl = if batch { + crate::descriptor_cache::batch_contains_ddl(sql) + } else { + crate::descriptor_cache::starts_with_ddl(sql) + }; + if ddl { + crate::descriptor_cache::ddl_executed(conn as usize); + } + if unsafe { crate::libpq_helpers::rust_pq_transaction_status((*conn).conn) } == 0 { + crate::descriptor_cache::connection_idle(conn as usize); + } +} diff --git a/rust/plex-pg-core/src/db_interpose_step.rs b/rust/plex-pg-core/src/db_interpose_step.rs index 19a0f6da..c2db3129 100644 --- a/rust/plex-pg-core/src/db_interpose_step.rs +++ b/rust/plex-pg-core/src/db_interpose_step.rs @@ -199,6 +199,7 @@ unsafe fn my_sqlite3_step_impl(p_stmt: *mut sqlite3_stmt) -> c_int { let original_sql = call_sqlite3_sql(p_stmt); let sql = crate::db_interpose_helpers::cstr_to_str_or_empty(original_sql); if !crate::pg_config::is_sqlite_passthrough_str(sql) + && !statement_is_rollback(p_stmt, sql) && !crate::pg_client::transaction::session_ready(handle_conn) { return SQLITE_ERROR; @@ -472,3 +473,16 @@ unsafe fn my_sqlite3_step_impl(p_stmt: *mut sqlite3_stmt) -> c_int { } rc } + +/// ROLLBACK may run on a lost session (it clears the tracked transaction). +/// PostgreSQL statements wrap a placeholder SQLite statement, so the original +/// SQL comes from the shim's statement when there is one. +unsafe fn statement_is_rollback(p_stmt: *mut sqlite3_stmt, sqlite_sql: &str) -> bool { + let pg_stmt = crate::pg_statement::rust_stmt_find(p_stmt as usize) as *mut PgStmt; + let sql = if pg_stmt.is_null() || (*pg_stmt).sql.is_null() { + sqlite_sql + } else { + crate::db_interpose_helpers::cstr_to_str_or_empty((*pg_stmt).sql) + }; + crate::pg_client::transaction::is_rollback_sql(sql) +} diff --git a/rust/plex-pg-core/src/db_interpose_step/retry.rs b/rust/plex-pg-core/src/db_interpose_step/retry.rs index 607c1e3b..5e4ebc6c 100644 --- a/rust/plex-pg-core/src/db_interpose_step/retry.rs +++ b/rust/plex-pg-core/src/db_interpose_step/retry.rs @@ -79,5 +79,8 @@ pub(super) fn maybe_retry_step(p_stmt: *mut sqlite3_stmt, rc: c_int) -> Option c_int { + if crate::libpq_helpers::rust_pq_status(pq) != 0 { + 0 + } else { + -1 + } +} + pub(super) fn first_execute_impl( pg_stmt: *mut PgStmt, exec_conn_io: *mut *mut PgConnection, @@ -113,6 +126,7 @@ pub(super) fn first_execute_impl( exec_conn, &mut stmt_guard, &mut conn_guard, + pg_conn_error_out, ) } } diff --git a/rust/plex-pg-core/src/db_interpose_step_read_utils/first_execute/connection.rs b/rust/plex-pg-core/src/db_interpose_step_read_utils/first_execute/connection.rs index 0c25eba1..2c000bfc 100644 --- a/rust/plex-pg-core/src/db_interpose_step_read_utils/first_execute/connection.rs +++ b/rust/plex-pg-core/src/db_interpose_step_read_utils/first_execute/connection.rs @@ -183,6 +183,7 @@ pub(super) unsafe fn ensure_connection_ready( log_error("=== END DIAGNOSTIC ==="); log_error("STEP READ: Attempting PQreset..."); crate::libpq_helpers::rust_pq_reset(ec.conn); + crate::descriptor_cache::connection_idle(exec_conn as usize); if crate::libpq_helpers::rust_pq_status(ec.conn) != CONNECTION_OK { log_error("STEP READ: PQreset failed, trying fresh PQconnectdb..."); diff --git a/rust/plex-pg-core/src/db_interpose_step_read_utils/first_execute/eager_fetch.rs b/rust/plex-pg-core/src/db_interpose_step_read_utils/first_execute/eager_fetch.rs index 822335ce..f0f45496 100644 --- a/rust/plex-pg-core/src/db_interpose_step_read_utils/first_execute/eager_fetch.rs +++ b/rust/plex-pg-core/src/db_interpose_step_read_utils/first_execute/eager_fetch.rs @@ -6,6 +6,7 @@ pub(super) unsafe fn eager_fetch_result( exec_conn: *mut PgConnection, stmt_guard: &mut Option, conn_guard: &mut PthreadMutexGuard, + pg_conn_error_out: *mut c_int, ) -> c_int { let s = &mut *pg_stmt; let ec = &*exec_conn; @@ -39,18 +40,34 @@ pub(super) unsafe fn eager_fetch_result( *stmt_guard = None; // unlock return STEP_RESULT_ROW; } - } else if !s.result.is_null() { - let err2 = crate::libpq_helpers::rust_pq_error_message(ec.conn); - let ctx = CString::new("EAGER FALLBACK").unwrap(); - let fallback_err = if err2.is_null() { - CString::new("?").unwrap() + } else { + // A failed query is an error, as on the streaming path; it used to + // come back as an empty result with only a log line. + if s.result.is_null() { + crate::db_interpose_exec::pg_path::set_pg_last_error( + s.conn, + "PostgreSQL returned no result", + ); } else { - CString::new(cstr_to_str(err2)).unwrap_or_else(|_| CString::new("?").unwrap()) - }; - log_sql_fallback(s.sql, s.pg_sql, fallback_err.as_ptr(), ctx.as_ptr()); - crate::libpq_helpers::rust_pq_clear(s.result); - s.result = std::ptr::null_mut(); + let err2 = crate::libpq_helpers::rust_pq_error_message(ec.conn); + let ctx = CString::new("EAGER FALLBACK").unwrap(); + let fallback_err = if err2.is_null() { + CString::new("?").unwrap() + } else { + CString::new(cstr_to_str(err2)).unwrap_or_else(|_| CString::new("?").unwrap()) + }; + log_sql_fallback(s.sql, s.pg_sql, fallback_err.as_ptr(), ctx.as_ptr()); + crate::db_interpose_exec::pg_path::record_pg_result_error(s.conn, exec_conn, s.result); + crate::libpq_helpers::rust_pq_clear(s.result); + s.result = std::ptr::null_mut(); + } + s.result_conn = std::ptr::null_mut(); + s.read_done = first_fetch_failure_state(ec.conn); crate::pg_client::rust_pool_check_health(exec_conn as *mut c_void); + *exec_conn_io = exec_conn; + *stmt_guard = None; // unlock + set_pg_conn_error(pg_conn_error_out); + return STEP_RESULT_ERROR; } s.read_done = 1; diff --git a/rust/plex-pg-core/src/db_interpose_step_read_utils/first_execute/streaming_fetch.rs b/rust/plex-pg-core/src/db_interpose_step_read_utils/first_execute/streaming_fetch.rs index 4f6786f3..1d25ffc1 100644 --- a/rust/plex-pg-core/src/db_interpose_step_read_utils/first_execute/streaming_fetch.rs +++ b/rust/plex-pg-core/src/db_interpose_step_read_utils/first_execute/streaming_fetch.rs @@ -117,16 +117,3 @@ pub(super) unsafe fn streaming_fetch_result( set_pg_conn_error(pg_conn_error_out); STEP_RESULT_ERROR } - -/// `read_done` after the first fetch failed. No row has reached the caller, so -/// when the session was lost the statement stays retryable: the step retry -/// re-runs it on the recovered session (a read prepared from the description -/// cache meets a dead session only here, not at prepare). Any other failure is -/// final. -unsafe fn first_fetch_failure_state(pq: *mut crate::libpq_helpers::PGconn) -> c_int { - if crate::libpq_helpers::rust_pq_status(pq) != 0 { - 0 - } else { - -1 - } -} diff --git a/rust/plex-pg-core/src/db_interpose_step_read_utils/reexecution.rs b/rust/plex-pg-core/src/db_interpose_step_read_utils/reexecution.rs index 90779b22..aa1e4f7a 100644 --- a/rust/plex-pg-core/src/db_interpose_step_read_utils/reexecution.rs +++ b/rust/plex-pg-core/src/db_interpose_step_read_utils/reexecution.rs @@ -12,7 +12,9 @@ pub(crate) fn should_clear_cross_thread_result( if stmt.result_conn == exec_conn { return false; } - stmt.streaming_mode != 0 + // A stream that was read ahead no longer needs its connection: its + // remaining rows are local. Requerying it would deliver from row 0 again. + stmt.streaming_mode != 0 && stmt.spill_state == 0 } pub(crate) fn should_use_streaming(stmt: *const PgStmt, disable_streaming_env: bool) -> bool { diff --git a/rust/plex-pg-core/src/db_interpose_step_write_utils/connection.rs b/rust/plex-pg-core/src/db_interpose_step_write_utils/connection.rs index 5ed069a7..44f8e9d3 100644 --- a/rust/plex-pg-core/src/db_interpose_step_write_utils/connection.rs +++ b/rust/plex-pg-core/src/db_interpose_step_write_utils/connection.rs @@ -194,6 +194,7 @@ pub extern "C" fn rust_step_write_prepare_connection( log_error("=== END DIAGNOSTIC ==="); log_error("STEP WRITE: Attempting PQreset..."); crate::libpq_helpers::rust_pq_reset(ec.conn); + crate::descriptor_cache::connection_idle(exec_conn as usize); if crate::libpq_helpers::rust_pq_status(ec.conn) != CONNECTION_OK { log_error("STEP WRITE: PQreset failed, trying fresh PQconnectdb..."); crate::pg_client::rust_stmt_cache_clear(exec_conn as *mut c_void); diff --git a/rust/plex-pg-core/src/db_interpose_step_write_utils/write_exec.rs b/rust/plex-pg-core/src/db_interpose_step_write_utils/write_exec.rs index d81508db..22b01040 100644 --- a/rust/plex-pg-core/src/db_interpose_step_write_utils/write_exec.rs +++ b/rust/plex-pg-core/src/db_interpose_step_write_utils/write_exec.rs @@ -245,6 +245,7 @@ pub extern "C" fn rust_step_write_execute_and_finalize( } crate::db_interpose_exec::pg_path::copy_pg_outcome(handle_conn, exec_conn); + crate::db_interpose_exec::pg_path::note_write_done(exec_conn, cstr_bytes(stmt.sql), false); stmt.write_executed = 1; if !res.is_null() { // A statement Plex wrote with RETURNING produces rows, as in diff --git a/rust/plex-pg-core/src/descriptor_cache.rs b/rust/plex-pg-core/src/descriptor_cache.rs index 040bead5..0c03e010 100644 --- a/rust/plex-pg-core/src/descriptor_cache.rs +++ b/rust/plex-pg-core/src/descriptor_cache.rs @@ -12,10 +12,10 @@ //! - DDL seen anywhere in the shim (prepare, free, `sqlite3_exec`) clears the //! cache and bumps a generation; a description taken before the bump is not //! stored afterwards. -//! - While DDL may be uncommitted, nothing is stored, and the end of the next -//! transaction clears the cache again (another session may have described -//! the old shape before the commit, or this session the new shape before a -//! rollback). +//! - While any session has executed DDL that is not yet committed or rolled +//! back, nothing is stored; when that session is idle again the cache is +//! cleared (another session may have described the old shape before the +//! commit, or this session the new shape before a rollback). //! - Statements that name a temporary table are never cached (temporary //! tables are per session; the cache is per process). //! - An execution whose columns differ from the description replaces it (see @@ -25,7 +25,7 @@ use std::collections::HashMap; use std::ffi::CStr; use std::os::raw::c_int; -use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{LazyLock, Mutex}; use crate::libpq_helpers::{self, PGresult, PG_COPYRES_ATTRS}; @@ -41,6 +41,9 @@ struct Cache { entries: HashMap, Vec<(c_int, usize)>>, len: usize, temporary_tables: Vec>, + /// Sessions (connection addresses) that executed DDL not yet committed + /// or rolled back. Nothing is stored while any exists. + ddl_sessions: Vec, } static CACHE: LazyLock> = LazyLock::new(|| { @@ -48,10 +51,10 @@ static CACHE: LazyLock> = LazyLock::new(|| { entries: HashMap::new(), len: 0, temporary_tables: Vec::new(), + ddl_sessions: Vec::new(), }) }); static GENERATION: AtomicU64 = AtomicU64::new(0); -static DDL_PENDING: AtomicBool = AtomicBool::new(false); fn with_cache(f: impl FnOnce(&mut Cache) -> T) -> T { let mut cache = CACHE.lock().unwrap_or_else(|poison| poison.into_inner()); @@ -90,12 +93,13 @@ pub(crate) fn lookup(sql: &CStr, param_count: c_int) -> *mut PGresult { /// may have changed since `described_at` (a `generation()` value). pub(crate) fn store(sql: &CStr, param_count: c_int, descriptor: *const PGresult, described_at: u64) { let bytes = sql.to_bytes(); - if bytes.len() > MAX_SQL_BYTES || DDL_PENDING.load(Ordering::SeqCst) { + if bytes.len() > MAX_SQL_BYTES { return; } with_cache(|cache| { // Checked under the lock: `clear` bumps the generation under it too. if GENERATION.load(Ordering::SeqCst) != described_at + || !cache.ddl_sessions.is_empty() || cache.temporary_tables.iter().any(|name| contains_word(bytes, name)) { return; @@ -143,10 +147,8 @@ pub(crate) fn clear() { }); } -/// A CREATE/ALTER/DROP is about to run or has run. Stops caching until the -/// end of the next transaction, which clears again. +/// A CREATE/ALTER/DROP is about to run: drop what was cached so far. pub(crate) fn note_ddl(sql: &[u8]) { - DDL_PENDING.store(true, Ordering::SeqCst); let temporary = temporary_table_name(sql); with_cache(|cache| { GENERATION.fetch_add(1, Ordering::SeqCst); @@ -159,11 +161,29 @@ pub(crate) fn note_ddl(sql: &[u8]) { }); } -/// A transaction ended (COMMIT, ROLLBACK, RELEASE, ROLLBACK TO). -pub(crate) fn transaction_ended() { - if DDL_PENDING.swap(false, Ordering::SeqCst) { - clear(); - } +/// DDL ran on `session`. Until that session is idle again (its transaction +/// committed or rolled back), other sessions may still see the old schema +/// and this one the new, so nothing is cached. +pub(crate) fn ddl_executed(session: usize) { + with_cache(|cache| { + GENERATION.fetch_add(1, Ordering::SeqCst); + clear_entries(cache); + if !cache.ddl_sessions.contains(&session) { + cache.ddl_sessions.push(session); + } + }); +} + +/// `session` has no open transaction (or is gone). Ends its DDL window, if +/// any; the schema others see may just have changed, so drop the cache. +pub(crate) fn connection_idle(session: usize) { + with_cache(|cache| { + if let Some(position) = cache.ddl_sessions.iter().position(|&s| s == session) { + cache.ddl_sessions.swap_remove(position); + GENERATION.fetch_add(1, Ordering::SeqCst); + clear_entries(cache); + } + }); } fn skip_space_and_comments(sql: &[u8]) -> &[u8] { @@ -256,6 +276,20 @@ mod tests { assert!(!starts_with_ddl(b"")); } + fn ddl_window_open_for(session: usize) -> bool { + with_cache(|cache| cache.ddl_sessions.contains(&session)) + } + + #[test] + fn ddl_window_is_closed_only_by_its_own_session() { + let (ddl, other) = (0xD01usize, 0xD02usize); + ddl_executed(ddl); + connection_idle(other); + assert!(ddl_window_open_for(ddl), "another session closed the DDL window"); + connection_idle(ddl); + assert!(!ddl_window_open_for(ddl)); + } + #[test] fn exec_batches_with_ddl_are_detected() { assert!(batch_contains_ddl(b"BEGIN; ALTER TABLE t ADD c int; COMMIT")); diff --git a/rust/plex-pg-core/src/pg_client/connection_lifecycle.rs b/rust/plex-pg-core/src/pg_client/connection_lifecycle.rs index fac6080c..fe6c59b4 100644 --- a/rust/plex-pg-core/src/pg_client/connection_lifecycle.rs +++ b/rust/plex-pg-core/src/pg_client/connection_lifecycle.rs @@ -164,6 +164,7 @@ pub(super) fn destroy_pool_connection(conn: *mut c_void) { } pool().forget_live_pool_connection(conn_ptr as *const c_void); rust_stmt_cache_drop(conn_ptr as *mut c_void); + super::transaction::forget_connection(conn_ptr); // SAFETY: conn_ptr is non-null after the check above. let conn = unsafe { &mut *conn_ptr }; if !conn.conn.is_null() { @@ -193,6 +194,8 @@ pub(super) fn reset_conn(conn: *mut c_void) -> bool { return false; } rust_pq_reset(conn.conn); + // Whatever transaction the old session had is gone with it. + crate::descriptor_cache::connection_idle(conn_ptr as usize); if rust_pq_status(conn.conn) != CONNECTION_OK { return false; } @@ -273,6 +276,9 @@ pub(super) fn close_handle_connection(conn_ptr: *mut PgConnection) { } rust_stmt_cache_clear(conn_ptr as *mut c_void); rust_stmt_cache_drop(conn_ptr as *mut c_void); + // A later connection may be allocated at this address; it must not + // inherit this one's transaction state. + super::transaction::forget_connection(conn_ptr); // SAFETY: conn_ptr is non-null after the check above. let conn = unsafe { &mut *conn_ptr }; let _conn_guard = unsafe { PthreadMutexGuard::lock(&mut conn.mutex as *mut _) }; diff --git a/rust/plex-pg-core/src/pg_client/transaction.rs b/rust/plex-pg-core/src/pg_client/transaction.rs index 3d441056..1713a296 100644 --- a/rust/plex-pg-core/src/pg_client/transaction.rs +++ b/rust/plex-pg-core/src/pg_client/transaction.rs @@ -11,6 +11,9 @@ use crate::libpq_helpers::*; struct TransactionState { active: bool, implicit: bool, + /// The session failed while COMMIT was in flight: the server may or may + /// not have committed. + commit_outcome_unknown: bool, session: usize, next_savepoint: u64, savepoints: Vec<(String, String)>, @@ -32,7 +35,35 @@ fn state_for(conn: *mut PgConnection) -> Arc> { .clone() } +/// A transaction is tracked but can no longer be on this session: the session +/// is unusable, was replaced, or is idle. +/// Caller holds the connection mutex. +unsafe fn session_lost_locked(conn: *mut PgConnection) -> bool { + let state = state_for(conn); + let state = state.lock().unwrap_or_else(|poison| poison.into_inner()); + state.active + && ((*conn).conn.is_null() + || rust_pq_status((*conn).conn) != super::session::CONNECTION_OK + || state.session != (*conn).conn as usize + || rust_pq_transaction_status((*conn).conn) == 0) +} + +/// Returns whether a COMMIT was in flight when the session was lost. +fn forget_lost_transaction(conn: *mut PgConnection) -> bool { + let state = state_for(conn); + let mut state = state.lock().unwrap_or_else(|poison| poison.into_inner()); + let unknown = state.commit_outcome_unknown; + *state = TransactionState::default(); + crate::descriptor_cache::connection_idle(conn as usize); + unsafe { + (*conn).last_error_code = 0; + (*conn).last_error.fill(0); + } + unknown +} + pub(crate) fn forget_connection(conn: *mut PgConnection) { + crate::descriptor_cache::connection_idle(conn as usize); states() .lock() .unwrap_or_else(|poison| poison.into_inner()) @@ -114,6 +145,7 @@ pub(crate) fn ensure_handle_session(conn: *mut PgConnection) { if !(*conn).conn.is_null() { if rust_pq_status((*conn).conn) != super::session::CONNECTION_OK { rust_pq_reset((*conn).conn); + crate::descriptor_cache::connection_idle(conn as usize); if rust_pq_status((*conn).conn) == super::session::CONNECTION_OK { super::session::pg_set_socket_timeout((*conn).conn); super::session::apply_session_settings((*conn).conn, &config.schema, false); @@ -232,6 +264,12 @@ fn tokenize(sql: &str) -> Result, &'static str> { Ok(tokens) } +/// A plain ROLLBACK (not ROLLBACK TO): allowed through the session check so +/// it can clear a transaction whose session was lost. +pub(crate) fn is_rollback_sql(sql: &str) -> bool { + is_transaction_sql(sql) && matches!(parse(sql), Ok(Control::Rollback)) +} + pub(crate) fn is_transaction_sql(sql: &str) -> bool { let sql = crate::pg_config::strip_leading_ws_and_sql_comments(sql); let keyword = sql @@ -347,6 +385,24 @@ pub(crate) unsafe fn set_error(conn: *mut PgConnection, message: &str) -> c_int 1 } +/// Run COMMIT/ROLLBACK; a COMMIT that fails because the session broke has +/// an unknown outcome, which a later ROLLBACK must report. +unsafe fn commit_or_mark_unknown( + conn: *mut PgConnection, + state: &mut TransactionState, + committing: bool, + sql: &str, +) -> Result<(), c_int> { + let outcome = command(conn, sql); + if outcome.is_err() + && committing + && ((*conn).conn.is_null() || rust_pq_status((*conn).conn) != super::session::CONNECTION_OK) + { + state.commit_outcome_unknown = true; + } + outcome +} + unsafe fn command(conn: *mut PgConnection, sql: &str) -> Result<(), c_int> { let sql = CString::new(sql).map_err(|_| set_error(conn, "Invalid transaction SQL"))?; let result = rust_pq_exec((*conn).conn, sql.as_ptr()); @@ -379,6 +435,20 @@ pub(crate) fn execute_transaction(conn: *mut PgConnection, sql: &str) -> Option< unsafe fn execute(conn: *mut PgConnection, sql: &str) -> Result { let _guard = PthreadMutexGuard::lock(&mut (*conn).mutex); let control = parse(sql).map_err(|message| set_error(conn, message))?; + if matches!(control, Control::Rollback) && session_lost_locked(conn) { + // The server discarded the transaction with the session, so the + // rollback has already happened; forget it instead of refusing + // forever (nothing else clears a transaction whose session is gone). + // Unless a COMMIT was in flight: then it may have committed. + let unknown = forget_lost_transaction(conn); + if unknown { + return Err(set_error( + conn, + "PostgreSQL session was lost during COMMIT; whether it committed is unknown", + )); + } + return Ok(0); + } if (*conn).conn.is_null() || rust_pq_status((*conn).conn) != super::session::CONNECTION_OK { return Err(set_error( conn, @@ -435,14 +505,8 @@ unsafe fn execute(conn: *mut PgConnection, sql: &str) -> Result { "Cannot commit an aborted PostgreSQL transaction; rollback required", )); } - command( - conn, - if matches!(control, Control::Commit) { - "COMMIT" - } else { - "ROLLBACK" - }, - )?; + let committing = matches!(control, Control::Commit); + commit_or_mark_unknown(conn, &mut state, committing, if committing { "COMMIT" } else { "ROLLBACK" })?; *state = TransactionState::default(); } Control::Savepoint(name) => { @@ -474,7 +538,7 @@ unsafe fn execute(conn: *mut PgConnection, sql: &str) -> Result { "Cannot release an aborted transaction; rollback required", )); } - command(conn, "COMMIT")?; + commit_or_mark_unknown(conn, &mut state, true, "COMMIT")?; *state = TransactionState::default(); } else { command( @@ -492,8 +556,8 @@ unsafe fn execute(conn: *mut PgConnection, sql: &str) -> Result { } } } - if ends_transaction { - crate::descriptor_cache::transaction_ended(); + if ends_transaction && rust_pq_transaction_status((*conn).conn) == 0 { + crate::descriptor_cache::connection_idle(conn as usize); } (*conn).last_error_code = 0; (*conn).last_error.fill(0);