Pumpkin-MC/Pumpkin · error

failed to add item stack resource

Error message

failed to add item stack resource

What it means

In to_wasm_event for BlockShearEntityEvent, after registering the world, the sheared item's ItemStack is wrapped in Arc<Mutex<>> and registered via state.add_item_stack(...); failure is unwrapped with expect('failed to add item stack resource'), panicking if the stack resource cannot be added to the host's resource table.

Solutions

  1. Ensure item stack resources are consumed/freed after each event so the resource table doesn't exhaust.
  2. Verify PluginHostState is alive and current before dispatching block shear events.
  3. Convert the expect into error propagation so dispatch can skip the plugin instead of panicking.
  4. Check for double-registration of the same stack and reuse resource handles where supported.

Example fix

// before
let item = state
    .add_item_stack(Arc::new(Mutex::new(self.item.clone())))
    .expect("failed to add item stack resource");
// after
let item = match state.add_item_stack(Arc::new(Mutex::new(self.item.clone()))) {
    Ok(item) => item,
    Err(e) => {
        log::error!("failed to add item stack resource: {e}");
        return;
    }
};
Defensive patterns

Strategy: validation

Validate before calling

// Confirm state capacity before registering the item stack
if state.resource_count() >= state.resource_limit() {
    log::warn!("plugin host resource table full; skipping event");
    return;
}

Try / catch

std::panic::catch_unwind(AssertUnwindSafe(|| dispatch_event(event))).unwrap_or_else(|_| {
    log::error!("failed to add item stack resource during event dispatch");
});

Prevention

When it happens

Trigger: Dispatching BlockShearEntityEvent to a WASM plugin when add_item_stack fails: resource-table exhaustion, invalid PluginHostState, or host shutdown in progress.

Common situations: High event volume filling the resource table; item stack added but never consumed after cancelled events; dispatch during plugin reload while state is reset.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of Pumpkin-MC/Pumpkin@8d4639e25a (2026-09-09). Data as JSON: /api/errors/b4ded31fc1180f71. Report an issue: GitHub.

Appendix: source

Thrown at crates/pumpkin/src/plugin/loader/wasm/wasm_host/wit/v0_1/events/block.rs:990

    fn from_wasm_event(event: Event, _state: &mut PluginHostState) -> Self {
        match event {
            Event::BlockReceiveGameEvent(_) => {
                panic!("Cannot construct BlockReceiveGameEvent from WASM")
            }
            _ => panic!("unexpected event type"),
        }
    }
}

impl ToFromWasmEvent for BlockShearEntityEvent {
    fn to_wasm_event(&self, state: &mut PluginHostState) -> Event {
        let target_world = state
            .add_world(self.world.clone())
            .expect("failed to add world resource");
        let item = state
            .add_item_stack(Arc::new(Mutex::new(self.item.clone())))
            .expect("failed to add item stack resource");
        Event::BlockShearEntityEvent(BlockShearEntityEventData {
            block_pos: to_wasm_block_position(self.block_pos),
            target_world,
            target_entity_id: self.target.get_entity().entity_id,
            item,
            cancelled: self.cancelled,
        })
    }

    fn apply_wasm_event(&mut self, event: Event, state: &mut PluginHostState) {
        cleanup_event(&event, state);
        if let Event::BlockShearEntityEvent(data) = event {
            self.cancelled = data.cancelled;
        }
    }

    fn from_wasm_event(event: Event, _state: &mut PluginHostState) -> Self {
        match event {

View on GitHub (pinned to 8d4639e25a)