Skip to content

[Server] Use HiResClock for time interval calculations to avoid system clock sensitivity#3594

Open
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-time-calculation-bug
Open

[Server] Use HiResClock for time interval calculations to avoid system clock sensitivity#3594
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-time-calculation-bug

Conversation

Copy link
Contributor

Copilot AI commented Mar 6, 2026

Time interval calculations using DateTime.UtcNow for session timeouts, sampling intervals, and diagnostics scan throttling are susceptible to erroneous behavior when the system clock is adjusted. Replaces these with the monotonic HiResClock.

Proposed changes

Session timeout tracking — most critical fix:

  • Added m_lastContactTickCount (HiResClock.TickCount64, ms) to Session, updated alongside ClientLastContactTime on every client contact
  • HasExpired now compares monotonic tick deltas instead of wall-clock arithmetic:
    // Before
    return SessionDiagnostics.ClientLastContactTime.AddMilliseconds(
        SessionDiagnostics.ActualSessionTimeout) < DateTime.UtcNow;
    
    // After
    return HiResClock.TickCount64 - m_lastContactTickCount >
        (long)SessionDiagnostics.ActualSessionTimeout;
  • ISession gains LastContactTickCount property; SessionManager keep-alive check uses it

Sampling loop (SamplingGroup):

  • Elapsed time calculation (startdelay) uses HiResClock.UtcNow instead of DateTime.UtcNow

Subscription publish loop (SubscriptionManager):

  • Start-time tracking uses HiResClock.UtcNow

Diagnostics scan rate-limiter (DiagnosticsNodeManager):

  • m_lastDiagnosticsScanTime stored and compared using HiResClock.UtcNow

ClientLastContactTime in SessionDiagnosticsDataType intentionally retains DateTime.UtcNow — it is an OPC UA protocol-visible wall-clock value.

Related Issues

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

The ISession interface gains LastContactTickCount — this is additive and non-breaking for external implementors since it exposes a value that was previously only internal state.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Server] Time calculations using DateTime are susceptible to produce erroneous values when System Time is changed during the calculation process.</issue_title>
<issue_description>### Type of issue

  • Bug
  • Enhancement
  • Compliance
  • Question
  • Help wanted

Current Behavior

Computing time intervals using DateTime are susceptible to produce erroneous values when System Time is changed during the calculation process.
The HiResClock should be used instead in such places since it is monotonic.

Expected Behavior

Computing time should be done with a monotonic clock such as existing HiResClock.

Steps To Reproduce

No response

Environment

- OS:
- Environment:
- Runtime:
- Nuget Version:
- Component:
- Server:
- Client:

Anything else?

No response</issue_description>

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


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

@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.

…UtcNow

Co-authored-by: romanett <7413710+romanett@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix time calculations using HiResClock for accuracy [Server] Use HiResClock for time interval calculations to avoid system clock sensitivity Mar 6, 2026
@romanett romanett requested a review from mrsuciu March 6, 2026 15:13
@romanett romanett marked this pull request as ready for review March 6, 2026 15:38
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] Time calculations using DateTime are susceptible to produce erroneous values when System Time is changed during the calculation process.

3 participants