Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(api): Tunnel 握手放到后台,断网时本机访问不等公网入口

longmeidao245dcf766374
diff --git a/src/peach/api.py b/src/peach/api.py
index 530ab55f2..61751edd9 100644
--- a/src/peach/api.py
+++ b/src/peach/api.py
@@ -398,9 +398,9 @@ def create_app(
loop = asyncio.get_running_loop()
previous_error_handler = loop.get_exception_handler()
loop.set_exception_handler(asyncio_handler)
- # 启动握手要等 cloudflared 连上边缘,可以占到几十秒;和 mDNS 一样交给线程,
- # 不要在事件循环里卡住整条服务的启动。
- await asyncio.to_thread(_start_tunnel, settings, tunnel_manager)
+ # 握手要等 cloudflared 连上边缘,断网时会等满 `wait_timeout`;放在后台,本机访问
+ # 不等公网入口。连接进度由 `tunnel_manager.snapshot()` 报告。
+ tunnel_startup = asyncio.create_task(asyncio.to_thread(_start_tunnel, settings, tunnel_manager))
ledger_ready = False
if settings.configured:
ledger_ready = _start_ledger_services(settings, contract,
@@ -443,6 +443,10 @@ def create_app(
contract.stop_background_jobs()
if mdns is not None:
await asyncio.to_thread(mdns.stop)
+ # stop 让还在等边缘的握手当场返回;握手若刚好在 stop 之后才拉起子进程,
+ # 第二次 stop 把它收掉。
+ await asyncio.to_thread(tunnel_manager.stop)
+ await tunnel_startup
await asyncio.to_thread(tunnel_manager.stop)
http_transport.close()
hls_plan_executor.shutdown(wait=False, cancel_futures=True)
diff --git a/src/peach/tunnel.py b/src/peach/tunnel.py
index 95cfff634..a3742fcf8 100644
--- a/src/peach/tunnel.py
+++ b/src/peach/tunnel.py
@@ -831,6 +831,9 @@ class TunnelManager:
break
self._ready.clear()
with self._lock:
+ # 等待期间被 `stop()` 收掉:状态归 stop 写,这里不再补一份 error。
+ if generation != self._generation:
+ return TunnelSnapshot(state="stopped")
process = self._process
url = self._url
error = self._error
diff --git a/tests/test_tunnel.py b/tests/test_tunnel.py
index 90a0d7740..68f08db65 100644
--- a/tests/test_tunnel.py
+++ b/tests/test_tunnel.py
@@ -3,6 +3,8 @@ import os
import subprocess
import sys
import tempfile
+import threading
+import time
import unittest
from pathlib import Path
from types import SimpleNamespace
@@ -467,6 +469,28 @@ class TunnelManagerTests(unittest.TestCase):
self.assertTrue(self.processes[0].terminated)
self.assertTrue(self.processes[0].killed)
+ def test_stop_during_the_edge_wait_ends_start_without_an_error_state(self):
+ def silent(command, **kwargs):
+ process = _Process(_Stream())
+ self.processes.append(process)
+ return process
+
+ manager = tunnel.TunnelManager(
+ self.root / "state", self.root / "logs", popen=silent, wait_timeout=30,
+ )
+ result = {}
+ starter = threading.Thread(target=lambda: result.update(snapshot=manager.start(self.plan())))
+ starter.start()
+ deadline = time.monotonic() + 5
+ while manager.snapshot().state != "starting" and time.monotonic() < deadline:
+ time.sleep(0.01)
+ manager.stop()
+ starter.join(timeout=5)
+ self.assertFalse(starter.is_alive())
+ self.assertEqual(result["snapshot"].state, "stopped")
+ self.assertTrue(self.processes[0].terminated)
+ self.assertFalse((self.root / "state" / tunnel.STATE_FILENAME).exists())
+
def test_snapshot_accepts_a_process_without_a_pid_attribute(self):
process = SimpleNamespace(poll=lambda: None, stdout=_Stream())
manager = tunnel.TunnelManager(self.root / "state", self.root / "logs")
@@ -899,6 +923,45 @@ class TunnelStartupTests(unittest.TestCase):
client.get("/healthz")
manager.start.assert_not_called()
+ def test_an_unreachable_edge_does_not_hold_back_local_readiness(self):
+ processes = []
+
+ def silent(command, **kwargs):
+ # 断网时的 cloudflared:进程活着,既不吐入口也不写 pidfile。
+ process = _Process(_Stream())
+ processes.append(process)
+ return process
+
+ manager = tunnel.TunnelManager(
+ self.root / "state", self.root / "logs", popen=silent, wait_timeout=30,
+ )
+ plan = tunnel.TunnelPlan(
+ binary=self.root / "cloudflared.exe",
+ origin=tunnel.TunnelOrigin("http://127.0.0.1:8900"),
+ command=("cloudflared", "tunnel", "--url", "http://127.0.0.1:8900"),
+ )
+ settings = PeachSettings(
+ db_path=self.root / "peach.db", configured=True, token="", tunnel_enabled=True,
+ tunnel_state_root=self.root / "state", tunnel_log_root=self.root / "logs",
+ )
+ began = time.monotonic()
+ with mock.patch.object(api.tunnel, "plan_for_settings", return_value=plan), \
+ TestClient(api.create_app(settings, tunnel_manager=manager)) as client:
+ ready = client.get("/healthz")
+ self.assertLess(time.monotonic() - began, 10)
+ self.assertEqual(ready.status_code, 200)
+ deadline = time.monotonic() + 5
+ state = ready.json()["tunnel"]["state"]
+ while state != "starting" and time.monotonic() < deadline:
+ time.sleep(0.01)
+ state = client.get("/healthz").json()["tunnel"]["state"]
+ self.assertEqual(state, "starting")
+ closing = time.monotonic()
+ # 关服务时 stop 打断还在等边缘的握手,不等满 `wait_timeout`,也不留 error 状态。
+ self.assertLess(time.monotonic() - closing, 10)
+ self.assertTrue(processes[0].terminated)
+ self.assertFalse((self.root / "state" / tunnel.STATE_FILENAME).exists())
+
class LocalClientTests(unittest.TestCase):
"""隧道转发进来的请求不算本机,配置页对它一律关门。"""