Skip to content

Hanging and undefined behavior in register_virtuals when overriding RigidDynamicBody3D's _integrate_forces for GDExtension class #651

Closed

Description

@nwroyer

I've noticed this when attempting to override _integrate_forces when extending RigidDynamicBody3D for a GDExtension. _physics_process, process, and _ready all execute without any issue, but the moment I attempt to uncomment the class's _integrate_forces, Godot hangs, displaying the opening splash screen and never actually loading the scene. Use of a debugger and print statements reveals that the overriding _integrate_forces isn't even called, and the hang occurs after _ready is executed.

Using the debugger reveals that the hang is occurring when register_virtuals is called in RigidDynamicBody3D, specifically in the following call stack:

  • RigidDynamicBody3D::register_virtuals
  • call_with_ptr_args (expanded from the BIND_VIRTUAL_METHOD macro)

Occasionally, this will crash with an abort signal instead of hanging, but I've been unable to nail down what will allow me to reproduce a crash. The hang always occurs when there's no crash, and will do so regardless of the content of _integrate_forces.

I suspect the issue is related to #630 since overriding any of the virtual functions that take basic types doesn't produce this behavior, and _integrate_forces has a single parameter of type PhysicsDirectBodyState3D*.

Activity

  1. added
    bug
    topic:gdextension
    on Nov 14, 2021
  2. nwroyer commented on Nov 17, 2021

    @nwroyer
    Author

    After some more investigation, I've observed that occasionally a SIGABRT will end up being raised within Object::get_instance_binding in the first two lines. When _integrate_forces is commented out, the program runs as expected; when it's left in, the error reappears, with no compile-time errors in either case.

    1    void *Object::get_instance_binding(void *p_token, const GDNativeInstanceBindingCallbacks *p_callbacks) {
    2      	void *binding = nullptr;
    3              _instance_binding_mutex.lock();
    ...

    The error is being raised in the line labeled 3 above.

    Further exploration with GDB indicates that RigidDynamicBody3D::register_virtuals is as follows:

    	template <class T>
    	static void register_virtuals() {
    		PhysicsBody3D::register_virtuals<T>();
    		if constexpr (!std::is_same_v<decltype(&RigidDynamicBody3D::_integrate_forces),decltype(&T::_integrate_forces)>) {
    			BIND_VIRTUAL_METHOD(T, _integrate_forces);
    		}
    	} 
    

    Setting a message tracepoint and print statements at the start and end of this function reveals that this function is actually being called twice but not actually finishing, and is being called from PhysicsBody3D's attempt to call the virtual _integrate_forces. It seems that this behavior itself might be part of the issue, as it's attempting to register the virtual functions while calling them, it seems.

    Within the BIND_VIRTUAL_METHOD's expansion that calls `` is where the error appears to be raised, on the second call.

    When debugging with GDB, the following error message is produced from the command line itself is sometimes produced:

    godot.linuxbsd.tools.64: tpp.c:82: __pthread_tpp_change_priority: Assertion new_prio == -1 || (new_prio >= fifo_min_prio && new_prio <= fifo_max_prio)' failed.`

    Getting a call stack is proving difficult, however, as usually the program will simply hang as noted in the previous comment, and only occasionally produce the above message and crash outright. This leads me to suspect that something - perhaps the _instance_binding_mutex - is in an undefined state.

  3. changed the title [-]Hanging in register_virtuals when overriding RigidDynamicBody3D's _integrate_forces for GDExtension class[/-] [+]Hanging and undefined behavior in register_virtuals when overriding RigidDynamicBody3D's _integrate_forces for GDExtension class[/+] on Nov 17, 2021
  4. added this to the 4.0 milestone on Jul 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtopic:gdextension

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions