Harden events against race conditions#5722
Conversation
src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaContext.cs
Show resolved
Hide resolved
| { | ||
| CommittingBatch(Channel, new EventArgs()); | ||
| } | ||
| CommittingBatch?.Invoke(Channel, new EventArgs()); |
There was a problem hiding this comment.
This is more of a style change, I don't think this does anything for race conditions. It generates the same code with a manual null check or a compiler-generated null check: https://sharplab.io/#v2:D4AQTAjAsAUCDMACciDCiDetE+UkALIgLIQAUAlNrljLvYgJYBmiZqA9gLZeMAufRgDsA5gCEAhnwDGAC0QBCALyIhAVwA2GqnQY5aevZx79BoyTNll1WgDSrN2gNzU9AX1eIPunJ4TIiYjBKTwNDY14BYXEpOQB+ADoASSEANw4AawBTa0d7G2dPb3o/JCzUrKE+RABRCqqACQkhABMNLIAnNG5IsxjLFxg3IA=
There was a problem hiding this comment.
@ThomasGoulet73 Not the same code. M2 copies the event and is then immune to further changes.
There was a problem hiding this comment.
Turns out you're right! I made the wrong assumption that invoking CommittingBatch was simply using it as a reference but turns out it does make a copy.
Here's the code I used to test this (It simulates a race condition):
https://sharplab.io/#v2:C4LgTgrgdgPgAgJgAwFgBQiCM65IARyYCsA3OugMJ4DGeAvHlAKYDueFAFAJRlrrUA6CgHsAtqICWwYBKgBzAEIBDYNQAWeANQMAogDcmUYJl44AbHmD0aAgCpMAzse6mMATgJmBAESYAbJQBPDkwAfSQInnI0QRFxKRl5ZVUNAFpdAyMTaLgPYFc4ABY8fUNnYQAjACsmaisHQwATJjAAGhLM4ABBMDkHPCVehy50AG90AF8cgGYCBHYx9Dxlglm4AA5PPHsnTG4llfG0FZOOsoAJJShGvxaaMUlpWUUVdWtgNQkHIQeE5+T1K5TitCG4OAASABEADkIH4/CA8KNqL8nklXho6AwoHC/BNIVE+MdgQQPHAvL4AsEEOFIkCSaCITDcYjkajEi8UvRsbj8YSTlMiSc4GtNuTto5gAh9sTlkcSQRMGCobD4ay4o8OQDMTz4Xz6cDcp4fP4ghwaREkISDoalUzVQikRq/uiuVjGLyCbwBdFhbMmJ0zkZLtdbmB2Oz/hjeBMgA==
C.Test1 prints
Null: False
Null: False
C.Test2 prints
Null: False
Null: True
No description provided.