zed-industries/zed · error

invalid entity type

Error message

invalid entity type

What it means

A type-downcast guard in observe_release's release listener: entities are stored as AnyEntity and the callback receives &mut dyn Any, which is downcast to T — the type observe_release was registered for. The expect ('invalid entity type') fires only if the release listener for entity id X is invoked with an entity whose concrete type differs from the registration's T, i.e. entity ids were reused or release listeners were routed to the wrong slot — an internal bookkeeping error.

Source

Thrown at crates/gpui/src/app.rs:2175

    pub(crate) fn new_entity_observer(
        &self,
        key: TypeId,
        value: NewEntityListener,
    ) -> Subscription {
        let (subscription, activate) = self.new_entity_observers.insert(key, value);
        activate();
        subscription
    }

    /// Arrange for the given function to be invoked whenever a view of the specified type is created.
    /// The function will be passed a mutable reference to the view along with an appropriate context.
    pub fn observe_new<T: 'static>(
        &self,
        on_new: impl 'static + Fn(&mut T, Option<&mut Window>, &mut Context<T>),
    ) -> Subscription {
        self.new_entity_observer(
            TypeId::of::<T>(),
            Box::new(
                move |any_entity: AnyEntity, window: &mut Option<&mut Window>, cx: &mut App| {
                    any_entity
                        .downcast::<T>()
                        .unwrap()
                        .update(cx, |entity_state, cx| {
                            on_new(entity_state, window.as_deref_mut(), cx)
                        })
                },
            ),
        )
    }

    /// Observe the release of a entity. The callback is invoked after the entity
    /// has no more strong references but before it has been dropped.
    pub fn observe_release<T>(
        &self,
        handle: &Entity<T>,

View on GitHub (pinned to 9d272b0363)

Solutions

  1. Check downcast_mut() and skip the callback when the type mismatches, logging the entity id mismatch
  2. Store the expected TypeId alongside each release listener and verify before dispatch
  3. Ensure release listeners are fully removed when an entity is dropped so ids are never reused with stale listeners
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at crates/gpui/src/app.rs:2138 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of zed-industries/zed@9d272b0363 (2026-08-20). Data as JSON: /api/errors/166d18dd310fca26. Report an issue: GitHub.