Skip to content

Commit e84bd49

Browse files
awaelchliethanwharris
authored andcommitted
App: Fix frontends when using multiprocessing in the cloud (#17324)
Co-authored-by: Ethan Harris <[email protected]>
1 parent 17c6c94 commit e84bd49

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/lightning/app/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
66

7+
## [2.0.1post] - 2023-04-12
8+
9+
### Fixed
10+
11+
- Fix frontend hosts when running with multi-process in the cloud ([#17324](https://github.com/Lightning-AI/lightning/pull/17324))
12+
13+
14+
715
## [2.0.1] - 2023-03-30
816

917
No changes.

src/lightning/app/runners/multiprocess.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,17 @@ def dispatch(self, *args: Any, open_ui: bool = True, **kwargs: Any):
6666

6767
_set_frontend_context()
6868
for frontend in self.app.frontends.values():
69-
host = "localhost"
7069
port = find_free_network_port()
71-
frontend.start_server(host="localhost", port=port)
72-
frontend.flow._layout["target"] = f"http://{host}:{port}/{frontend.flow.name}"
70+
71+
server_host = "0.0.0.0" if in_cloudspace else "localhost"
72+
server_target = (
73+
f"https://{port}-{constants.LIGHTNING_CLOUDSPACE_HOST}"
74+
if in_cloudspace
75+
else f"http://localhost:{port}"
76+
)
77+
78+
frontend.start_server(host=server_host, port=port)
79+
frontend.flow._layout["target"] = f"{server_target}/{frontend.flow.name}"
7380

7481
_set_flow_context()
7582

tests/tests_app/runners/test_multiprocess.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from lightning.app import LightningApp, LightningFlow, LightningWork
8+
from lightning.app.core import constants
89
from lightning.app.frontend import StaticWebFrontend, StreamlitFrontend
910
from lightning.app.runners import MultiProcessRuntime
1011
from lightning.app.utilities.component import _get_context
@@ -46,15 +47,34 @@ def run(self):
4647
self.stop()
4748

4849

50+
@pytest.mark.parametrize(
51+
"cloudspace_host, port, expected_host, expected_target",
52+
[
53+
(None, 7000, "localhost", "http://localhost:7000"),
54+
("test.lightning.ai", 7000, "0.0.0.0", "https://7000-test.lightning.ai"),
55+
],
56+
)
4957
@mock.patch("lightning.app.runners.multiprocess.find_free_network_port")
50-
def test_multiprocess_starts_frontend_servers(*_):
58+
def test_multiprocess_starts_frontend_servers(
59+
mock_find_free_network_port, monkeypatch, cloudspace_host, port, expected_host, expected_target
60+
):
5161
"""Test that the MultiProcessRuntime starts the servers for the frontends in each LightningFlow."""
62+
63+
monkeypatch.setattr(constants, "LIGHTNING_CLOUDSPACE_HOST", cloudspace_host)
64+
mock_find_free_network_port.return_value = port
65+
5266
root = StartFrontendServersTestFlow()
5367
app = LightningApp(root)
5468
MultiProcessRuntime(app).dispatch()
5569

5670
app.frontends[root.flow0.name].start_server.assert_called_once()
71+
assert app.frontends[root.flow0.name].start_server.call_args.kwargs["host"] == expected_host
72+
5773
app.frontends[root.flow1.name].start_server.assert_called_once()
74+
assert app.frontends[root.flow1.name].start_server.call_args.kwargs["host"] == expected_host
75+
76+
assert app.frontends[root.flow0.name].flow._layout["target"] == f"{expected_target}/{root.flow0.name}"
77+
assert app.frontends[root.flow1.name].flow._layout["target"] == f"{expected_target}/{root.flow1.name}"
5878

5979
app.frontends[root.flow0.name].stop_server.assert_called_once()
6080
app.frontends[root.flow1.name].stop_server.assert_called_once()

0 commit comments

Comments
 (0)