Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 11, 2026

Proposed changes

During server shutdown, background tasks (PublishSubscriptionsAsync and ConditionRefreshWorkerAsync) can race with Dispose(), attempting to access disposed SemaphoreSlim and ManualResetEvent objects. This causes ObjectDisposedException errors in logs despite being a benign shutdown sequence.

Solution: Catch ObjectDisposedException in both background tasks and log as normal shutdown instead of error.

catch (ObjectDisposedException)
{
    m_logger.LogInformation(
        "Subscription - Publish Task {TaskId:X8} Exited Normally (disposed during shutdown).",
        Task.CurrentId);
}

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • Enhancement (non-breaking change which adds functionality)
  • Test enhancement (non-breaking change to increase test coverage)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected, requires version increase of Nuget packages)
  • Documentation Update (if none of the other choices apply)

Checklist

  • I have read the CONTRIBUTING doc.
  • I have signed the CLA.
  • I ran tests locally with my changes, all passed.
  • I fixed all failing tests in the CI pipelines.
  • I fixed all introduced issues with CodeQL and LGTM.
  • I have added tests that prove my fix is effective or that my feature works and increased code coverage.
  • I have added necessary documentation (if appropriate).
  • Any dependent changes have been merged and published in downstream modules.

Further comments

No tests added - the race condition is timing-dependent and difficult to reproduce reliably. The fix is defensive: it handles an expected shutdown scenario gracefully rather than modifying coordination logic.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Server] Fix ObjectDisposedException in SubscriptionPublishTask on shutdown</issue_title>
<issue_description>SubscriptionTest SubscriptionTest
SERVER 18:35:14.494 [Opc.Ua.Server.SubscriptionManager] Subscription - Publish Task (null) Exited Unexpectedly.
[ObjectDisposedException] Cannot access a disposed object.
Object name: 'System.Threading.SemaphoreSlim'.
--- at Opc.Ua.Server.SubscriptionManager.PublishSubscriptionsAsync(Int32 sleepCycle, CancellationToken cancellationToken) in /_/Libraries/Opc.Ua.Server/Subscription/SubscriptionManager.cs:line 2076
</issue_description>

<agent_instructions>Please fix the ObjectDisposedException in the SubscriptionManager</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI and others added 2 commits January 11, 2026 18:50
Add ObjectDisposedException handling in PublishSubscriptionsAsync and ConditionRefreshWorkerAsync to gracefully handle shutdown race condition where semaphore/events are disposed while background tasks are still running.

Co-authored-by: romanett <[email protected]>
Added clarification in log messages to indicate when tasks exit due to ObjectDisposedException during shutdown, making it easier to distinguish from normal shutdown in logs.

Co-authored-by: romanett <[email protected]>
Copilot AI changed the title [WIP] Fix ObjectDisposedException in SubscriptionManager during shutdown Fix ObjectDisposedException in SubscriptionManager background tasks on shutdown Jan 11, 2026
Copilot AI requested a review from romanett January 11, 2026 19:00
@romanett romanett marked this pull request as ready for review January 12, 2026 04:49
@marcschier marcschier merged commit 9e192c6 into master Jan 12, 2026
111 of 112 checks passed
@romanett romanett deleted the copilot/fix-objectdisposedexception-subscriptionmanager branch January 12, 2026 07:08
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.

[Server] Fix ObjectDisposedException in SubscriptionPublishTask on shutdown

4 participants