Skip to content

Commit de055b8

Browse files
committed
Fixed EntityDictionary potentially caching soon-to-be removed entities.
1 parent fc618e1 commit de055b8

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

  • addons/source-python/packages/source-python/entities

addons/source-python/packages/source-python/entities/dictionary.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
# ============================================================================
66
# >> IMPORTS
77
# ============================================================================
8+
# Python Imports
9+
# ContextLib
10+
from contextlib import suppress
11+
812
# Source.Python Imports
913
# Core
1014
from core import AutoUnload
1115
# Entities
16+
from entities.constants import EntityFlags
1217
from entities.entity import Entity
1318
from entities.helpers import index_from_inthandle
1419
# Listeners
@@ -46,8 +51,15 @@ def __init__(self, factory=Entity, *args, **kwargs):
4651

4752
def __missing__(self, index):
4853
"""Add and return the entity instance for the given index."""
49-
instance = self[index] = self._factory(index, *self._args,
50-
**self._kwargs)
54+
instance = self._factory(index, *self._args, **self._kwargs)
55+
56+
# Only cache entities that are not marked for deletion.
57+
# This is required, because if someone request an entity instance
58+
# after we invalidated our cache but before the engine processed
59+
# the deletion we would now have an invalid instance in the cache.
60+
if not instance.entity_flags & EntityFlags.KILLME:
61+
self[index] = instance
62+
5163
return instance
5264

5365
def __delitem__(self, index):

0 commit comments

Comments
 (0)