fix(database): add whole-transaction mutex to PhlixMySQLConnection (txn-mutex defect)#333
Merged
Conversation
…xn-mutex defect) Option-A: wrap the entire transaction in a reentrant mutex, not per-query. When beginTrans() is called, acquire the transaction lock. When commitTrans() or rollBackTrans() is called, release it. While inside a transaction (transNesting > 0), query() runs without releasing the per-query lock between queries, preventing concurrent coroutines from interleaving queries inside our transaction on the shared socket. The lock is reentrant per coroutine (transNesting counter), so nested beginTrans() calls (MySQL savepoints) work correctly. Tests: add PhlixMySQLConnectionTest verifying property declarations and method signatures (reflection-only, no socket opened).
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| UnusedCode | 1 medium |
| BestPractice | 2 medium |
| Documentation | 12 minor |
| ErrorProne | 1 medium |
| CodeStyle | 24 minor |
| Complexity | 4 medium |
🟢 Metrics 28 complexity · 0 duplication
Metric Results Complexity 28 Duplication 0
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #333 +/- ##
============================================
+ Coverage 59.93% 60.00% +0.07%
- Complexity 12927 12948 +21
============================================
Files 480 480
Lines 42190 42255 +65
============================================
+ Hits 25286 25357 +71
+ Misses 16904 16898 -6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…kTrans result before mutating transNesting Three atomicity bugs were fixed: 1. beginTrans() reentrant path — transNesting was incremented BEFORE parent::beginTrans() returned, so on parent failure the counter was left inflated and subsequent commitTrans()/rollBackTrans() would operate on a mismatched nesting depth. Now we capture the parent result first, then only increment transNesting on success. 2. beginTrans() non-reentrant path — transNesting = 1 and transLockHolder were set before parent::beginTrans() confirmed the DB transaction started. If the DB call failed, the in-memory state was already updated, leaving the object in an inconsistent state. Now the result is captured first, and state is rolled back on failure (transLockHolder reset, token restored to the channel, channel discarded). 3. commitTrans()/rollBackTrans() nested path — transNesting was decremented BEFORE parent::commitTrans()/rollBackTrans() confirmed success. If the parent call failed, transNesting was left too low, potentially triggering the outermost path prematurely. Now the parent result is captured first, and transNesting is only decremented on success. 4. Concurrent transaction integration test — a Swoole coroutine test was added that runs two concurrent transactions and verifies their query logs are never interleaved, proving the transLock mutex serialises concurrent transaction bodies. Additional changes: - class PhlixMySQLConnection is no longer final (required to subclass it in the test with a SQLite in-memory PDO) - local-variable pattern used in commitTrans/rollBackTrans outermost path to help PHPStan track the null-assignment state changes - phpstan-ignore on one line where PHPStan misinfers that \$this->transLock !== null is always true after beginTrans() failure (the impure parent::beginTrans() result makes the state change invisible to PHPStan without the suppression)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the txn-mutex defect in
PhlixMySQLConnection— the coroutine mutex was held per singlequery()only, so multi-call transactions /SELECT … FOR UPDATEcould interleave on the shared socket.Changes
src/Common/Database/PhlixMySQLConnection.php: Add whole-transaction mutex (Option-A):$transLock(Channel),$transLockHolder,$transNestingbeginTrans(): acquires the transaction lock (reentrant per coroutine viatransNestingcounter)query(): when inside a transaction (transNesting > 0), runs without releasing the per-query lock between queriescommitTrans()/rollBackTrans(): releases the transaction lock when exiting the outermost transaction (nested calls handle savepoints correctly)tests/Unit/Common/Database/PhlixMySQLConnectionTest.php: New test class verifying:beginTrans(),commitTrans(),rollBackTrans(),query()are publicGate
./vendor/bin/phpunit— 3627 tests, 16 skipped./vendor/bin/phpstan analyze src/ --level=9 --no-progress— [OK] No errors./vendor/bin/phpcs --standard=PSR12 -n src/— cleanSuccess conditions
Two concurrent coroutines each starting a multi-statement transaction do NOT interleave their queries on the shared socket.