What's the recommended method of circumventing infinite recursion with the scene_update_post handler? I am writing a Blender addon that stores extra information inside a mesh (as custom vertex data layers).
The information in these extra layers needs to be recalculated whenever the mesh changes. More specifically, this custom data is a function of the vertex position, so whenever a vertex's position changes, the data needs to be recalculated.

One way to recalculate the data when vertex positions change (the only one I know of) is by handling
bpy.app.handlers.scene_update_post. However, when I write the recalculated data to the mesh, bpy.app.handlers.scene_update_post is triggered again (as it should be)! This leads to an infinite recursion.

This behavior of bpy.app.handlers.scene_update_post is intentional. A similar thing happens with update callbacks for custom properties, as documented on developer.blender.org.


  What is the recommended way of sidestepping this problem? In other words, how can I avoid infinite recursion when handling bpy.app.handlers.scene_update_post?
I ran into this problem myself a while ago - the way i solved it was basically using a semaphore-like approach, where the handler would set a boolean value to true when it is invoked, and set it to false before it returns. Before setting the boolean, it would first check if it was already set, and if so, return without executing. I doubt that race conditions are a thing in Blender's python API, but hey, it works.

handler_running = False

    def handler():
        if handler_running:
            return
        handler_running = True
        # Do something
        handler_running = False