fix(#38): break Transaction<->Snapshot reference cycle - #78
Open
s2x wants to merge 2 commits into
Open
Conversation
Transaction::snapshot() cached the Snapshot on the parent transaction, closing a reference cycle (Transaction -> snapshotInstance -> Snapshot -> parentTransaction) that kept both objects alive by refcount and deferred fdb_transaction_destroy() to the cycle collector - or to process shutdown when zend.enable_gc=0. Long-running workers calling readTransact(), directory operations (HighContentionAllocator) or Locality accumulated undestroyed native transaction handles. snapshot() now returns a fresh Snapshot per call. The Snapshot still anchors its parent one-directionally, so the shared native handle stays valid for the snapshot's lifetime and both are freed deterministically on scope exit. - Unit: tests/Unit/SnapshotLifetimeTest.php (native-free, reflection- built; asserts fresh instance per call, anchor semantics, and refcount-only destruction without gc_collect_cycles) - Integration: tests/Integration/TransactionLifecycleTest.php (weak-ref loop over createTransaction+snapshot; directory creation leaves no cyclic garbage). Both fail against the previous implementation. - Docs: snapshot lifetime/ownership notes in docs/transactions.md
5 tasks
Readonly properties may only be reflection-initialized from their declaring class scope; on PHP < 8.4 a subclass scope (Transaction for props declared on ReadTransaction) is rejected. Resolve the ReflectionProperty against ReadTransaction::class explicitly.
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.
Closes #38
Problem
Transaction::snapshot()cached theSnapshotinstance on the parent transaction:Transaction -> $snapshotInstance -> Snapshot -> $parentTransaction -> TransactionBoth sides held strong references, so refcounts never reached zero on scope exit and
Transaction::__destruct()(i.e.fdb_transaction_destroy()) only ran via the cyclecollector — or at process shutdown when
zend.enable_gc=0. The cycle was created onevery
snapshot()call:Database::readTransact(), everyHighContentionAllocator::allocate()(i.e. every directory create/open), and
Locality. A long-running worker thereforeaccumulated undestroyed native FDB transaction handles.
Change
snapshot()now returns a freshSnapshotper call (as proposed in the issue).The
Snapshotstill anchors its parent one-directionally, so the shared native handleremains valid for the snapshot's lifetime; once both go out of scope, destruction is
deterministic by refcounting alone. Read-only semantics are unchanged.
Verification
composer lint(PHPCS + Rector + PHPStan L9)composer test:unitNew tests:
tests/Unit/SnapshotLifetimeTest.php— native-free (reflection-built objects, types-only FFI scope): fresh instance per call, snapshot anchors parent, deterministic destruction withoutgc_collect_cycles(), destructor-run observable.tests/Integration/TransactionLifecycleTest.php— weak-ref loop overcreateTransaction()+snapshot()+getReadVersion(); directory creation loop leaves zero cyclic garbage.Docs: lifetime/ownership notes added to
docs/transactions.md. Changelog entry added under[Unreleased] → Fixed.