Fix timeout/duration calculations using monotonic time and precise tick accumulation#123980
Merged
stephentoub merged 4 commits intomainfrom Feb 4, 2026
Merged
Fix timeout/duration calculations using monotonic time and precise tick accumulation#123980stephentoub merged 4 commits intomainfrom
stephentoub merged 4 commits intomainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
…lations Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes incorrect timeout/duration calculations caused by using TimeSpan.Milliseconds (component) instead of TimeSpan.TotalMilliseconds (total).
Changes:
- Fix remaining-timeout decrement in
SocketAsyncContext.Unixto use total elapsed milliseconds. - Fix phoneme duration accumulation in
System.Speechto use total duration milliseconds.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs | Corrects timeout adjustment logic to subtract total elapsed milliseconds. |
| src/libraries/System.Speech/src/Internal/Synthesis/EngineSite.cs | Corrects phoneme duration accumulation to account for durations > 1 second. |
Comments suppressed due to low confidence (2)
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs:1414
- There are existing timeout-related functional tests, but none appear to exercise the multi-iteration path in PerformSyncOperation where the remaining
timeoutis decremented after a wake-up + failed processing. Adding a targeted regression test that forces multiple signals over >1s and asserts the overall operation respects the configured timeout would help prevent this from regressing again.
// Adjust timeout and try again.
if (timeout > 0)
{
timeout -= (int)(DateTime.UtcNow - waitStart).TotalMilliseconds;
src/libraries/System.Speech/src/Internal/Synthesis/EngineSite.cs:489
- This change fixes duration accumulation when phoneme conversion spans more than 1 second. Please consider adding a regression test (via the public SpeechSynthesizer PhonemeReached event path) that produces phoneme events with durations crossing a 1s boundary and verifies the resulting converted phoneme event’s duration matches the sum of the originals.
for (int i = 0; i < _lastComplete;)
{
ttsEvent = (TTSEvent)_phonemeQueue.Dequeue();
totalDuration += (long)ttsEvent.PhonemeDuration.TotalMilliseconds;
i += ttsEvent.Phoneme.Length;
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs
Outdated
Show resolved
Hide resolved
Copilot
AI
changed the title
[WIP] Fix bug in timeout calculation for SocketAsyncContext
Fix .Milliseconds to .TotalMilliseconds in timeout/duration calculations
Feb 4, 2026
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
auto-merge was automatically disabled
February 4, 2026 05:40
Head branch was pushed to by a user without write access
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot
AI
changed the title
Fix .Milliseconds to .TotalMilliseconds in timeout/duration calculations
Fix timeout/duration calculations using monotonic time and precise tick accumulation
Feb 4, 2026
rzikm
approved these changes
Feb 4, 2026
MihaZupan
approved these changes
Feb 4, 2026
stephentoub
approved these changes
Feb 4, 2026
Member
|
/ba-g unrelated wasm failures |
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.
Description
Two instances incorrectly used
.Milliseconds(returns component 0-999) instead of the total duration. For a 2.5 second duration,.Millisecondsreturns500while.TotalMillisecondsreturns2500.0. Additionally, the timeout calculation used non-monotonic time source (DateTime.UtcNow) which could be affected by system clock adjustments.Changes
System.Net.Sockets (SocketAsyncContext.Unix.cs:1391, 1414)
DateTime waitStart = DateTime.UtcNow;andtimeout -= (DateTime.UtcNow - waitStart).Milliseconds;long waitStart = Stopwatch.GetTimestamp();andtimeout -= (int)Stopwatch.GetElapsedTime(waitStart).TotalMilliseconds;Stopwatch) instead ofDateTime.UtcNow, ensuring timeout calculations are not affected by system clock adjustments (e.g., NTP corrections, manual clock changes)System.Speech (EngineSite.cs:483, 488, 493)
long totalDuration = 0;withtotalDuration += ttsEvent.PhonemeDuration.Milliseconds;andTimeSpan.FromMilliseconds(totalDuration)long totalDurationTicks = 0;withtotalDurationTicks += ttsEvent.PhonemeDuration.Ticks;andTimeSpan.FromTicks(totalDurationTicks)Original prompt
This pull request was created from Copilot chat.
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.