ArgumentNullException when killing Boars and Chickens in OnEntitySpawnedError

Hello

I have been trying to identify some console log error spam of ArgumentNullExceptions caused by one of my plugins. It is supposed to prevent certain animals spawning in certain areas at certain times of day, so instead of being able to use chicken.population or boar.population svars I needed to solve this programmatically.

For some reason tho when specifically killing boars or chickens in OnEntitySpawned it would afterwards spam the console with ArgumentNullExceptions, as many exception messages as animals were killed. I have not been able to identify any other entities where this occurs.

Minimum reproducable example:

namespace Oxide.Plugins
{
    [Info("Test", "Longoon", "1.0")]
    class Test : RustPlugin
    {
        void OnEntitySpawned(BaseNetworkable entity)
        {
            if (entity.PrefabName == "assets/rust.ai/agents/boar/boar.prefab" || entity.PrefabName == "assets/rust.ai/agents/chicken/chicken.prefab")
            {
                entity.Kill();
                Puts("Incoming ArgumentNullException");
            }
        }
    }
}​

To reproduce:
1. Spool up a new Rust Server with Oxide
2. Once it's fully loaded add above Test.cs into the Oxide plugins folder
3. Wait for the server to spawn some Chickens or Boars, usually takes a minute
4. You will see a bunch of "Incoming ArgumentNullException" messages followed by a bunch of ArgumentNullExceptions

So my questions are:
1. Is this the proper way of killing/preventing animals from spawning?
2. If it is, then why am I getting all of these Exceptions? If it's not then what should I be doing instead?

I have already tried to debug RustDedicated in VisualStudio, enabled ArgumentNullException in Break When Thrown in Exception Settings and tried all available Code Types when attaching to the server, I was unable to get VS to break when these occur.

Thanks for your help!

Console log

You'd need to check your server log for the full error, but you aren't using anything that would cause that really.

Ah yes, sorry, I thought the console output was the server log, but I have checked the actual log and it says:

ArgumentNullException: Value cannot be null.
Parameter name: key
  at System.Collections.Generic.Dictionary`2[TKey,TValue].FindEntry (TKey key) [0x00008] in :0
  at System.Collections.Generic.Dictionary`2[TKey,TValue].TryGetValue (TKey key, TValue& value) [0x00000] in :0
  at Spatial.Grid`1[T].Remove (T obj) [0x00002] in :0
  at BaseEntity+Query+EntityTree.RemoveBrain (BaseEntity entity) [0x00000] in <6a0ecd327b0a4f348fdfee9fcd4348a3>:0
  at BaseAIBrain`1[T].OnDestroy () [0x00013] in <6a0ecd327b0a4f348fdfee9fcd4348a3>:0
  at AnimalBrain.OnDestroy () [0x00000] in <6a0ecd327b0a4f348fdfee9fcd4348a3>:0

(Filename:  Line: 0)
I suppose it's got nothing to do with umod then.

Edit: I don't know if this is related to killing Scientists on spawn but I am also getting these exceptions a lot:
NullReferenceException
  at (wrapper managed-to-native) UnityEngine.Component.get_gameObject(UnityEngine.Component)
  at SpawnGroup.Clear () [0x00015] in <6a0ecd327b0a4f348fdfee9fcd4348a3>:0
  at PuzzleReset.DoReset () [0x00119] in <6a0ecd327b0a4f348fdfee9fcd4348a3>:0
  at PuzzleReset.ResetTick () [0x00034] in <6a0ecd327b0a4f348fdfee9fcd4348a3>:0
  at InvokeHandlerBase`1[T].DoTick () [0x000be] in <29e58900325647f7920a9f7279f35770>:0
  at InvokeHandlerBase`1[T].LateUpdate () [0x0000c] in <29e58900325647f7920a9f7279f35770>:0

(Filename: <6a0ecd327b0a4f348fdfee9fcd4348a3> Line: 0)
Although I don't think there is much that I can do about those as both of them stem directly from Rust, not from umod or a plugin.

I'm not sure if/how killing the NPC would cause that, but you could try using NextTick() with that hook before killing them.