Repository: apache/airflow
File: providers/microsoft/azure/src/airflow/providers/microsoft/azure/triggers/data_factory.py (Python)

Code before:
                if self.wait_for_termination:
                    while self.end_time > time.time():
                        try:
                            pipeline_status = await hook.get_adf_pipeline_run_status(
                                run_id=self.run_id,
                                resource_group_name=self.resource_group_name,
                                factory_name=self.factory_name,
                            )
                            executed_after_token_refresh = True
                            event = self._build_trigger_event(pipeline_status)
                            if event:
                                yield event
                                return
                            self.log.info(
                                "Sleeping for %s. The pipeline state is %s.",
                                self.check_interval,
                                pipeline_status,
                            )
                            await asyncio.sleep(self.check_interval)
                        except ServiceRequestError:
                            # conn might expire during long running pipeline.
                            # If exception is caught, it tries to refresh connection once.
                            # If it still doesn't fix the issue,
                            # than the execute_after_token_refresh would still be False
                            # and an exception will be raised
                            if executed_after_token_refresh:
                                await hook.refresh_conn()
                                executed_after_token_refresh = False
                            else:
                                raise

                    yield TriggerEvent(
                        {
                            "status": "error",
                            "message": f"Timeout: The pipeline run {self.run_id} has {pipeline_status}.",
                            "run_id": self.run_id,
                        }
                    )
                else:
                    yield TriggerEvent(
                        {
                            "status": "success",
                            "message": f"The pipeline run {self.run_id} has {pipeline_status} status.",
                            "run_id": self.run_id,
                        }
                    )
            except Exception as e:
                self.log.exception(e)
                if self.run_id:
                    try:
                        self.log.info("Cancelling pipeline run %s", self.run_id)

Review comment (suggestion):
Same here:

```
if not executed_after_token_refresh:
     raise
await hook.refresh_conn()
executed_after_token_refresh = False                            
```

Code after:
                    while self.end_time > time.time():
                        try:
                            pipeline_status = await hook.get_adf_pipeline_run_status(
                                run_id=self.run_id,
                                resource_group_name=self.resource_group_name,
                                factory_name=self.factory_name,
                            )
                            executed_after_token_refresh = True
                            event = self._build_trigger_event(pipeline_status)
                            if event:
                                yield event
                                return
                            self.log.info(
                                "Sleeping for %s. The pipeline state is %s.",
                                self.check_interval,
                                pipeline_status,
                            )
                            await asyncio.sleep(self.check_interval)
                        except ServiceRequestError:
                            # conn might expire during long running pipeline.
                            # If exception is caught, it tries to refresh connection once.
                            # If it still doesn't fix the issue,
                            # than the execute_after_token_refresh would still be False
                            # and an exception will be raised
                            if not executed_after_token_refresh:
                                raise
                            await hook.refresh_conn()
                            executed_after_token_refresh = False

                    yield TriggerEvent(
                        {
                            "status": "error",
                            "message": f"Timeout: The pipeline run {self.run_id} has {pipeline_status}.",
                            "run_id": self.run_id,
                        }
                    )
                else:
                    yield TriggerEvent(
                        {
                            "status": "success",
                            "message": f"The pipeline run {self.run_id} has {pipeline_status} status.",
                            "run_id": self.run_id,
                        }
                    )
            except Exception as e:
                self.log.exception(e)
                if self.run_id:
                    try:
                        self.log.info("Cancelling pipeline run %s", self.run_id)
                        await hook.cancel_pipeline_run(
                            run_id=self.run_id,