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

AdvancedImage.UpdateImage can error with "CancellationTokenSource has been disposed" when redrawn rapidly in virtualizing panels #19

Closed
ionite34 opened this issue Sep 23, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@ionite34
Copy link
Contributor

In a virtualizing ItemsRepeater, when scrolling back and forth rapidly and causing redraws, the AdvancedImage.UpdateImage method can error with ObjectDisposedException likely due to the previous call not finishing before the new call, perhaps a lock mechanism is needed?

Relevant lines:
https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/blob/830e2bfd0ee30e85f81821d8636e4ed59a1084fd/AsyncImageLoader.Avalonia/AdvancedImage.axaml.cs#L189C3-L189C10

@SKProCH SKProCH added the bug Something isn't working label Sep 23, 2023
SKProCH added a commit that referenced this issue Sep 23, 2023
@SKProCH
Copy link
Member

SKProCH commented Sep 23, 2023

Hello, @ionite34

Just pushed an 3.2.1-rc1 version with some changes, related to this problem. Can you check out, does it solves your problem?

@ionite34
Copy link
Contributor Author

ionite34 commented Sep 25, 2023

Hello, @ionite34

Just pushed an 3.2.1-rc1 version with some changes, related to this problem. Can you check out, does it solves your problem?

This seems to still occur but errors on a different line, ObjectDisposedException when accessing .Token

await Task.Delay(10, cancellationTokenSource.Token);

I think the cause is that another call of UpdateImage, despite the interlocked exchange, can get the var cancellationTokenSource = new CancellationTokenSource(); and disposes it before the cancellationTokenSource.Token access which errors.

I reproduced the error in the demo project by modifying this function in AdvancedImagePage to do a lot of Source changes

        private void SetSourceButton_OnClick(object? sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Parallel.ForEach(Enumerable.Range(0, 100), i =>
                {
                    Dispatcher.UIThread.Invoke(() =>
                    {
                        CurrentImageExample.Source = $"/Assets/cat{Random.Shared.Next(4, 6)}.jpg";
                    });
                });
            });
        }

I've linked #20 which seems to fix the issue

ionite34 added a commit to ionite34/AsyncImageLoader.Avalonia that referenced this issue Sep 25, 2023
SKProCH added a commit that referenced this issue Sep 25, 2023
Fix ObjectDisposedException in Cancellation token access (#19)
@SKProCH SKProCH closed this as completed Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants