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

Prevent ArgumentOutOfRangeException #96

Merged
merged 4 commits into from
Nov 11, 2021

Conversation

brminnick
Copy link
Owner

@brminnick brminnick commented Nov 11, 2021

Description

This PR fixes a race condition that can happen when the same event is removed in parallel.

Error

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Stack Trace

AsyncAwaitBestPractices.EventManagerService.RemoveEventHandler (System.String& eventName, System.Object& handlerTarget, System.Reflection.MemberInfo& methodInfo, System.Collections.Generic.Dictionary`2[System.String,System.Collections.Generic.List`1[AsyncAwaitBestPractices.Subscription]]& eventHandlers) [0x0005c] in <63bee8e7afc14051bfc24f7fd7c5ed2c>:0;
AsyncAwaitBestPracticesAsyncAwaitBestPractices.WeakEventManager.RemoveEventHandler (System.Delegate& handler, System.String& eventName) [0x00035] in <63bee8e7afc14051bfc24f7fd7c5ed2c>:0;AsyncAwaitBestPracticesAsyncAwaitBestPractices.WeakEventManager.RemoveEventHandler (System.Delegate handler, System.String eventName) [0x00000] in <63bee8e7afc14051bfc24f7fd7c5ed2c>:0;AsyncAwaitBestPracticesGitTrends.GitHubAuthenticationService.remove_AuthorizeSessionStarted (System.EventHandler value) [0x00000] in <5bbcdde3e8a94c7aaeae05ae46b1d556>:0;

Reproduction

Parallel.For(0, 10, count => _propertyChangedWeakEventManager.RemoveEventHandler(sampleDelegate, nameof(sampleDelegate)));

static void sampleDelegate(object? sender, EventArgs e)
{
}

Detailed Description

  • Adds lock to EventManagerService.RemoveEventHandler and EventManagerService.AddEventHandler
  • Adds Unit Test to ensure race condition no longer occurs
  • Adds .NET 6 support to Unit Tests
  • Adds .NET 5 and .NET 6 Support to Azure Pipelines Build
  • Increment to v6.0.3

@brminnick brminnick enabled auto-merge (squash) November 11, 2021 19:43
@brminnick brminnick merged commit 82a5d6b into main Nov 11, 2021
@brminnick brminnick deleted the Handle-ArgumentOutOfRangeException branch November 11, 2021 20:27
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.

None yet

1 participant