Add-On crashes when saving .blend file in bpy.app.handlers.render_pre

I’m working on my add-on that saves the .blend file on every F12 in a new folder

Source:
GitHub - ICarryTheDustOfAJourney/Autosave-Render: Add-In for Blender to automatically save files when rendering (older version)

I’m updating the code now, but I’m now encountering all sorts of horrible crashes after it worked without problems for years. In most cases Blender sponaneously exits, sometimes it hangs forever, very random behaviour.

I guess datastructures get updated due to rendering while the (probably) async writing thread started by bpy.ops.wm.save_as_mainfile() is still trying to save it. Is there a sync version of this call?

Blender seems not to be thread-save. Using print() I found out, that the crash can occur in various stages of the code execution, not necessarily immediately after the call of bpy.ops.wm.save_as_mainfile()

‘Render’-> ‘Lock Interface’ is activated , btw

Some frequent error-messages from the test.crash.txt files :

Code marked as unreachable has been executed. Please report this as a bug.
Error found at C:\Users\blender\git\blender-v400\blender.git\source\blender\nodes\NOD_geometry_exec.hh:337 in get_output_index.
Error   : EXCEPTION_ACCESS_VIOLATION
Address : 0x00007FF703149DC3
Module  : blender.exe
Thread  : 00000d44
Calling operator "bpy.ops.wm.save_as_mainfile" error, can\'t modify blend data in this state (drawing/rendering)

…and all kinds of EXCEPTION_ACCESS_VIOLATION, sometimes up to 9 at at time

Since I can’t explicitly allocate and point to memory in Python, it can’t be a direct error in my code, it must be something in the code invoked by it.

I’m using 4.0.2 on W10/W11, GTX 2080, Ryzen 7, 32GB RAM, SSDs, render engine doesn’t make no difference.

Btw: I’m new here, is this the right place for such questions?

Meanwhile I (hope I) found out what the real reason is:

When the pathname of the file to be saved exceeds the OS’ path name length limit (Windows: ~250 chars) blender crashes with all kinds of and not so kind errormessages…

Edit: This discussion is now here