Skip to content

Suppress ExecutionContext while creating timers#6106

Merged
vonzshik merged 1 commit intomainfrom
6105-suppress-execution-context-for-timers
Oct 1, 2025
Merged

Suppress ExecutionContext while creating timers#6106
vonzshik merged 1 commit intomainfrom
6105-suppress-execution-context-for-timers

Conversation

@vonzshik
Copy link
Copy Markdown
Contributor

@vonzshik vonzshik commented May 5, 2025

Fixes #6105

@vonzshik vonzshik requested review from Brar and roji as code owners May 5, 2025 13:49
// Create the timer, but don't start it; the manual run below will will schedule the first refresh.
_periodicPasswordProviderTimer = new Timer(state => _ = RefreshPassword(), null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
// Create the timer, but don't start it; the manual run below will schedule the first refresh.
using (ExecutionContext.SuppressFlow()) // Don't capture the current ExecutionContext and its AsyncLocals onto the timer causing them to live forever
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to extend that so the first refresh attempt behaves the exact same way as the later ones.


_sendFeedbackTimer = new Timer(TimerSendFeedback, state: null, WalReceiverStatusInterval, Timeout.InfiniteTimeSpan);
_requestFeedbackTimer = new Timer(TimerRequestFeedback, state: null, _requestFeedbackInterval, Timeout.InfiniteTimeSpan);
using (ExecutionContext.SuppressFlow()) // Don't capture the current ExecutionContext and its AsyncLocals onto the timer causing them to live forever
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, this shouldn't really be necessary since this is IAsyncEnumerator and timers will be disposed whenever the enumerator is, but might as well do this for consistency.

@NinoFloris
Copy link
Copy Markdown
Member

NinoFloris commented May 5, 2025

It actually needs to look like this for Timer dotnet/runtime#24770 (comment) (suppress throws if it's already suppressed, they're extremely ugly apis)

Regardless, it's unfortunate UnsafeCreate never made it into the BCL, might be worth re-opening because all the other linked issues didn't pan out either.

@vonzshik
Copy link
Copy Markdown
Contributor Author

vonzshik commented May 5, 2025

It actually needs to look like this for Timer dotnet/runtime#24770 (comment) (suppress throws if it's already suppressed, they're extremely ugly apis)

Hmm, I never knew about this!

I actually copy-pasted this from the runtime, so I guess it's also a bug there?

@NinoFloris
Copy link
Copy Markdown
Member

Yep :)

@vonzshik
Copy link
Copy Markdown
Contributor Author

vonzshik commented May 5, 2025

Though looking through the code, didn't dotnet/runtime#82912 fix this?

@NinoFloris
Copy link
Copy Markdown
Member

Huh and I didn't know about that one 🤣 OK then the xmldoc is just outdated.

@vonzshik vonzshik force-pushed the 6105-suppress-execution-context-for-timers branch from e9bad6c to 5255655 Compare September 10, 2025 11:39
@roji
Copy link
Copy Markdown
Member

roji commented Oct 1, 2025

@vonzshik I think this can be merged without additional reviewing?

@vonzshik
Copy link
Copy Markdown
Contributor Author

vonzshik commented Oct 1, 2025

@vonzshik I think this can be merged without additional reviewing?

Probably? Mostly, I just wanted to keep you in the loop since we do to some extent change how _periodicPasswordProviderTimer on dataSource works. I doubt someone accesses UI from it, but just in case...

@vonzshik vonzshik merged commit ee97842 into main Oct 1, 2025
16 checks passed
@vonzshik vonzshik deleted the 6105-suppress-execution-context-for-timers branch October 1, 2025 14:51
@roji
Copy link
Copy Markdown
Member

roji commented Oct 1, 2025

OK, understood. BTW IIRC we discourage the periodic password provider in any case, we could even consider obsoleting it at some point.

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.

Internal timers shouldn't capture external ExecutionContext

3 participants