Merge pull request #2798 from weeeeeeeeees1/fix/launcher-job-state-ownership

Keep web launcher job status tied to its process
This commit is contained in:
MoonEyes
2026-12-02 20:36:26 +00:00
committed by GitHub
2 changed files with 46 additions and 5 deletions

View File

@ -319,8 +319,10 @@ class Launcher:
self.log.extend(lines)
self._send("log", list(lines))
def set_state(self, text: str) -> None:
def set_state(self, process: subprocess.Popen, text: str) -> None:
with self.lock:
if self.process is not process:
return
changed = text != self.job_state
self.job_state = text
if changed:
@ -341,15 +343,16 @@ class Launcher:
self.say("Preview (--no-run): nothing was started. This would run:", " " + shown)
return "Preview: nothing was started. The command is in the log on the Play page."
try:
self.process = core.spawn(command, cwd=self.repo)
process = core.spawn(command, cwd=self.repo)
except OSError as err:
self.say(f"Could not start: {err}")
return f"Could not start: {err}"
self.process = process
self.kind = kind
self.job_state = "Starting"
self.say("> " + shown)
self._send("state", None)
threading.Thread(target=self._follow, args=(self.process, kind), daemon=True).start()
threading.Thread(target=self._follow, args=(process, kind), daemon=True).start()
return ""
def _follow(self, process: subprocess.Popen, kind: str) -> None:
@ -361,12 +364,14 @@ class Launcher:
new = core.state_for_line(state, line + "\n")
if new != state:
state = new
self.set_state(state)
self.set_state(process, state)
code = process.wait()
self.say(f"Exited with code {code}.")
with self.lock:
if self.process is not process:
return
self.kind = ""
self.set_state("Ready" if code == 0 else f"Exited with code {code}")
self.job_state = "Ready" if code == 0 else f"Exited with code {code}"
self._send("state", None)
def play(self) -> str:

View File

@ -12,6 +12,7 @@ from pathlib import Path
import tempfile
import threading
import unittest
from unittest.mock import Mock, patch
ROOT = Path(__file__).resolve().parents[1]
_spec = importlib.util.spec_from_file_location("leonida_launcher_serve", ROOT / "launcher" / "serve.py")
@ -94,6 +95,41 @@ class FetchTests(unittest.TestCase):
self.assertEqual(os.listdir(dst), [])
class JobTests(unittest.TestCase):
def test_old_follower_leaves_the_new_job_state_intact(self):
with tempfile.TemporaryDirectory() as tmp:
launcher = serve.Launcher(settings_path=str(Path(tmp) / "settings.json"))
old = Mock(stdout=[b"loading\n"])
old.wait.return_value = 7
active = Mock()
active.poll.return_value = None
launcher.process = active
launcher.kind = "play"
launcher.job_state = "Starting"
with patch.object(serve.core, "state_for_line", return_value="Loading"):
launcher._follow(old, "play")
self.assertTrue(launcher.running())
self.assertEqual(launcher.kind, "play")
self.assertEqual(launcher.job_state, "Starting")
def test_current_follower_publishes_its_exit_status(self):
with tempfile.TemporaryDirectory() as tmp:
launcher = serve.Launcher(settings_path=str(Path(tmp) / "settings.json"))
process = Mock(stdout=[])
process.wait.return_value = 7
process.poll.return_value = 7
launcher.process = process
launcher.kind = "play"
launcher._follow(process, "play")
self.assertFalse(launcher.running())
self.assertEqual(launcher.kind, "")
self.assertEqual(launcher.job_state, "Exited with code 7")
class ServerTests(unittest.TestCase):
@classmethod
def setUpClass(cls):