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

[Core] Disconnect one-shot signals before calling callbacks #89451

Merged
1 commit merged into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 36 additions & 41 deletions core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1100,11 +1100,6 @@ bool Object::_has_user_signal(const StringName &p_name) const {
return signal_map[p_name].user.name.length() > 0;
}

struct _ObjectSignalDisconnectData {
StringName signal;
Callable callable;
};

Error Object::_emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
if (unlikely(p_argcount < 1)) {
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
Expand Down Expand Up @@ -1153,78 +1148,78 @@ Error Object::emit_signalp(const StringName &p_name, const Variant **p_args, int
// which is needed in certain edge cases; e.g., https://github.com/godotengine/godot/issues/73889.
Ref<RefCounted> rc = Ref<RefCounted>(Object::cast_to<RefCounted>(this));

List<_ObjectSignalDisconnectData> disconnect_data;

// Ensure that disconnecting the signal or even deleting the object
// will not affect the signal calling.
LocalVector<Connection> slot_conns;
slot_conns.resize(s->slot_map.size());
{
uint32_t idx = 0;
for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
slot_conns[idx++] = slot_kv.value.conn;
Callable *slot_callables = (Callable *)alloca(sizeof(Callable) * s->slot_map.size());
uint32_t *slot_flags = (uint32_t *)alloca(sizeof(uint32_t) * s->slot_map.size());
uint32_t slot_count = 0;

for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
memnew_placement(&slot_callables[slot_count], Callable(slot_kv.value.conn.callable));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memnew is required here to handle the uninitialized memory

Copy link
Member Author

@AThousandShips AThousandShips Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotta fix memory management here

Edit: Resolved, had to destroy them properly

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's a better way to handle the lifetime here do tell, I don't have an asan setup at the moment so can't test efficiently for leaks

slot_flags[slot_count] = slot_kv.value.conn.flags;
++slot_count;
}

DEV_ASSERT(slot_count == s->slot_map.size());

// Disconnect all one-shot connections before emitting to prevent recursion.
for (uint32_t i = 0; i < slot_count; ++i) {
bool disconnect = slot_flags[i] & CONNECT_ONE_SHOT;
#ifdef TOOLS_ENABLED
if (disconnect && (slot_flags[i] & CONNECT_PERSIST) && Engine::get_singleton()->is_editor_hint()) {
// This signal was connected from the editor, and is being edited. Just don't disconnect for now.
disconnect = false;
}
#endif
if (disconnect) {
_disconnect(p_name, slot_callables[i]);
}
DEV_ASSERT(idx == s->slot_map.size());
}

OBJ_DEBUG_LOCK

Error err = OK;

for (const Connection &c : slot_conns) {
if (!c.callable.is_valid()) {
for (uint32_t i = 0; i < slot_count; ++i) {
const Callable &callable = slot_callables[i];
const uint32_t &flags = slot_flags[i];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added these for convenience, I ended up changing all the code below anyway as c went away but still neater


if (!callable.is_valid()) {
// Target might have been deleted during signal callback, this is expected and OK.
continue;
}

const Variant **args = p_args;
int argc = p_argcount;

if (c.flags & CONNECT_DEFERRED) {
MessageQueue::get_singleton()->push_callablep(c.callable, args, argc, true);
if (flags & CONNECT_DEFERRED) {
MessageQueue::get_singleton()->push_callablep(callable, args, argc, true);
} else {
Callable::CallError ce;
_emitting = true;
Variant ret;
c.callable.callp(args, argc, ret, ce);
callable.callp(args, argc, ret, ce);
_emitting = false;

if (ce.error != Callable::CallError::CALL_OK) {
#ifdef DEBUG_ENABLED
if (c.flags & CONNECT_PERSIST && Engine::get_singleton()->is_editor_hint() && (script.is_null() || !Ref<Script>(script)->is_tool())) {
if (flags & CONNECT_PERSIST && Engine::get_singleton()->is_editor_hint() && (script.is_null() || !Ref<Script>(script)->is_tool())) {
continue;
}
#endif
Object *target = c.callable.get_object();
Object *target = callable.get_object();
if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD && target && !ClassDB::class_exists(target->get_class_name())) {
//most likely object is not initialized yet, do not throw error.
} else {
ERR_PRINT("Error calling from signal '" + String(p_name) + "' to callable: " + Variant::get_callable_error_text(c.callable, args, argc, ce) + ".");
ERR_PRINT("Error calling from signal '" + String(p_name) + "' to callable: " + Variant::get_callable_error_text(callable, args, argc, ce) + ".");
err = ERR_METHOD_NOT_FOUND;
}
}
}

bool disconnect = c.flags & CONNECT_ONE_SHOT;
#ifdef TOOLS_ENABLED
if (disconnect && (c.flags & CONNECT_PERSIST) && Engine::get_singleton()->is_editor_hint()) {
//this signal was connected from the editor, and is being edited. just don't disconnect for now
disconnect = false;
}
#endif
if (disconnect) {
_ObjectSignalDisconnectData dd;
dd.signal = p_name;
dd.callable = c.callable;
disconnect_data.push_back(dd);
}
}

while (!disconnect_data.is_empty()) {
const _ObjectSignalDisconnectData &dd = disconnect_data.front()->get();

_disconnect(dd.signal, dd.callable);
disconnect_data.pop_front();
for (uint32_t i = 0; i < slot_count; ++i) {
slot_callables[i].~Callable();
}

return err;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# https://github.com/godotengine/godot/issues/89439
extends Node

signal my_signal

func async_func():
await my_signal
my_signal.emit()

func test():
async_func()
my_signal.emit()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GDTEST_OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# https://github.com/godotengine/godot/issues/89439

signal my_signal

func foo():
print("Foo")
my_signal.emit()

func bar():
print("Bar")

func baz():
print("Baz")

func test():
@warning_ignore("return_value_discarded")
my_signal.connect(foo, CONNECT_ONE_SHOT)
@warning_ignore("return_value_discarded")
my_signal.connect(bar, CONNECT_ONE_SHOT)
@warning_ignore("return_value_discarded")
my_signal.connect(baz)
my_signal.emit()
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
GDTEST_OK
Foo
Baz
Bar
Baz
Loading