Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v4 Regression - ecs_ensure_id in hook causes INTERNAL_ERROR #1545

Open
cmbartschat opened this issue Jan 31, 2025 · 0 comments
Open

v4 Regression - ecs_ensure_id in hook causes INTERNAL_ERROR #1545

cmbartschat opened this issue Jan 31, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@cmbartschat
Copy link

Describe the bug

  1. Create a world, 2 components, and an entity
  2. Configure an on_add hook on component 1
  3. Defer the world
  4. Add component 1 to an entity
  5. Return the world to non-deferred
  6. The on_add hook runs
  7. The callback calls ecs_ensure_id on the entity for component 1 and component 2
  8. This results in an internal assertion failure
fatal: entity.c: 4689: assert: i != diff->added.count INTERNAL_ERROR
1   ecs-hook-test                       0x00000001004f6dfc ecs_log_ + 512
2   ecs-hook-test                       0x00000001004f6328 ecs_assert_log_ + 388
3   ecs-hook-test                       0x000000010051de74 flecs_defer_end + 4816
4   ecs-hook-test                       0x000000010052abb8 flecs_ensure + 844
5   ecs-hook-test                       0x0000000100532e18 flecs_set_id_move + 380
6   ecs-hook-test                       0x000000010051ee00 flecs_defer_end + 8796
7   ecs-hook-test                       0x000000010051ba34 ecs_defer_end + 304
8   ecs-hook-test                       0x0000000100435fb0 _ZN26EcsHookTest_OnAddHook_Test8TestBodyEv + 1108
9   ecs-hook-test                       0x0000000100486eb4 _ZN7testing8internal35HandleExceptionsInMethodIfSupportedINS_4TestEvEET0_PT_MS4_FS3_vEPKc + 292

This didn't happen on Flecs 3.2.12 (3210bff)

To Reproduce

#include "flecs.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

typedef struct {
} EmptyComponent1;
ECS_COMPONENT_DECLARE(EmptyComponent1);

typedef struct {
} EmptyComponent2;
ECS_COMPONENT_DECLARE(EmptyComponent2);

class EcsHookTest : public ::testing::Test {};

void addHook(ecs_iter_t *it) {
  ecs_world_t *world = it->world;
  for (int i = 0; i < it->count; i++) {
    ecs_entity_t entity = it->entities[i];
    ecs_ensure_id(world, entity, ecs_id(EmptyComponent1));
    ecs_ensure_id(world, entity, ecs_id(EmptyComponent2));
  }
}

TEST_F(EcsHookTest, OnAddHook) {

  ecs_world_t *world = ecs_init();
  ECS_COMPONENT_DEFINE(world, EmptyComponent1);
  ECS_COMPONENT_DEFINE(world, EmptyComponent2);

  ecs_entity_t eid = ecs_new(world);

  static const ecs_type_hooks_t hooks = {
    .on_add = addHook,
  };
  ecs_set_hooks_id(world, ecs_id(EmptyComponent1), &hooks);

  ecs_defer_begin(world);
  ecs_ensure_id(world, eid, ecs_id(EmptyComponent1));
  ecs_defer_end(world);
}

Expected behavior

Flecs's internal state should remain consistent during this operation, and not crash

@cmbartschat cmbartschat added the bug Something isn't working label Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant