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

ByteBufferAsyncProcessor: prevent deadlocks on corrupted state exceptions #189

Merged
merged 1 commit into from
Dec 20, 2020

Conversation

ForNeVeR
Copy link
Collaborator

@ForNeVeR ForNeVeR commented Dec 19, 2020

It turns out that some of our code works under HandleProcessCorruptedStateExceptions attribute on .NET Framework. This attribute allows the process to proceed execution even in case of memory access violation, but will only execute catch and finally blocks (including the implicitly generated finally in case of a lock block) in methods directly marked with the attribute.

So, if ByteBufferAsyncProcessor.Put is called from such method, and then a corrupted state exception occurs inside of the lock block, the lock will be abandoned in a locked state. This could lead to deadlocks that are hard to detect.

Provided we don't want to change the behavior of all the external code right now, the safest action we could take is to guard the path leading to our logger with the same attribute. It will at least help to preserve the logger functionality even in case of corrupted state exception, which should be helpful.

…ions

It turns out that some of our code works under
[HandleProcessCorruptedStateExceptions] attribute on .NET Framework.
This attribute allows the process to proceed execution even in case of
memory access violation, but will only execute catch and finally blocks
in direct methods marked with the attribute.

So, if ByteBufferAsyncProcessor.Put is called from such method, and then
an exception occurs inside of the lock block, the lock will be abandoned
in a locked state. This could lead to deadlocks that are hard to detect.

Provided we don't want to change the behavior of all the external code
right now, the safest action we could take is to guard the path leading
to our logger with the same attribute. It will at least help to preserve
the logger functionality even in case of corrupted state exception,
which should be helpful.
@ForNeVeR ForNeVeR merged commit 996a3a2 into master Dec 20, 2020
@ForNeVeR ForNeVeR deleted the net211.im/safe-logger branch December 20, 2020 07:05
@@ -463,6 +463,9 @@ public bool Terminate(int timeoutMs = Timeout.Infinite)
}


#if !NET35
[System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions] // to force myLock to be unlocked even in case of corrupted state exception
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would have also added logging of the exception

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, a good idea! See #190.

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.

3 participants