From 295bf8f08fdd99f1767b616e6247253b89f47022 Mon Sep 17 00:00:00 2001
From: Jim Crist-Harif <jcristharif@gmail.com>
Date: Mon, 4 Oct 2021 10:02:55 -0500
Subject: [PATCH] Pass `host` through LocalCluster to workers

Previously the `host` parameter to `LocalCluster` would only be
forwarded to `Scheduler` instances and not `Worker`/`Nanny` instances,
leading to workers listening on non-localhost in some configurations.
This fixes that and adds a test.
---
 distributed/deploy/local.py            |  1 +
 distributed/deploy/tests/test_local.py | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+)

--- a/distributed/deploy/local.py
+++ b/distributed/deploy/local.py
@@ -189,6 +189,7 @@
 
         worker_kwargs.update(
             {
+                "host": host,
                 "nthreads": threads_per_worker,
                 "services": worker_services,
                 "dashboard_address": worker_dashboard_address,
--- a/distributed/deploy/tests/test_local.py
+++ b/distributed/deploy/tests/test_local.py
@@ -4,6 +4,7 @@
 import subprocess
 import sys
 from time import sleep
+from urllib.parse import urlparse
 from threading import Lock
 import unittest
 import weakref
@@ -1045,3 +1046,20 @@
         n_workers=0, silence_logs=False, dashboard_address=None, asynchronous=True
     ) as c:
         pass
+
+
+@pytest.mark.asyncio
+@pytest.mark.parametrize("host", [None, "127.0.0.1"])
+@pytest.mark.parametrize("use_nanny", [True, False])
+async def test_cluster_host_used_throughout_cluster(host, use_nanny):
+    """Ensure that the `host` kwarg is propagated through scheduler, nanny, and workers"""
+    async with LocalCluster(host=host, asynchronous=True) as cluster:
+        url = urlparse(cluster.scheduler_address)
+        assert url.hostname == "127.0.0.1"
+        for worker in cluster.workers.values():
+            url = urlparse(worker.address)
+            assert url.hostname == "127.0.0.1"
+
+            if use_nanny:
+                url = urlparse(worker.process.worker_address)
+                assert url.hostname == "127.0.0.1"
