Skip to content

Commit 39ceda4

Browse files
fix(mcp): Resolve add_node resource leak and duplication issues
Modified godot_operations.gd to add a delay before freeing the instantiated scene root, preventing both RID leaks and node duplication.
1 parent fd64ead commit 39ceda4

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/scripts/godot_operations.gd

+26-9
Original file line numberDiff line numberDiff line change
@@ -572,26 +572,43 @@ func add_node(params):
572572
if debug_mode:
573573
print("Pack result: " + str(result) + " (OK=" + str(OK) + ")")
574574

575+
var save_error = ERR_CANT_CREATE # Initialize with an error state
576+
575577
if result == OK:
578+
absolute_scene_path = ProjectSettings.globalize_path(full_scene_path) # Ensure absolute path is defined here (Removed var)
576579
if debug_mode:
577580
print("Saving scene to: " + absolute_scene_path)
578-
var save_error = ResourceSaver.save(packed_scene, absolute_scene_path)
581+
save_error = ResourceSaver.save(packed_scene, absolute_scene_path) # Use absolute path for saving
579582
if debug_mode:
580583
print("Save result: " + str(save_error) + " (OK=" + str(OK) + ")")
584+
581585
if save_error == OK:
586+
# Simplified success message for clarity
587+
print("Node '" + params.node_name + "' of type '" + params.node_type + "' added successfully")
588+
# Add a small delay after successful save before cleanup
582589
if debug_mode:
583-
var file_check_after = FileAccess.file_exists(absolute_scene_path)
584-
print("File exists check after save: " + str(file_check_after))
585-
if file_check_after:
586-
print("Node '" + params.node_name + "' of type '" + params.node_type + "' added successfully")
587-
else:
588-
printerr("File reported as saved but does not exist at: " + absolute_scene_path)
589-
else:
590-
print("Node '" + params.node_name + "' of type '" + params.node_type + "' added successfully")
590+
print("Waiting briefly after save...")
591+
OS.delay_msec(100) # 100ms delay
591592
else:
592593
printerr("Failed to save scene: " + str(save_error))
594+
# No quit here, proceed to cleanup
593595
else:
594596
printerr("Failed to pack scene: " + str(result))
597+
# No quit here, proceed to cleanup
598+
599+
# --- Start Cleanup ---
600+
# Removed packed_scene.free() as PackedScene is RefCounted
601+
# Reinstate scene_root.free() to prevent RID leak
602+
if scene_root and is_instance_valid(scene_root):
603+
if debug_mode:
604+
print("Freeing instantiated scene root node after delay")
605+
scene_root.free() # Free the instantiated scene root
606+
# --- End Cleanup ---
607+
608+
# Quit only if there was a critical error during packing or saving
609+
if result != OK or save_error != OK:
610+
quit(1) # Exit with error code if packing or saving failed
611+
# Otherwise, the script will naturally exit after _init finishes
595612

596613
# Load a sprite into a Sprite2D node
597614
func load_sprite(params):

0 commit comments

Comments
 (0)