Repository: ray-project/ray
File: python/ray/tests/conftest.py (Python)

Code before:
                process_info.process.wait(timeout=5)
            except OSError:
                # Process already dead
                pass
            except subprocess.TimeoutExpired:
                logger.warning(
                    f"Redis server process {process_info.process.pid} did not exit within 5 seconds "
                    "after SIGKILL during test cleanup"
                )
    else:
        # Find and kill all Redis server processes on host
        redis_procs = []
        for proc in psutil.process_iter(["name", "cmdline"]):
            try:
                if proc.name() == "redis-server":
                    redis_procs.append(proc)
            except psutil.NoSuchProcess:
                pass

        for proc in redis_procs:
            try:
                proc.kill()
                proc.wait(timeout=5)
            except psutil.NoSuchProcess:
                pass
            except psutil.TimeoutExpired:
                logger.warning(
                    f"Redis server process {proc.pid} did not exit within 5 seconds "
                    "after SIGKILL during test cleanup"
                )


@contextmanager
def _setup_redis(request, with_sentinel=False):
    with tempfile.TemporaryDirectory() as tmpdirname:
        kill_all_redis_server()
        address_str, processes = (
            start_redis_with_sentinel(tmpdirname)
            if with_sentinel
            else start_redis(tmpdirname)
        )
        old_addr = os.environ.get("RAY_REDIS_ADDRESS")
        os.environ["RAY_REDIS_ADDRESS"] = address_str
        import uuid

        ns = str(uuid.uuid4())
        old_ns = os.environ.get("RAY_external_storage_namespace")
        os.environ["RAY_external_storage_namespace"] = ns

        yield
        if old_addr is not None:

Review comment (suggestion):
we should probably just let it raise if this happens to make the issue obvious, should never stay alive 5s after sigkill

Code after:
    # Kill Redis server processes
    for proc in redis_procs:
        try:
            proc.kill()
        except psutil.NoSuchProcess:
            pass


@contextmanager
def _setup_redis(request, with_sentinel=False):
    with tempfile.TemporaryDirectory() as tmpdirname:
        kill_all_redis_server()
        address_str, processes = (
            start_redis_with_sentinel(tmpdirname)
            if with_sentinel
            else start_redis(tmpdirname)
        )
        old_addr = os.environ.get("RAY_REDIS_ADDRESS")
        os.environ["RAY_REDIS_ADDRESS"] = address_str
        import uuid

        ns = str(uuid.uuid4())
        old_ns = os.environ.get("RAY_external_storage_namespace")
        os.environ["RAY_external_storage_namespace"] = ns

        yield
        if old_addr is not None:
            os.environ["RAY_REDIS_ADDRESS"] = old_addr
        else:
            del os.environ["RAY_REDIS_ADDRESS"]

        if old_ns is not None:
            os.environ["RAY_external_storage_namespace"] = old_ns
        else:
            del os.environ["RAY_external_storage_namespace"]

        kill_processes(processes)


@pytest.fixture
def maybe_setup_external_redis(request):
    if external_redis_test_enabled():
        with _setup_redis(request):
            yield
    else:
        yield


@pytest.fixture(scope="module")
def maybe_setup_external_redis_shared(request):
    if external_redis_test_enabled():