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

Enabling or disabling a component between defer_suspend and defer_resume keeps the world deferred after progress #1209

Closed
humanoidentity opened this issue May 21, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@humanoidentity
Copy link

humanoidentity commented May 21, 2024

Describe the bug
Whenever I enable or disable a component within a no_readonly system, between defer_suspend + defer_resume calls, the world stays deferred after running progress().

Briefly, this is what I'm trying to do:

m_world
    .system()
    .no_readonly()
    .iter([&](flecs::iter& it) {
        m_world.defer_suspend();
        entity.disable<MyComponent>(); // This somehow causes the world to stay deferred after progress().
        m_world.defer_resume();
    });

To Reproduce
This is a complete repro case, reduced from a larger project:

#include "flecs.h"

#include <iostream>

using namespace flecs;

struct MyComponent {
    int value = 0;
};

int main() {
    // Create the world.
    world m_world;

    // Register a component.
    m_world.component<MyComponent>();

    // Create an entity with the component.
    auto entity = m_world.entity();
    entity.set<MyComponent>({});

    // Register a system that tries to disable the component while deferring is suspended.
    m_world
        .system()
        .no_readonly()
        .iter([&](flecs::iter& it) {
            m_world.defer_suspend();
            entity.disable<MyComponent>(); // This somehow causes the world to stay deferred after progress().
            m_world.defer_resume();
        });

    // Precondition: is_deferred correctly outputs false.
    std::cout << "m_world.is_deferred(): " << m_world.is_deferred() << std::endl;

    // Run the system.
    m_world.progress();

    // BUG: is_deferred incorrectly outputs true. World unexpectedly stays deferred after progress().
    std::cout << "m_world.is_deferred(): " << m_world.is_deferred() << std::endl;

    // Trying to run again will assert: !ecs_is_deferred(world) because the world stayed deferred.
    m_world.progress();
}

Steps to reproduce the behavior:

  1. Compile and run the code against latest flecs main (37233f1). I'm on Mac, so I used: clang -c flecs.c -o flecs.o && clang++ -std=c++17 flecs.o main.cpp -o program && ./program

The output is:

m_world.is_deferred(): 0
m_world.is_deferred(): 1
fatal: flecs.c: 62749: assert: !ecs_is_deferred(world) INVALID_OPERATION
1   program                             0x00000001040673fc flecs_log_msg + 1644
2   program                             0x0000000104091324 ecs_printv_ + 204
3   program                             0x00000001040361b0 ecs_log_ + 100
4   program                             0x000000010402cdb4 ecs_assert_log_ + 272
5   program                             0x00000001040d4934 flecs_workers_progress + 572
6   program                             0x00000001040d6254 ecs_progress + 348
7   program                             0x0000000104122dbc _ZNK5flecs5world8progressEf + 36
8   program                             0x0000000104122a78 main + 428
9   dyld                                0x00000001807250e0 start + 2360
Abort trap: 6

Expected behavior
I expected output of:

m_world.is_deferred(): 0
m_world.is_deferred(): 0

Additional context
It seems like the enabling / disabling of components is causing trouble. When I simply create entities or add components, the behavior is good.

@SanderMertens
Copy link
Owner

Just checked in a fix on the v4 branch

@humanoidentity
Copy link
Author

This is confirmed fixed on my end with the v4 branch, against both the minimal repro case and my larger project. I'll go ahead and close this bug. This fix makes a huge difference in my project. Many thanks @SanderMertens!

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

2 participants