Skip to content

Commit b679fc2

Browse files
tchatonmanskxBorda
authored
(app) Resolve a bug where the state changes isn't detected properly (#14465)
Co-authored-by: Mansy <[email protected]> Co-authored-by: Jirka Borovec <[email protected]>
1 parent d013bcc commit b679fc2

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src/lightning_app/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
2121

2222
-
2323

24+
### Fixed
25+
26+
- Resolved a bug where the state change detection using DeepDiff won't worked with Path, Drive objects ([#14465](https://github.com/Lightning-AI/lightning/pull/14465))
2427

2528
### Removed
2629

src/lightning_app/core/app.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from time import time
1010

1111
from deepdiff import DeepDiff, Delta
12+
from lightning_utilities.core.apply_func import apply_to_collection
1213

1314
import lightning_app
1415
from lightning_app import _console
@@ -21,6 +22,7 @@
2122
)
2223
from lightning_app.core.queues import BaseQueue, SingleProcessQueue
2324
from lightning_app.frontend import Frontend
25+
from lightning_app.storage import Drive, Path
2426
from lightning_app.storage.path import storage_root_dir
2527
from lightning_app.utilities.app_helpers import _delta_to_app_state_delta, _LightningAppRef, Logger
2628
from lightning_app.utilities.commands.base import _process_requests
@@ -319,9 +321,12 @@ def maybe_apply_changes(self) -> bool:
319321
deltas = self._collect_deltas_from_ui_and_work_queues()
320322

321323
if not deltas:
324+
# Path and Drive aren't processed by DeepDiff, so we need to convert them to dict.
325+
last_state = apply_to_collection(self.last_state, (Path, Drive), lambda x: x.to_dict())
326+
state = apply_to_collection(self.state, (Path, Drive), lambda x: x.to_dict())
322327
# When no deltas are received from the Rest API or work queues,
323328
# we need to check if the flow modified the state and populate changes.
324-
deep_diff = DeepDiff(self.last_state, self.state, verbose_level=2)
329+
deep_diff = DeepDiff(last_state, state, verbose_level=2)
325330
if deep_diff:
326331
# TODO: Resolve changes with ``CacheMissException``.
327332
# new_state = self.populate_changes(self.last_state, self.state)

tests/tests_app/core/test_lightning_app.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from lightning_app.core.queues import BaseQueue, MultiProcessQueue, RedisQueue, SingleProcessQueue
2121
from lightning_app.frontend import StreamlitFrontend
2222
from lightning_app.runners import MultiProcessRuntime, SingleProcessRuntime
23+
from lightning_app.storage import Path
2324
from lightning_app.storage.path import storage_root_dir
2425
from lightning_app.testing.helpers import RunIf
2526
from lightning_app.testing.testing import LightningTestApp
@@ -975,3 +976,44 @@ def test_debug_mode_logging():
975976
app = LightningApp(A4())
976977
assert _console.level == logging.INFO
977978
MultiProcessRuntime(app, start_server=False).dispatch()
979+
980+
981+
class WorkPath(LightningWork):
982+
def __init__(self):
983+
super().__init__()
984+
self.path = None
985+
986+
def run(self):
987+
self.path = Path(__file__)
988+
989+
990+
class FlowPath(LightningFlow):
991+
def __init__(self):
992+
super().__init__()
993+
self.w = WorkPath()
994+
995+
def run(self):
996+
self.w.run()
997+
998+
999+
class TestLightningHasUpdatedApp(LightningApp):
1000+
def __init__(self, *args, **kwargs):
1001+
super().__init__(*args, **kwargs)
1002+
self.counter = 0
1003+
1004+
def run_once(self):
1005+
res = super().run_once()
1006+
1007+
if self.root.w.has_succeeded:
1008+
self.counter += 1
1009+
1010+
# TODO: Resolve bug where it should work with self.counter == 2
1011+
if self.counter > 5:
1012+
assert not self._has_updated
1013+
return True
1014+
return res
1015+
1016+
1017+
def test_lightning_app_has_updated():
1018+
app = TestLightningHasUpdatedApp(FlowPath())
1019+
MultiProcessRuntime(app, start_server=False).dispatch()

tests/tests_app_examples/test_v0_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class LightningAppTestInt(LightningTestApp):
1313
def run_once(self) -> Tuple[bool, float]:
14-
if self.root.counter > 1:
14+
if self.root.counter == 1:
1515
print("V0 App End")
1616
self.stage = AppStage.STOPPING
1717
return True, 0.0

0 commit comments

Comments
 (0)