Repository: servo/servo
File: tests/wpt/tests/tools/wptrunner/wptrunner/browsers/servodriver.py (Python)

Code before:

        super().__init__(logger, binary=binary, webdriver_binary=binary,
                         webdriver_args=args, host=webdriver_host, env=env,
                         supports_pac=False, **kwargs)

        self.hosts_path = hosts_path

    def make_command(self):
        return [self.webdriver_binary, f"--webdriver={self.port}"] + self.webdriver_args

    def cleanup(self):
        os.remove(self.hosts_path)

    def is_alive(self):
        # This is broken. It is always True.
        if not super().is_alive():
            return False
        try:
            requests.get(f"http://{self.host}:{self.port}/status", timeout=3)    
        except requests.exceptions.Timeout:
            # FIXME: This indicates a hanged browser. Reasons need to be investigated further.
            self.logger.debug("Servo webdriver status request timed out.")
            return True
        except Exception as e:
            self.logger.debug(f"Servo has shut down normally. {e}")
            return False

        return True

    def stop(self, force=False):
        retry_cnt = 0
        while self.is_alive():
            self.logger.info("Trying to shut down gracefully by extension command")
            try:
                requests.delete(
                    f"http://{self.host}:{self.port}/session/dummy-session-id/servo/shutdown",
                    timeout=3
                )
            except requests.exceptions.ConnectionError:
                self.logger.debug("Browser already shut down (connection refused)")
                break
            except requests.exceptions.RequestException as e:
                self.logger.debug(f"Request exception: {e}")
                break
            except requests.exceptions.Timeout:
                self.logger.debug("Request timed out")
                break
                
            retry_cnt += 1
            if retry_cnt >= self.shutdown_retry:
                self.logger.debug("Max retry exceeded.")

Review comment (bug):
```suggestion
        except Exception as exception:
            self.logger.debug(f"Servo has shut down normally. {exception}")
            return False
```

Code after:

        super().__init__(logger, binary=binary, webdriver_binary=binary,
                         webdriver_args=args, host=webdriver_host, env=env,
                         supports_pac=False, **kwargs)

        self.hosts_path = hosts_path

    def make_command(self):
        return [self.webdriver_binary, f"--webdriver={self.port}"] + self.webdriver_args

    def cleanup(self):
        os.remove(self.hosts_path)

    def is_alive(self):
        # This is broken. It is always True.
        if not super().is_alive():
            return False
        try:
            requests.get(f"http://{self.host}:{self.port}/status", timeout=3)    
        except requests.exceptions.Timeout:
            # FIXME: This indicates a hanged browser. Reasons need to be investigated further.
            # It happens with ~0.1% probability in our CI runs.
            self.logger.debug("Servo webdriver status request timed out.")
            return True
        except Exception as exception:
            self.logger.debug(f"Servo has shut down normally. {exception}")
            return False

        return True

    def stop(self, force=False):
        retry_cnt = 0
        while self.is_alive():
            self.logger.info("Trying to shut down gracefully by extension command")
            try:
                requests.delete(
                    f"http://{self.host}:{self.port}/session/dummy-session-id/servo/shutdown",
                    timeout=3
                )
            except requests.exceptions.ConnectionError:
                self.logger.debug("Browser already shut down (connection refused)")
                break
            except requests.exceptions.RequestException as exeception:
                self.logger.debug(f"Request exception: {exeception}")
                break
            except requests.exceptions.Timeout:
                self.logger.debug("Request timed out")
                break
                
            retry_cnt += 1
            if retry_cnt >= self.shutdown_retry_attempts: