Skip to content

Refuse a transaction left open across within() on PostgreSQL, add transaction() (0.3.0) - #10

Merged
rasuvaeff merged 2 commits into
masterfrom
fix/pg-rollback-leak
Nov 21, 2026
Merged

rasuvaeff merged 2 commits into
masterfrom
fix/pg-rollback-leak

Conversation

@rasuvaeff

@rasuvaeff rasuvaeff commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Summary

PostgreSQL has no non-transactional SET (SET, set_config(..., false) and RESET are all undone by a rollback of the transaction they ran in). When within() was entered outside a transaction and the callback left one open, the restore ran inside it and the caller's later rollback undid it: the session kept the budget timeout.

  • Added StatementTimeout::transaction($context, $callback): opens a transaction through PDO, runs the callback under the budget, commits, or rolls back and rethrows. On PostgreSQL the limit is SET LOCAL, which ends with the transaction on commit and rollback alike (also after a timeout aborted it), so nothing can be left on the session. A transaction already open is refused with LogicException.
  • Changed (breaking, 0.x minor): on PostgreSQL, within()/run() entered outside a transaction throws LogicException when the callback returns with a transaction open, instead of silently leaking. A callback that throws keeps its own error (documented: the restore may still be lost there).
  • MySQL/MariaDB checked on live servers: session variables are not transactional, the restore survives a later rollback; a test pins it.

Tests

  • Integration (live postgres:17, mysql:8.4, mariadb:11.8): postgresqlRefusesATransactionLeftOpenAcrossWithin and transactionLeavesNoLimitBehind were red before the fix, green after; mysqlAndMariaDbKeepTheRestoreThroughALaterRollback green on both.
  • Unit: refusal paths, transaction() commit/rollback/terminated context/already-open.
  • Local mutation (--with-uncovered, zend.assertions=-1): MSI 92.8% (minMsi 88).

Docs: README.md + README.ru.md, llms.txt, agent skill, AGENTS.md, CHANGELOG ## 0.3.0 — 2026-11-21.

Fixes #6

Summary by Sourcery

Add owned transaction execution and prevent PostgreSQL timeout settings from leaking when callbacks leave transactions open.

New Features:

  • Add StatementTimeout::transaction() to run budgeted callbacks in an owned transaction with automatic commit or rollback.

Bug Fixes:

  • Prevent PostgreSQL statement-timeout settings from being silently left on a session when within() callbacks leave transactions open.
  • Preserve callback errors while handling transaction cleanup failures.

Enhancements:

  • Reject PostgreSQL within() and run() callbacks that return with a transaction opened outside the callback's scope.
  • Document safe transaction usage and PostgreSQL, MySQL, and MariaDB behavior.

Documentation:

  • Update English and Russian README documentation, project guidance, skills, and changelog for the new transaction API and PostgreSQL safeguards.

Tests:

  • Add unit and live database integration coverage for transaction ownership, rollback and timeout cleanup, open-transaction refusal, and cross-dialect restore behavior.

…nsaction()

PostgreSQL has no non-transactional SET: a restore that ran inside a
transaction the within() callback left open was undone by the caller's
rollback, and the session kept the budget timeout. within() now throws
LogicException in that case; transaction() owns the transaction and uses
SET LOCAL, which ends with it on commit and rollback alike. Live tests pin
the refusal, transaction() on all three servers, and that MySQL/MariaDB
session variables survive a later rollback.

Fixes #6
@sourcery-ai

sourcery-ai Bot commented Nov 21, 2026

Copy link
Copy Markdown

Sorry @rasuvaeff, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 5 days and 15 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f7635424-3b5e-4bbf-9e99-1d406ba7b967
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@sourcery-ai

sourcery-ai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

API

Document and expose the PostgreSQL within() refusal, including its behavior when no deadline is set.

  • src/StatementTimeout.php L98-101 (+3 small edits)

Add the public transaction() API and document its transaction ownership, commit, rollback, and dialect behavior.

  • src/StatementTimeout.php L142-188

Implementation

Implement PostgreSQL timeout scoping and reject callbacks that return with a transaction open when within() began outside one.

  • src/StatementTimeout.php L235-287

Documentation

Update the project guidance, changelog, English and Russian READMEs, and skill documentation to explain the new API and transaction-safety rules.

  • AGENTS.md L86-93
  • CHANGELOG.md L8-21
  • README.md L53-67, L122-150 (+3 small edits)
  • README.ru.md L56-69, L125-152 (+3 small edits)
  • llms.txt L14, L26-34, L62-66
  • resources/skills/rasuvaeff-context-db/SKILL.md L7-8, L35-42

Tests

Add unit and live-server coverage for refusing PostgreSQL callbacks that leave transactions open, transaction ownership and cleanup, and MySQL/MariaDB restoration behavior.

  • tests/Integration/ServerTimeoutIntegrationTest.php L142-147, L158-230 (+2 small edits)
  • tests/StatementTimeoutTest.php L262-447

Extend the PDO test double to model transaction lifecycle operations and rollback failures.

  • tests/Support/FakePdo.php L26, L70-99

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@rasuvaeff
rasuvaeff merged commit 0b93a81 into master Nov 21, 2026
15 checks passed
@rasuvaeff
rasuvaeff deleted the fix/pg-rollback-leak branch November 21, 2026 05:12
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.

PostgreSQL: the budget timeout can stay on the session when the callback's transaction is rolled back

1 participant