Strange physics behaviour when switching object parent to new parent

Godot Version

v4.2.2.stable.mono.official [15073afe3]

Question

I’m making a game that uses a magnet to move objects around. When I pull an object to the magnet I have code to attach the object to the magnet until it lets go. The way I am doing that is by changing the objects parent to be the magnet with the following function.

private void AttachObject(PhysicsBody2D body) {
	if (body.GetParent() != this && body is PhysicsBody2D) {
		
		// Store object space data
		Vector2 ObjectPosition = body.GlobalPosition;
		float ObjectRotation = body.GlobalRotation;
		
		// Remove object from original parent and add to this
		ObjectParent = body.GetParent();
		ObjectParent.RemoveChild(body);
		AddChild(body);


		// Get the collision shape from the attracted object
		CollisionShape2D ObjectCollision = null;
		foreach (Node node in body.GetChildren()) {
			if (node is CollisionShape2D shape) {
				ObjectCollision = shape;
			}
		}

		// Get the size of the object to offset the achor point
		// This keeps the object sitting next to the magnet without overlapping
		if (ObjectCollision != null) {
			Vector2 shapeSize = GetShapeSize(ObjectCollision);
			anchorOffset = shapeSize.X >= shapeSize.Y ? shapeSize.X : shapeSize.Y;
		}
		_anchor.Position = new Vector2(_anchor.Position.X + (anchorOffset / 2), _anchor.Position.Y);

		// Return object to it's original movement state
		body.GlobalPosition = ObjectPosition;
		body.GlobalRotation = ObjectRotation;

		// Store attached object in global variable
		attachedObject = body;

		ObjectAttached = true;		
	}
}

With this code to keep it fixed to the magnet.

	public override void _Process(double delta) {
		// Keeping the attached object to the anchor position
		if (ObjectAttached) {			
			attachedObject.GlobalPosition = _anchor.GlobalPosition;
			attachedObject.GlobalRotation = GlobalRotation;
		}
	}

When this happens, it seems to have two versions of the same object running at once. The object will visually be on the magnet but when showing collisions, I can see that the object has followed its physics path if I hadn’t grabbed it. If the phantom version of the object is moving while I let go, it will retain the phantoms position and velocity.

When removing the line of code that adds the to the magnet this behaviour doesn’t occur and it just disappears so I don’t believe it still has any tie to it’s old parent.

Edit:
I found a semi solution. When attaching the object to the magnet I am now stopping all its physics by sleeping, freezing and disabling the object and then reenabling all that when dettaching