You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Server: claim subscription expiry without the manager semaphore - #5228
Fixes SB-4 from the 2026-11-15 server architecture review: TransferSubscriptions held SubscriptionManager.m_semaphoreSlim across await PrepareSessionTransferAsync, while the publish sweep's expiry path took the same semaphore with a synchronous Wait() inside Parallel.For. One slow transfer could stall the publish threads.
Changes
Per-subscription expiry claim.ISubscriptionPublishPipeline.TryClaimExpiration / ReleaseExpirationClaim / IsExpirationClaimed, decided under Subscription.m_lock and mutually exclusive with TryBeginTransfer.
Queued subscriptions: SessionPublishQueue.TryClaimForExpiration claims and removes the exact queue entry under the queue lock (same lock order as TryClaimForTransfer).
Abandoned subscriptions: claim, remove from the abandoned set, release the claim if removal fails.
The sweep no longer touches m_semaphoreSlim. m_expiringSubscriptions is removed; StoreSubscriptionsAsync checks the claim flag.
Three-phase transfer. (1) validate and reserve under the semaphore, recorded in m_transfersInFlight; (2) PrepareSessionTransferAsync with no lock; (3) commit under the semaphore. On failure RollbackAsync runs outside the semaphore and only the restore bookkeeping runs under it.
Ordering preserved. Closing the source or destination session, deleting the subscription, a second transfer of the same id, and shutdown wait for the in-flight transfer itself (EnterSemaphoreAfterTransfersAsync, which releases the semaphore while waiting) instead of on the semaphore.
Documented invariants and lock order at m_semaphoreSlim: semaphore → SessionPublishQueue.m_lock → Subscription.m_lock → status-messages lock.
Invariants
A subscription is never both expired and transferred.
A subscription is never transferred twice concurrently.
A failed transfer leaves the original owner intact (source entry is only claimed until commit; source session close waits for the transfer).
Behaviour change
None visible to clients. The sweep, other transfers, and create/delete of other subscriptions no longer wait behind a slow transfer.
The (SubscriptionManager) downcast in SessionPublishQueue is left as is (SB-1; the queue needs SubscriptionExpired/CleanupSubscriptions on the manager).
Tests
7 new deterministic (TaskCompletionSource-gated) tests in SubscriptionTests.cs: blocked transfer does not stall the sweep; expiry-first wins (queued and abandoned); transfer-first wins over a stale sweep snapshot; second transfer of the same id waits and gets BadNothingToDo; failed transfer restores the source before the source session closes; claim and reservation exclude each other. The stall test fails against the old code.
Server.Tests net10.0: 12549 passed, 8 failed (pre-existing local symlink-permission tests).
The publish sweep claimed expirations with a synchronous Wait() on the
subscription manager semaphore, from inside Parallel.For, while
TransferSubscriptions held that semaphore across the awaited
PrepareSessionTransferAsync. One slow monitored item transfer stalled
the publish threads.
- Expiry is claimed per subscription (TryClaimExpiration under the
subscription lock, exclusive with the TryBeginTransfer reservation);
a queued subscription is claimed under the queue lock together with
the removal of its exact entry. The sweep no longer takes the manager
semaphore. m_expiringSubscriptions is replaced by the claim flag.
- TransferSubscriptions runs in three phases: reserve under the
semaphore, prepare without it, commit or roll back under it.
Transfers in flight are registered; closing either session, deleting
the subscription, a second transfer of it and shutdown wait for them,
keeping the ordering they had while the transfer held the semaphore.
- Lock order and invariants are documented at m_semaphoreSlim.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
romanett
changed the title
Server: claim subscription expiry without the manager semaphore (SB-4)
Server: claim subscription expiry without the manager semaphore
Dec 1, 2026
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The same-session transfer shortcut bypasses the expiry check, returning BadNothingToDo instead of BadSubscriptionIdInvalid for an expired subscription awaiting cleanup.
This PR removes subscription-expiry processing’s dependency on the manager semaphore, allowing publish sweeps and unrelated requests to proceed while a transfer is waiting.
Changes:
Adds per-subscription expiry claims that exclude concurrent transfer reservations.
Splits transfers into reserve, prepare, and commit phases, with explicit waiting for conflicting operations.
Documents locking invariants and adds seven deterministic concurrency tests.
File
Description
tests/Opc.Ua.Server.Tests/SubscriptionTests.cs
Tests blocked transfers, expiry ordering, rollback, and competing reservations.
The same-session BadNothingToDo shortcut ran before TryBeginTransfer saw
the expiry claim, so the owner got BadNothingToDo for a subscription that
was already expiring. Treat an expiry-claimed subscription as gone for
every caller, and cover the same-session request in the expiry-first test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
❌ Patch coverage is 79.76879% with 35 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.86%. Comparing base (aa5f140) to head (f92670b). ⚠️ Report is 7 commits behind head on master.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The documented lock hierarchy reverses the implemented order of the status-messages and subscription locks, which could lead future changes to introduce a deadlock.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Correct the documented status-message and subscription lock order
The last two locks are documented in the opposite order to the implementation: SubscriptionExpired holds m_statusMessagesLock while calling PublishTimeout, which acquires Subscription.m_lock; the transferred-status path does the same through SubscriptionTransferred. Following the hierarchy documented here in future changes could therefore introduce a deadlock. Please place the status-messages lock before the subscription lock in this hierarchy.
SubscriptionExpired and the transferred status take Subscription.m_lock
under m_statusMessagesLock; document that order instead of the reverse.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The transfer releases its reservation before initializing and filtering the destination status queue, creating an expiry race that can lose BadTimeout notifications and needs a gated regression test.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Initialize the destination status queue before releasing the reservation
CompleteTransfer releases the reservation before the destination status queue is initialized and filtered. If the subscription is due to expire at this point, the semaphore-free sweep can enqueue BadTimeout that the filter then removes, or drop it because the status queue does not exist yet. Move the status-queue initialization/filtering block before CompleteTransfer, and add a gated regression test for expiry during commit.
…fer reservation
CompleteTransfer released the reservation before the destination status
queue was created and filtered, so a sweep-side expiry in that window
could drop its BadTimeout (no queue yet) or have it filtered away. Build
the queue first; the reservation keeps the sweep out until it exists.
Adds a test that holds the status-messages lock to stop the commit there.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Filtering the destination status queue before the fallible monitored-item commit can permanently delete pending transfer notifications when the commit fails, because rollback does not restore them.
A failing CommitMonitoredItemEffects rolled the transfer back after the
destination status queue had already dropped the subscription's pending
messages (e.g. GoodSubscriptionTransferred from an earlier transfer).
Run the fallible commit first; the reservation is still held until the
filtered queue exists.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The three-phase transfer and semaphore-free expiry changes alter ordering across callbacks, session closure, deletion, and shutdown, warranting human concurrency review with gated and stress tests not independently executed.
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
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.
Fixes SB-4 from the 2026-11-15 server architecture review:
TransferSubscriptionsheldSubscriptionManager.m_semaphoreSlimacrossawait PrepareSessionTransferAsync, while the publish sweep's expiry path took the same semaphore with a synchronousWait()insideParallel.For. One slow transfer could stall the publish threads.Changes
ISubscriptionPublishPipeline.TryClaimExpiration/ReleaseExpirationClaim/IsExpirationClaimed, decided underSubscription.m_lockand mutually exclusive withTryBeginTransfer.SessionPublishQueue.TryClaimForExpirationclaims and removes the exact queue entry under the queue lock (same lock order asTryClaimForTransfer).m_semaphoreSlim.m_expiringSubscriptionsis removed;StoreSubscriptionsAsyncchecks the claim flag.m_transfersInFlight; (2)PrepareSessionTransferAsyncwith no lock; (3) commit under the semaphore. On failureRollbackAsyncruns outside the semaphore and only the restore bookkeeping runs under it.EnterSemaphoreAfterTransfersAsync, which releases the semaphore while waiting) instead of on the semaphore.m_semaphoreSlim: semaphore →SessionPublishQueue.m_lock→Subscription.m_lock→ status-messages lock.Invariants
Behaviour change
None visible to clients. The sweep, other transfers, and create/delete of other subscriptions no longer wait behind a slow transfer.
The
(SubscriptionManager)downcast inSessionPublishQueueis left as is (SB-1; the queue needsSubscriptionExpired/CleanupSubscriptionson the manager).Tests
SubscriptionTests.cs: blocked transfer does not stall the sweep; expiry-first wins (queued and abandoned); transfer-first wins over a stale sweep snapshot; second transfer of the same id waits and getsBadNothingToDo; failed transfer restores the source before the source session closes; claim and reservation exclude each other. The stall test fails against the old code.🤖 Generated with Claude Code