InvalidCastException when sending MSMQ message #5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We are using your library under .net core and have discovered that, at least it seems, when the code is under load (~10 req/sec) we get the following exception:
I'm not entirely sure what all the code in here is doing, but it appears to be constructing COM property arrays (presumably for placing/serializing into MSMQ). Maybe? I haven't dealt with MSMQ at this level before. Regardless, a particular function of yours has me curious that perhaps we're running into some issues with a collision of intent of an array:
When looking at the code that's crashing, I see you're casing it and thereby treating it as a uint at the time of the crash. However, the object in the array at that index is a GCHandle (which seems to be the second intent of the handles array). Maybe the fact you're using this array to store different things is causing an issue? Or perhaps index collisions?
Without knowing all of the specifics I modified your code to be more mindful of nullity and the underlying types. It seems to have prevented the crash we're experiencing. But I'm opening this PR to open a discussion and not to necessarily recommend this code as-is. I just don't understand what's going on underneath to say for sure if this is even a valid fix.
One last thing: when I cleaned up the code to be mindful of whether the underying type as a GCHandle, I had to also be aware to not call .Free() on a null object. It seems as though the GCHandle, if it exists, was being freed and collected previously. I tried to determine if there was anything async going on (threads stomping on each other), by putting things inside lock statements, but to not avail.