Repository: livekit/agents
File: livekit-agents/livekit/agents/ipc/supervised_proc.py (Python)

Code before:
        if hasattr(signal, "SIGUSR1"):
            return

        try:
            # send a message to the process to trigger stack trace dump on Windows
            # it might not work if the event loop is already blocked
            logger.info(
                "sending DumpStackTraceRequest message to process", extra=self.logging_extra()
            )
            await channel.asend_message(self._pch, proto.DumpStackTraceRequest())
            await asyncio.sleep(0.5)
        except Exception:
            pass

    async def _send_kill_signal(self) -> None:
        """forcefully kill the process"""
        try:
            if not self._proc.is_alive():
                return
        except ValueError:
            return

        logger.info("killing process", extra=self.logging_extra())
        if sys.platform == "win32":
            try:
                self._proc.terminate()
            except ValueError:
                pass
        else:
            if hasattr(signal, "SIGUSR1"):
                try:
                    logger.info("sending SIGUSR1 signal to process", extra=self.logging_extra())
                    os.kill(self._proc.pid, signal.SIGUSR1)  # type: ignore[arg-type]
                    await asyncio.sleep(0.5)
                except Exception:
                    pass
            try:
                self._proc.kill()
            except ValueError:
                pass

        self._kill_sent = True

    @log_exceptions(logger=logger)
    async def _supervise_task(self) -> None:
        try:
            await self._initialize_fut
        except asyncio.TimeoutError:
            pass  # this happens when the initialization takes longer than self._initialize_timeout
        except Exception:
            pass  # initialization failed

Review comment (suggestion):
```suggestion
                if self._proc.is_alive():
                    self._proc.terminate()
```

Code after:
        if hasattr(signal, "SIGUSR1"):
            return

        try:
            # send a message to the process to trigger stack trace dump on Windows
            # it might not work if the event loop is already blocked
            logger.info(
                "sending DumpStackTraceRequest message to process", extra=self.logging_extra()
            )
            await channel.asend_message(self._pch, proto.DumpStackTraceRequest())
            await asyncio.sleep(0.5)
        except Exception:
            pass

    async def _send_kill_signal(self) -> None:
        """forcefully kill the process"""
        try:
            if not self._proc.is_alive():
                return
        except ValueError:
            return

        logger.info("killing process", extra=self.logging_extra())
        if sys.platform == "win32":
            try:
                if self._proc.is_alive():
                    self._proc.terminate()
            except ValueError:
                pass
        else:
            if hasattr(signal, "SIGUSR1"):
                try:
                    logger.info("sending SIGUSR1 signal to process", extra=self.logging_extra())
                    os.kill(self._proc.pid, signal.SIGUSR1)  # type: ignore[arg-type]
                    await asyncio.sleep(0.5)
                except Exception:
                    pass
            try:
                if self._proc.is_alive():
                    self._proc.kill()
            except ValueError:
                pass

        self._kill_sent = True

    @log_exceptions(logger=logger)
    async def _supervise_task(self) -> None:
        try:
            await self._initialize_fut
        except asyncio.TimeoutError:
            pass  # this happens when the initialization takes longer than self._initialize_timeout