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

Ensure Singleton Closes Any Opened Object During Concurrent Open/Close #173

Closed
wants to merge 2 commits into from
Closed

Ensure Singleton Closes Any Opened Object During Concurrent Open/Close #173

wants to merge 2 commits into from

Conversation

paulsavides
Copy link

See Issue = #172

Issue Overview
When using the singleton class, running GetOrCreateAsync() & CloseAsync() in a particular order, the singleton will end up disposed but with Value remaining populated.

This mainly causes an issue because it is the backing for FaultTolerantAmqpObject<T>. So, a concurrent open/close may cause an amqpobject that we would expect to be closed to remain opened in the background.

Issue Reproduction
The unit test in this PR contains a reproduction directly against the singleton class.

This project reproduces the issue directly using the FaultTolerantAmqpObject class https://github.com/paulsavides/ServiceBusTesting/tree/b3a5df45c67af84a816ec935d5b6bb749d915b80/ReproAmqp

When using the changes I have made in this PR, have confirmed the issues are fixed.

Issue Fix
Generally just adding back a bit of code that was removed in this commit 8366a43

After actually opening the object, just add the following check:

if (this.disposed && this.TryRemove())
{
    OnSafeClose(value);
}

I briefly considered throwing an ObjectDisposedException here, however, the task result has already been set at this point so setting an exception on the task completion source would not work. Additionally, moving the IsDisposed check before the OnCreate() or SetResult would leave in a race condition so, without introducing additional locking we're stuck just returning a closed object here.

Also added a TryRemove() around both places we may call OnSafeClose(). This is just to try and ensure only one thread will actually call the remove.


Please let me know if you require any changes here, if this is the complete wrong approach or if I have completely misdiagnosed the issue.

Thank you for taking a look,
Paul Savides

@ghost
Copy link

ghost commented Nov 20, 2020

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.

❌ paulsavides sign now
You have signed the CLA already but the status is still pending? Let us recheck it.

@paulsavides paulsavides changed the title Ensure Single Closes Any Opened Object During Concurrent Open/Close Ensure Singleton Closes Any Opened Object During Concurrent Open/Close Nov 20, 2020
@paulsavides
Copy link
Author

er, sorry, going to remake this with a different account so I can fill out the cla more appropriately

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.

1 participant