Can't end Python script gracefully after rendering with Cycles

Once I render with Cycles with my script, I can’t end the script gracefully. The terminal gets stall and I can only exit the script via Control+C.

Compositing | Tile 38-40
Fra:69 Mem:276.22M (0.00M, Peak 276.22M) | Time:00:13.34 | Compositing | Tile 40-40
Fra:69 Mem:276.16M (0.00M, Peak 276.22M) | Time:00:13.34 | Compositing | De-initializing execution
Fra:69 Mem:276.16M (0.00M, Peak 276.22M) | Time:00:13.34 | Sce: Scene Ve:0 Fa:0 La:0
Saved: ‘/tmp/.png’
Time: 00:14.11 (Saving: 00:00.77)

INFO:root:Total 0:00:50 elapsed
Exception ignored in: <function CyclesRender.del at 0x7fca9ac66d40>
Traceback (most recent call last):
File “/home/xavier/Code/.venv/lib/python3.7/site-packages/2.83/scripts/addons/cycles/init.py”, line 67, in del
File “/home/xavier/Code/.venv/lib/python3.7/site-packages/2.83/scripts/addons/cycles/engine.py”, line 160, in free
ImportError: sys.meta_path is None, Python is likely shutting down
Error: Not freed memory blocks: 287, total unfreed memory 15.792580 MB

The thing is, I need this script to keep on running after the first render, as it will render a second scene.

Is there any workaround to this problem ?

EDIT: So I have been checking how to add an application fallback, following this answer:

The problem now is that when I register the callback, I can only print a message so far, but I can’t do anything else on that function. I am clearly missing something here, it would be great if somebody could shed some light so I can solve this.

EDIT: Ok I have made progress with this and now I can enqueue a second render. What I’m trying to figure out is how to make the handler fit properly in my app so I can register the callback function passing args, which to my understanding is not possible.

EDIT: I finally went the easier route by saving files and rendering from terminal, as I was getting a segfault after the second render was done via app handler.