Repository navigation
[FIX] Postgres: Prevent transaction/connection leaks and protect UID/ModSeq invariants - #3237
prosgarz35 wants to merge 3 commits into
Conversation
chibenwa
left a comment
There was a problem hiding this comment.
Mono.usingWhen already applies task cleanup in case of error or cancelation.
This commit is thus useless.
| .flatMap(Result::getRowsUpdated) | ||
| .then(), | ||
| Connection::close, | ||
| (connection, error) -> connection.close(), |
There was a problem hiding this comment.
Likely log the error as this is a terminal exception, here it is lost
| .flatMap(table -> createAndAlterTable(table, dsl, connection)))) | ||
| .then(), | ||
| connection -> postgresExecutor.connectionFactory().closeConnection(connection), | ||
| (connection, error) -> postgresExecutor.connectionFactory().closeConnection(connection), |
There was a problem hiding this comment.
Likely log the error as this is a terminal exception, here it is lost
| .map(r -> r.get(0, String.class)) | ||
| .collectList(), | ||
| connection -> postgresExecutor.connectionFactory().closeConnection(connection), | ||
| (connection, error) -> postgresExecutor.connectionFactory().closeConnection(connection), |
There was a problem hiding this comment.
Likely log the error as this is a terminal exception, here it is lost
| .doOnError(e -> LOGGER.error("Error while truncating table {}", table.getName(), e))) | ||
| .then()), | ||
| connection -> postgresExecutor.connectionFactory().closeConnection(connection), | ||
| (connection, error) -> postgresExecutor.connectionFactory().closeConnection(connection), |
There was a problem hiding this comment.
Likely log the error as this is a terminal exception, here it is lost
| .flatMap(index -> createTableIndex(index, dsl)))) | ||
| .then(), | ||
| connection -> postgresExecutor.connectionFactory().closeConnection(connection), | ||
| (connection, error) -> postgresExecutor.connectionFactory().closeConnection(connection), |
There was a problem hiding this comment.
Likely log the error as this is a terminal exception, here it is lost
| private <T> Mono<T> handleTransactionTimeout(Connection connection, TimeoutException timeoutException) { | ||
| LOGGER.error(JOOQ_TIMEOUT_ERROR_LOG, timeoutException); | ||
| return cancelRunningQuery(connection) | ||
| .then(Mono.from(connection.rollbackTransaction()) | ||
| .onErrorResume(e -> { | ||
| LOGGER.warn("Failed to rollback the timed out Postgres transaction", e); | ||
| return Mono.empty(); | ||
| })) | ||
| .then(Mono.error(timeoutException)); | ||
| } |
There was a problem hiding this comment.
A coding agent did output to me that r2dbc already does a transaction cleanup.
Do we have a test reproducing the issue ?
| private Mono<Void> setNewUidAndModSeq(MailboxMessage mailboxMessage) { | ||
| return mailboxDAO.incrementAndGetLastUidAndModSeq(mailboxMessage.getMailboxId()) | ||
| .defaultIfEmpty(Pair.of(MessageUid.MIN_VALUE, ModSeq.first())) | ||
| .switchIfEmpty(Mono.error(new MailboxNotFoundException(mailboxMessage.getMailboxId()))) |
There was a problem hiding this comment.
As far as I am aware of, we cannot distinguish "new mailbox with not yet a UID" from "mailbox that do not exist yet"
Taken in isolation, this code path could look like a minor bug. But without an existing mailbox this get never called.
I propose we actually drop it.
| .defaultIfEmpty(ModSeq.first()); | ||
| .switchIfEmpty(Mono.error(new MailboxNotFoundException(mailboxId))); |
|
Hello @prosgarz35 |
Description
This PR provides surgical fixes for critical operational issues in the PostgreSQL backend stack (
backends-common/postgresandmailbox/postgres):1. Rollback transaction on timeout (
PostgresExecutor.java)jooqReactiveTimeout) was triggered insideexecuteTransaction(...),handleTimeout(connection, e)cancelled the running query viacancelRunningQuery(connection), but omitted rolling back the uncommitted transaction. As a result, the connection remained in an open/in-progress transaction state (idle in transaction), locking table rows and holding connections from the R2DBC pool until eviction.handleTransactionTimeout(connection, timeoutException)which cancels the running query and explicitly rolls back the transaction before propagating theTimeoutException.2. Null-safe check in
preparedStatementConflictException(PostgresExecutor.java)preparedStatementConflictException(), checkingthrowable.getMessage().contains(...)without verifyinggetMessage() != nullcould throw an unexpectedNullPointerExceptionduring retry filtering if the root cause exception lacked a message.throwable.getMessage() != null.3. Ensure connection release on error/cancel in schema management (
PostgresTableManager.java)Mono.usingWhencalls in schema initialization (initializePostgresExtension,initializeTables,listExistTables,truncate,initializeTableIndexes) only registered the normal completion release callback (asyncCleanup). In the event of an error or downstream cancellation during table or index creation, the acquired R2DBC connections were not properly closed/returned to the pool.onErrorandonCancelcallbacks to guarantee connection closure across all execution paths.4. Protect UID and ModSeq monotonic invariants (
PostgresUidProvider,PostgresModSeqProvider,PostgresMessageMapper)incrementAndGetLastUidandincrementAndGetModSeqused.defaultIfEmpty(MessageUid.MIN_VALUE)and.defaultIfEmpty(ModSeq.first()). If a mailbox was concurrently deleted or not found, the operation silently fell back to UID1or ModSeq1. This broke RFC 3501 strict monotonic ordering guarantees and could corrupt client sync state..defaultIfEmpty(...)fallbacks with.switchIfEmpty(Mono.error(new MailboxNotFoundException(...)))acrossPostgresUidProvider,PostgresModSeqProvider, andPostgresMessageMapper.setNewUidAndModSeq.Testing
backends-common/postgresmailbox/postgresserver/apps/postgres-app