Sequentially creating and distroying render products/ writers

Hi there, I have a bunch of USD files I call “Templates”. In my extention, I try to open a random template, move the camera around with scatter2D and I have a custom writer that saves the images at every orchestrator step in my system. I take X snapshot from each randomly selected template and then I repeat the process. Looks like this:

    async def load_templates_and_run(self):
        for _ in range(NUM_OF_GARDENS):
            template = np.random.choice([TEMPLATE1, TEMPLATE2, TEMPLATE3])
            await self.open_stage(template)
            await asyncio.sleep(10)
            if self.delegates is not None and len(self.delegates) > 0:
                for delegate in self.delegates:
                    delegate()
            randomize_scene_replicator()
            carb.log_error(f"Randomized Garden. waiting 10 seconds...")
            await asyncio.sleep(10)
            await self.run_randomization()


    async def run_randomization(self):
        for i in range(NUM_OF_FRAMES_PER_GARDEN):
            carb.log_error(f"frame number {i}")
            await rep.orchestrator.step_async(rt_subframes=40)
            carb.log_error(f"step async... ")
            await omni.kit.app.get_app().next_update_async()

        await omni.kit.app.get_app().next_update_async()
        rep.orchestrator.stop()

Now, every time I open a new USD (template) I need to reanitialize my my custom writer (which also creates a render product with rep.create.render_product(cam_path, resolution)). For this I use the delegates parameter:

    def __init__(self, num_of_gardens = NUM_OF_GARDENS, num_of_frames_per_garden = NUM_OF_FRAMES_PER_GARDEN, delegates = None):

        self.num_of_gardens = num_of_gardens
        self.num_of_frames_per_garden = num_of_frames_per_garden
        self.delegates = delegates
        
        self.templates_signal = asyncio.ensure_future(self.load_templates_and_run())

and I call init like this:

    def StartRandomizerSimulation(self):
        
        self.template_randomizer = TemplateRandomization(delegates = [self.initialize_cities_writer])
        

initialize_cities_writer() simply registers my CustomWriter and creates and attaches a render product.

Now, I believe I am missing a step where I need to distroy the previously created render product and/or writer before I create new ones?
The first time I switch from an existing template (after running NUM_OF_FRAMES_PER_GARDEN frames), My consule fluds with the error:

[Error] [omni.usd] HydraEngine::render failed to end the compute graph: error code 6
2024-07-11 21:43:10 [207,486ms] [Error] [omni.hydra] Invalid USD RenderProduct Prim: /Render/RenderProduct_Replicator

This is the log file:

kit_20240712_003943.log (27.1 MB)

How can I handle this issue? should I be somehow distroying the existing render product or writer before switching templates? if so, then how?

Thank you,
Danielle