lidgren / lidgren-network-gen3

Lidgren Network Library
https://groups.google.com/forum/#!forum/lidgren-network-gen3
MIT License
1.19k stars 331 forks source link

Crash with NetServer.SendToAll() #122

Open cclogg opened 5 years ago

cclogg commented 5 years ago

This has been a fairly rare occurrence, but we had a few crashes that looked like this:

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at System.Collections.Generic.List`1.Enumerator.MoveNext()
   at Lidgren.Network.NetPeer.SendMessage(NetOutgoingMessage msg, IList`1 recipients, NetDeliveryMethod method, Int32 sequenceChannel)
   at Lidgren.Network.NetServer.SendToAll(NetOutgoingMessage msg, NetDeliveryMethod method)

It seems like the SendToAll method is enumerating all connections, but perhaps in the off chance that someone connects during that brief moment, this crash will happen. Side note: most of my code uses the SendMessage(NetOutgoingMessage msg, NetConnection recipient, NetDeliveryMethod method) method, and that one has never crashed thus far.