Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CompositionTarget.Rendering not always can be unsubscribed. #4621

Closed
azchohfi opened this issue Mar 22, 2021 · 4 comments
Closed

CompositionTarget.Rendering not always can be unsubscribed. #4621

azchohfi opened this issue Mar 22, 2021 · 4 comments
Labels
appModel-win32 Exclusive to WinUI 3 Win32 Desktop apps area-C#/WinRT product-winui3 WinUI 3 issues team-Markup Issue for the Markup team

Comments

@azchohfi
Copy link
Contributor

Describe the bug
CompositionTarget.Rendering -= Callback;
is supposed to unassign the method, but sometimes it doesn't.

Steps to reproduce the bug
The WCT uses this pattern in our TokenizingTextBox:

        public static Task<bool> ExecuteAfterCompositionRenderingAsync(Action action, TaskCreationOptions? options = null)
        {
....
            var taskCompletionSource = options.HasValue ? new TaskCompletionSource<bool>(options.Value)
                : new TaskCompletionSource<bool>();

            try
            {
                void Callback(object sender, object args)
                {
                    CompositionTarget.Rendering -= Callback;

                    action();

                    taskCompletionSource.SetResult(true);
                }

                CompositionTarget.Rendering += Callback;
            }
            catch (Exception e)
            {
                taskCompletionSource.SetException(e); // Note this can just sometimes be a wrong thread exception, see WinUI function notes.
            }

            return taskCompletionSource.Task;
....
        }

Opening the WCT sample app (the WinUI3 version) and clicking a few times to add an item from the TTB will eventually reproduce this.
We then get exceptions on taskCompletionSource.SetResult(true), since this is trying to complete the task more than once. I worked around this issue by switching to a TrySetResult, and only if successful we call the callback, but there will be performance and memory problems with this, since that no-op method will still be called at every single time this callback is called (every render...).
This method worked fine on UWP/XAML for a while now, but it intermittently fails to unassign the callback (but after the first time, it keeps calling that callback, even given the fact that we unsubscribe as the very first thing inside the callback).

Expected behavior
CompositionTarget.Rendering -= Callback
actually removed the method from the callback list, never being called again unless resubscribed.

Version Info
Reunion 0.5-Prerelease, but this is not a new regression, as this has been seem in previous previews.

NuGet package version:
[WinUI 3 - Project Reunion 0.5 Preview: 0.5.0-prerelease]

Windows app type:

UWP Win32
Yes
Windows 10 version Saw the problem?
Insider Build Yes
October 2020 Update (19042)
May 2020 Update (19041)
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT
@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Mar 22, 2021
@StephenLPeters
Copy link
Contributor

@azchohfi does this repro on both UWP and Desktop applications?

@StephenLPeters StephenLPeters added needs-author-feedback Asked author to supply more information. and removed needs-triage Issue needs to be triaged by the area owners labels Mar 22, 2021
@azchohfi
Copy link
Contributor Author

I did not test this on UWP, as the previews we are shipping of the WCT support only Desktop.

@ghost ghost added needs-triage Issue needs to be triaged by the area owners and removed needs-author-feedback Asked author to supply more information. labels Mar 22, 2021
@StephenLPeters
Copy link
Contributor

@Scottj1s my guess is this is a cswinrt issue.

@StephenLPeters StephenLPeters added area-C#/WinRT appModel-win32 Exclusive to WinUI 3 Win32 Desktop apps product-winui3 WinUI 3 issues and removed needs-triage Issue needs to be triaged by the area owners labels Mar 23, 2021
@krschau krschau added the team-Markup Issue for the Markup team label Jul 29, 2021
@manodasanW
Copy link
Member

This has been resolved by the recent event handler fixes from CsWinRT which were part of the Windows App SDK 0.8.2 update. Tried out the above repro without the workaround and the issue doesn't seem to repro.

azchohfi added a commit to CommunityToolkit/WindowsCommunityToolkit that referenced this issue Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
appModel-win32 Exclusive to WinUI 3 Win32 Desktop apps area-C#/WinRT product-winui3 WinUI 3 issues team-Markup Issue for the Markup team
Projects
None yet
Development

No branches or pull requests

5 participants