Werkzeug: eingefrorene Tische alter Mittler von aussen neu verbinden (--wache)
Mittler von vor27b97335haben keine Riss-Wache. hand/tisch_neu_verbinden.py erkennt den Riss am Ende des table_<nr>.log, findet im Mittler den Server-Griff der Pipe, an der der Unity-Tisch haengt (GetNamedPipeClientProcessId), und schliesst ihn (DUPLICATE_CLOSE_SOURCE) - wie riss_heilen. Live 15.11. 22:40: sechs eingefrorene Tische so geheilt, alle spielen wieder. --wache wiederholt das alle 10 s nur fuer alte Mittler (hoechstens 3 je Tisch in 10 Min) und endet, wenn keiner mehr laeuft. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit1a6fe30853)
This commit is contained in:
@@ -0,0 +1,242 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Einen eingefrorenen Tisch von aussen heilen: seine Spiel-Verbindung im Mittler trennen.
|
||||
|
||||
Fuer Mittler, die noch OHNE Riss-Wache laufen (vor 27b97335 gestartet): hat ihr Tisch die
|
||||
Rahmengrenze verloren (`[PIP] -> Exception frames` im table_<nr>.log), wartet sein Leser auf eine
|
||||
unsinnige Laenge und zeigt nichts mehr. Dasselbe wie `Mittler.riss_heilen`, nur von aussen: im
|
||||
Mittler-Prozess den Server-Griff der Pipe schliessen, an deren anderem Ende der Unity-Tisch dieses
|
||||
Raums haengt (GetNamedPipeClientProcessId). Der Mittler sieht EOF, haelt seine Bruecke und nimmt
|
||||
den reconnectenden Tisch wieder an (SPIEL-RECONNECT, 13.11. live belegt).
|
||||
|
||||
Sicher, weil der Server-Griff overlapped ist und der Mittler nur mit PeekNamedPipe pollt (kein
|
||||
blockierender ReadFile, der am geschlossenen Griff haengen bliebe).
|
||||
|
||||
python hand/tisch_neu_verbinden.py # zeigt eingefrorene Tische, tut nichts
|
||||
python hand/tisch_neu_verbinden.py --heilen # trennt sie
|
||||
python hand/tisch_neu_verbinden.py --heilen 1784516
|
||||
python hand/tisch_neu_verbinden.py --wache # alle 10 s, nur Mittler von vor dem Fix;
|
||||
# endet, wenn keiner davon mehr laeuft
|
||||
"""
|
||||
import argparse
|
||||
import ctypes
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
from ctypes import wintypes
|
||||
|
||||
HIER = os.path.dirname(os.path.abspath(__file__))
|
||||
WURZEL = os.path.dirname(HIER)
|
||||
for p in (HIER, os.path.join(WURZEL, "lesen"), os.path.join(WURZEL, "client")):
|
||||
if p not in sys.path:
|
||||
sys.path.insert(0, p)
|
||||
|
||||
import mittler # noqa: E402
|
||||
import winproc # noqa: E402
|
||||
|
||||
_k = ctypes.WinDLL("kernel32", use_last_error=True)
|
||||
_nt = ctypes.WinDLL("ntdll")
|
||||
|
||||
PROCESS_DUP_HANDLE = 0x0040
|
||||
DUPLICATE_SAME_ACCESS = 0x2
|
||||
DUPLICATE_CLOSE_SOURCE = 0x1
|
||||
FILE_TYPE_PIPE = 3
|
||||
SYSTEM_EXTENDED_HANDLE_INFORMATION = 64
|
||||
STATUS_INFO_LENGTH_MISMATCH = 0xC0000004
|
||||
|
||||
_k.OpenProcess.restype = wintypes.HANDLE
|
||||
_k.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
|
||||
_k.DuplicateHandle.argtypes = [wintypes.HANDLE, wintypes.HANDLE, wintypes.HANDLE,
|
||||
ctypes.POINTER(wintypes.HANDLE), wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
|
||||
_k.GetCurrentProcess.restype = wintypes.HANDLE
|
||||
_k.GetFileType.argtypes = [wintypes.HANDLE]
|
||||
_k.GetNamedPipeClientProcessId.argtypes = [wintypes.HANDLE, ctypes.POINTER(wintypes.ULONG)]
|
||||
_k.GetNamedPipeServerProcessId.argtypes = [wintypes.HANDLE, ctypes.POINTER(wintypes.ULONG)]
|
||||
_k.CloseHandle.argtypes = [wintypes.HANDLE]
|
||||
_k.GetProcessTimes.argtypes = [wintypes.HANDLE] + [ctypes.POINTER(wintypes.FILETIME)] * 4
|
||||
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
|
||||
|
||||
|
||||
class _Eintrag(ctypes.Structure):
|
||||
_fields_ = [("Object", ctypes.c_void_p), ("UniqueProcessId", ctypes.c_size_t),
|
||||
("HandleValue", ctypes.c_size_t), ("GrantedAccess", wintypes.ULONG),
|
||||
("CreatorBackTraceIndex", wintypes.USHORT), ("ObjectTypeIndex", wintypes.USHORT),
|
||||
("HandleAttributes", wintypes.ULONG), ("Reserved", wintypes.ULONG)]
|
||||
|
||||
|
||||
def griffe(pid):
|
||||
"""Alle Griffwerte des Prozesses `pid` (NtQuerySystemInformation, erweiterte Liste)."""
|
||||
groesse = 1 << 22
|
||||
while True:
|
||||
puffer = ctypes.create_string_buffer(groesse)
|
||||
noetig = wintypes.ULONG(0)
|
||||
st = _nt.NtQuerySystemInformation(SYSTEM_EXTENDED_HANDLE_INFORMATION, puffer, groesse,
|
||||
ctypes.byref(noetig)) & 0xFFFFFFFF
|
||||
if st == STATUS_INFO_LENGTH_MISMATCH:
|
||||
groesse = max(groesse * 2, noetig.value + 65536)
|
||||
continue
|
||||
if st != 0:
|
||||
raise OSError(f"NtQuerySystemInformation 0x{st:08x}")
|
||||
break
|
||||
anzahl = ctypes.c_size_t.from_buffer(puffer).value
|
||||
kopf = 2 * ctypes.sizeof(ctypes.c_size_t)
|
||||
feld = (_Eintrag * anzahl).from_buffer(puffer, kopf)
|
||||
return [e.HandleValue for e in feld if e.UniqueProcessId == pid]
|
||||
|
||||
|
||||
def spiel_griff(mittler_pid, tisch_pid):
|
||||
"""Der Server-Griff im Mittler, an dessen Pipe-Ende der Tischprozess haengt -> Wert oder None."""
|
||||
proz = _k.OpenProcess(PROCESS_DUP_HANDLE, False, mittler_pid)
|
||||
if not proz:
|
||||
raise OSError(f"OpenProcess {mittler_pid} err {ctypes.get_last_error()}")
|
||||
try:
|
||||
for wert in griffe(mittler_pid):
|
||||
kopie = wintypes.HANDLE()
|
||||
if not _k.DuplicateHandle(proz, wert, _k.GetCurrentProcess(), ctypes.byref(kopie),
|
||||
0, False, DUPLICATE_SAME_ACCESS):
|
||||
continue
|
||||
try:
|
||||
if _k.GetFileType(kopie) != FILE_TYPE_PIPE:
|
||||
continue
|
||||
klient, server = wintypes.ULONG(0), wintypes.ULONG(0)
|
||||
if not _k.GetNamedPipeClientProcessId(kopie, ctypes.byref(klient)):
|
||||
continue
|
||||
if not _k.GetNamedPipeServerProcessId(kopie, ctypes.byref(server)):
|
||||
continue
|
||||
if klient.value == tisch_pid and server.value == mittler_pid:
|
||||
return wert
|
||||
finally:
|
||||
_k.CloseHandle(kopie)
|
||||
return None
|
||||
finally:
|
||||
_k.CloseHandle(proz)
|
||||
|
||||
|
||||
def schliessen(mittler_pid, wert):
|
||||
"""Den Griff im Mittler schliessen (DUPLICATE_CLOSE_SOURCE, ohne Kopie)."""
|
||||
proz = _k.OpenProcess(PROCESS_DUP_HANDLE, False, mittler_pid)
|
||||
if not proz:
|
||||
raise OSError(f"OpenProcess {mittler_pid} err {ctypes.get_last_error()}")
|
||||
try:
|
||||
if not _k.DuplicateHandle(proz, wert, None, None, 0, False, DUPLICATE_CLOSE_SOURCE):
|
||||
raise OSError(f"DuplicateHandle(CLOSE_SOURCE) err {ctypes.get_last_error()}")
|
||||
finally:
|
||||
_k.CloseHandle(proz)
|
||||
|
||||
|
||||
def startzeit(pid):
|
||||
"""Startzeit eines Prozesses (Epoche) oder None."""
|
||||
proz = _k.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
|
||||
if not proz:
|
||||
return None
|
||||
try:
|
||||
t = [wintypes.FILETIME() for _ in range(4)]
|
||||
if not _k.GetProcessTimes(proz, *[ctypes.byref(x) for x in t]):
|
||||
return None
|
||||
roh = (t[0].dwHighDateTime << 32) | t[0].dwLowDateTime
|
||||
return roh / 1e7 - 11644473600.0
|
||||
finally:
|
||||
_k.CloseHandle(proz)
|
||||
|
||||
|
||||
def nummer(raum):
|
||||
m = re.search(r"(\d{6,})\s*$", str(raum or ""))
|
||||
return m.group(1) if m else None
|
||||
|
||||
|
||||
def eingefroren(pfad):
|
||||
"""Endet das Tischlog mit einem Riss (danach keine einzige normale Zeile mehr)?"""
|
||||
try:
|
||||
with open(pfad, "rb") as f:
|
||||
f.seek(0, 2)
|
||||
f.seek(max(0, f.tell() - 65536))
|
||||
schwanz = f.read()
|
||||
except OSError:
|
||||
return False
|
||||
i = schwanz.rfind(mittler.RISS_MARKE)
|
||||
if i < 0:
|
||||
return False
|
||||
# Nach dem Riss steht nur die Ausnahme ("[ERROR] - Handled Exception", " at ..."), keine
|
||||
# neue gewoehnliche Zeitstempel-Zeile.
|
||||
danach = schwanz[i:].split(b"\n")[1:]
|
||||
return not any(z.startswith(b"[") and b"Exception" not in z for z in danach)
|
||||
|
||||
|
||||
def lage():
|
||||
"""[(nummer, raum, tisch_pid, mittler_pid, eingefroren)] fuer alle Tische mit Mittler."""
|
||||
mittlers = [(int(pid), cmd) for pid, cmd in winproc.prozesse()
|
||||
if "python" in cmd.lower() and "mittler.py" in cmd]
|
||||
raus = []
|
||||
for pid, raum, sock in mittler.spiele():
|
||||
n = nummer(raum)
|
||||
if not n or (sock or "") != mittler.unser_name(raum):
|
||||
continue
|
||||
mit = [m for m, cmd in mittlers if raum in cmd]
|
||||
if len(mit) != 1:
|
||||
continue # keiner oder mehrdeutig: nicht anfassen
|
||||
pfad = mittler.tischlog(raum)
|
||||
raus.append((n, raum, int(pid), mit[0], bool(pfad) and eingefroren(pfad)))
|
||||
return raus
|
||||
|
||||
|
||||
WACHE_TAKT_S = 10.0
|
||||
|
||||
|
||||
def wache(takt=WACHE_TAKT_S):
|
||||
"""Eingefrorene Tische alter Mittler (ohne eigene Riss-Wache) heilen, bis keiner mehr laeuft.
|
||||
Hoechstens RISS_HOECHSTENS Heilungen je Tisch in RISS_FENSTER_S - wie im Mittler selbst."""
|
||||
fix = os.path.getmtime(mittler.__file__) # neuer gestartete Mittler heilen sich selbst
|
||||
heilungen = {}
|
||||
while True:
|
||||
alte = 0
|
||||
for n, raum, tisch, mit, kalt in lage():
|
||||
beginn = startzeit(mit)
|
||||
if beginn is None or beginn >= fix:
|
||||
continue
|
||||
alte += 1
|
||||
if not kalt:
|
||||
continue
|
||||
jetzt = time.time()
|
||||
h = heilungen[n] = [t for t in heilungen.get(n, []) if jetzt - t < mittler.RISS_FENSTER_S]
|
||||
if len(h) >= mittler.RISS_HOECHSTENS:
|
||||
continue
|
||||
wert = spiel_griff(mit, tisch)
|
||||
if wert is None:
|
||||
continue
|
||||
schliessen(mit, wert)
|
||||
h.append(jetzt)
|
||||
print(time.strftime("%H:%M:%S"), f"{raum}: eingefroren - Spiel-Verbindung getrennt "
|
||||
f"(Mittler {mit}, Heilung {len(h)})", flush=True)
|
||||
if not alte:
|
||||
print(time.strftime("%H:%M:%S"), "kein alter Mittler mehr - Wache endet", flush=True)
|
||||
return
|
||||
time.sleep(takt)
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
ap.add_argument("nummern", nargs="*", help="nur diese Tischnummern (sonst alle eingefrorenen)")
|
||||
ap.add_argument("--heilen", action="store_true", help="wirklich trennen (sonst nur anzeigen)")
|
||||
ap.add_argument("--wache", action="store_true", help="dauernd heilen (nur Mittler von vor dem Fix)")
|
||||
a = ap.parse_args()
|
||||
if a.wache:
|
||||
return wache()
|
||||
for n, raum, tisch, mit, kalt in sorted(lage()):
|
||||
if a.nummern and n not in a.nummern:
|
||||
continue
|
||||
if not a.nummern and not kalt:
|
||||
print(f"{n} ok {raum}")
|
||||
continue
|
||||
wert = spiel_griff(mit, tisch)
|
||||
zeile = f"{n} {'EINGEFROREN' if kalt else 'gewaehlt '} {raum} Mittler {mit}, Tisch {tisch}, Griff {wert}"
|
||||
if wert is None:
|
||||
print(zeile + " - kein Spiel-Griff gefunden, nichts getan")
|
||||
continue
|
||||
if a.heilen:
|
||||
schliessen(mit, wert)
|
||||
zeile += " -> getrennt"
|
||||
print(zeile)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Erkennt hand/tisch_neu_verbinden.py einen eingefrorenen Tisch am Tischlog - und nur ihn?
|
||||
|
||||
Rein: nur Logdateien im Temp-Ordner, keine Pipe, kein Prozess. Nur unter Windows (ctypes-Griffe).
|
||||
|
||||
python tests/tisch_neu_verbinden_spec.py -v
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
HIER = os.path.dirname(os.path.abspath(__file__))
|
||||
WURZEL = os.path.dirname(HIER)
|
||||
for p in (os.path.join(WURZEL, "hand"), os.path.join(WURZEL, "lesen"), os.path.join(WURZEL, "client")):
|
||||
if p not in sys.path:
|
||||
sys.path.insert(0, p)
|
||||
|
||||
if sys.platform == "win32":
|
||||
import tisch_neu_verbinden as t # noqa: E402
|
||||
|
||||
# Wie im echten table_1781849.log vom 15.11. 22:18:32
|
||||
RISS = (b"[2026-11-15 22:17:26:647] [UNITY] -> Server:: Game Started\r\n"
|
||||
b"[2026-11-15 22:18:32:717] [PIP] -> Exception frames - start: Newtonsoft.Json.JsonTextReader.ParseValue\r\n"
|
||||
b"[2026-11-15 22:18:32:721] [ERROR] - Handled Exception : Newtonsoft.Json.JsonReaderException: Unexpected\r\n"
|
||||
b" at Newtonsoft.Json.JsonTextReader.ParseValue () [0x002b3] in <29e3>:0 \r\n"
|
||||
b" at PBNamedPipe.H.Pipes.PipeConnection`1[T].<Start>b__37_0 () [0x00141] in <772d>:0 \r\n\r\n")
|
||||
# Nach der Heilung: der Leser meldet das Trennen, danach laeuft der Tisch wieder
|
||||
GEHEILT = (b"[2026-11-15 22:41:20:192] [PIP] -> Exception frames - start: PBNamedPipe.H.Pipes.IO.PipeStreamReader\r\n"
|
||||
b"[2026-11-15 22:41:20:193] [ERROR] - Handled Exception : System.IO.IOException: Unable to read data.\r\n"
|
||||
b" at PBNamedPipe.H.Pipes.IO.PipeStreamReader.ReadAsync () [0x000f5] in <772d>:0 \r\n"
|
||||
b"[2026-11-15 22:41:20:288] [UNITY] -> FetchAppLanguageConfig\r\n")
|
||||
|
||||
|
||||
@unittest.skipUnless(sys.platform == "win32", "nur Windows")
|
||||
class Eingefroren(unittest.TestCase):
|
||||
def pruefe(self, inhalt):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
pfad = os.path.join(d, "table_1781849.log")
|
||||
with open(pfad, "wb") as f:
|
||||
f.write(inhalt)
|
||||
return t.eingefroren(pfad)
|
||||
|
||||
def test_riss_am_ende_ist_eingefroren(self):
|
||||
self.assertTrue(self.pruefe(RISS))
|
||||
|
||||
def test_nach_der_heilung_nicht_mehr(self):
|
||||
self.assertFalse(self.pruefe(RISS + GEHEILT))
|
||||
|
||||
def test_ohne_riss_und_ohne_log(self):
|
||||
self.assertFalse(self.pruefe(b"[2026-11-15 22:17:26:647] [UNITY] -> Server:: Game Started\r\n"))
|
||||
self.assertFalse(t.eingefroren(os.path.join(tempfile.gettempdir(), "gibt_es_nicht_4711.log")))
|
||||
|
||||
def test_nummer_aus_dem_raum(self):
|
||||
self.assertEqual(t.nummer("14R NL 0.01-0.02 EV-INRIT-(A) 1781849"), "1781849")
|
||||
self.assertIsNone(t.nummer("Klon"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user