记录无人值守部署实测: 6 个失败根因与自愈看门狗
在一台无人在场的远程 Windows 机器上从零部署并跑通镜牢, 记录六个真实失败: 会话 Disc 导致桌面不渲染、sinner_order 全 0 导致编队死循环、 set_win_size 未留标题栏导致窗口装不下、窗口非 16:9 导致模板匹配全面崩坏、 background_click 触发 SetForegroundWindow 崩溃、ps1 无 BOM 导致中文注释解析失败。 实测结果: 一局完整镜牢 00:39:28, exit 0, 零 [ERROR]; 单测 171 项通过。 新增 scripts/keep_display.ps1 看门狗: 会话掉到 Disc 时用提权 tscon 重新挂 console 恢复渲染, 无人连接也持续出图。只在 Disc 时动作, 有人在用不插手。 正常路径与修复路径均已实测验证。 另加 session_state.py (WTS API 查会话状态, 替代在计划任务里失灵 qwinsta) 与 render_check.py (BitBlt 渲染自检)。
This commit is contained in:
@@ -0,0 +1,77 @@
|
|||||||
|
# 看门狗: 会话掉回 "Disc" 时重新挂到物理控制台, 恢复桌面渲染。
|
||||||
|
#
|
||||||
|
# 背景: DSH / lc-mcp 常驻服务跑在会话 2。RDP 断开后 Windows 会拆掉该会话的
|
||||||
|
# 显示表面, 截图变全黑, AALC 直接失明。把会话挂到 console 后即使无人连接
|
||||||
|
# 也持续渲染。
|
||||||
|
#
|
||||||
|
# 只在会话状态为 Disc(无人连接) 时动作; 用户正通过 RDP/ToDesk 连着
|
||||||
|
# (Active/Conn) 时绝不插手, 否则会把用户挤下线。
|
||||||
|
#
|
||||||
|
# tscon 需要 SeTcbPrivilege, 普通令牌拿不到, 所以用 -Verb RunAs 提权。
|
||||||
|
# 本机 ConsentPromptBehaviorAdmin=0(从不通知), 提权静默通过, 不会弹框。
|
||||||
|
|
||||||
|
param(
|
||||||
|
[int]$SessionId = 2,
|
||||||
|
[string]$LogPath = "$env:USERPROFILE\lc_mcp\keep_display.log",
|
||||||
|
[switch]$Verbose,
|
||||||
|
[switch]$ForceDisc
|
||||||
|
)
|
||||||
|
|
||||||
|
function Write-Log([string]$msg) {
|
||||||
|
$dir = Split-Path -Parent $LogPath
|
||||||
|
if (-not (Test-Path $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null }
|
||||||
|
Add-Content -Path $LogPath -Value ("[{0}] {1}" -f (Get-Date -Format 'yyyy-MM-dd HH:mm:ss'), $msg) -Encoding UTF8
|
||||||
|
$all = @(Get-Content $LogPath -ErrorAction SilentlyContinue)
|
||||||
|
if ($all.Count -gt 400) { $all | Select-Object -Last 400 | Set-Content $LogPath -Encoding UTF8 }
|
||||||
|
}
|
||||||
|
|
||||||
|
# 会话状态: 用 WTS API (qwinsta 在计划任务上下文里拿不到输出)
|
||||||
|
$py = 'D:\lc-mcp\vendor\aalc\.venv\Scripts\python.exe'
|
||||||
|
$statePy = Join-Path $PSScriptRoot 'session_state.py'
|
||||||
|
if (-not (Test-Path $py) -or -not (Test-Path $statePy)) {
|
||||||
|
Write-Log "missing python ($py) or session_state.py ($statePy); abort"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
$state = (& $py -X utf8 $statePy $SessionId 2>&1 | Out-String).Trim()
|
||||||
|
if ($state -notin @('Active', 'Connected', 'Disc', 'Idle')) {
|
||||||
|
Write-Log "session $SessionId state unreadable ('$state'); abort"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($ForceDisc) {
|
||||||
|
$state = 'Disc'
|
||||||
|
Write-Log "ForceDisc: skipping state check (real state was '$state')"
|
||||||
|
} elseif ($state -ne 'Disc') {
|
||||||
|
if ($Verbose) { Write-Log "session $SessionId is $state; nothing to do" }
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Log "session $SessionId is Disc -> re-attaching to console via elevated tscon"
|
||||||
|
$out = Join-Path $env:TEMP 'lc_keep_display_tscon.txt'
|
||||||
|
Remove-Item $out -ErrorAction SilentlyContinue
|
||||||
|
# 显式记录退出码: 只看有没有输出文件分不清"成功但无输出"和"根本没跑"
|
||||||
|
$inner = "tscon $SessionId /dest:console > `"$out`" 2>&1; Add-Content `"$out`" ('EXIT=' + `$LASTEXITCODE)"
|
||||||
|
try {
|
||||||
|
Start-Process -FilePath "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe" `
|
||||||
|
-ArgumentList @('-NoProfile', '-WindowStyle', 'Hidden', '-Command', $inner) `
|
||||||
|
-Verb RunAs -WindowStyle Hidden -Wait -ErrorAction Stop
|
||||||
|
} catch {
|
||||||
|
Write-Log "elevated tscon launch FAILED: $($_.Exception.Message)"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
Start-Sleep -Seconds 2
|
||||||
|
$tsconOut = if (Test-Path $out) { ((Get-Content $out -Raw) -replace '\s+', ' ').Trim() } else { '(no output file)' }
|
||||||
|
$after = (& $py -X utf8 $statePy $SessionId 2>&1 | Out-String).Trim()
|
||||||
|
Write-Log "tscon said: $tsconOut | session $SessionId now: $after"
|
||||||
|
|
||||||
|
# 渲染自检: BitBlt 屏幕 DC 成功 = 桌面在出图
|
||||||
|
$renderPy = Join-Path $PSScriptRoot 'render_check.py'
|
||||||
|
if (Test-Path $renderPy) {
|
||||||
|
$rc = (& $py -X utf8 $renderPy 2>&1 | Out-String).Trim()
|
||||||
|
Write-Log "render check: $rc"
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($after -ne 'Active' -and $after -ne 'Connected') {
|
||||||
|
Write-Log "WARN: session $SessionId still '$after' after tscon"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""桌面渲染自检: 从屏幕 DC 做一次 BitBlt。
|
||||||
|
|
||||||
|
成功 = 桌面正在出图(会话处于渲染状态);
|
||||||
|
失败(err 6 ERROR_INVALID_HANDLE) = 会话被断开, Windows 拆掉了显示表面,
|
||||||
|
此时游戏截图会全黑, AALC 会失明。
|
||||||
|
|
||||||
|
退出码: 0 渲染正常, 1 渲染不可用
|
||||||
|
"""
|
||||||
|
import ctypes
|
||||||
|
|
||||||
|
SRCCOPY = 0x00CC0020
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
u = ctypes.windll.user32
|
||||||
|
g = ctypes.windll.gdi32
|
||||||
|
hdc = u.GetDC(0)
|
||||||
|
mem = g.CreateCompatibleDC(hdc)
|
||||||
|
bmp = g.CreateCompatibleBitmap(hdc, 64, 64)
|
||||||
|
g.SelectObject(mem, bmp)
|
||||||
|
r = g.BitBlt(mem, 0, 0, 64, 64, hdc, 0, 0, SRCCOPY)
|
||||||
|
g.DeleteObject(bmp)
|
||||||
|
g.DeleteDC(mem)
|
||||||
|
u.ReleaseDC(0, hdc)
|
||||||
|
print('BitBlt=%d desktop=%dx%d' % (r, u.GetSystemMetrics(0), u.GetSystemMetrics(1)))
|
||||||
|
return 0 if r else 1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
raise SystemExit(main())
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""打印指定 Windows 终端服务会话的连接状态。
|
||||||
|
|
||||||
|
qwinsta 在计划任务/隐藏窗口上下文里拿不到输出, 所以用 WTS API。
|
||||||
|
|
||||||
|
用法: python session_state.py [session_id]
|
||||||
|
输出: Active / Connected / Disc / Idle / Listen / ... 或 UNKNOWN
|
||||||
|
退出码: 0 成功, 1 查询失败
|
||||||
|
"""
|
||||||
|
import ctypes
|
||||||
|
import sys
|
||||||
|
from ctypes import wintypes
|
||||||
|
|
||||||
|
WTS_CURRENT_SERVER_HANDLE = 0
|
||||||
|
WTS_CONNECT_STATE = 8
|
||||||
|
|
||||||
|
WTS_STATES = {
|
||||||
|
0: 'Active',
|
||||||
|
1: 'Connected',
|
||||||
|
2: 'ConnectQuery',
|
||||||
|
3: 'Shadow',
|
||||||
|
4: 'Disc',
|
||||||
|
5: 'Idle',
|
||||||
|
6: 'Listen',
|
||||||
|
7: 'Reset',
|
||||||
|
8: 'Down',
|
||||||
|
9: 'Init',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv):
|
||||||
|
session_id = int(argv[1]) if len(argv) > 1 else 2
|
||||||
|
wts = ctypes.WinDLL('wtsapi32')
|
||||||
|
wts.WTSQuerySessionInformationW.argtypes = [
|
||||||
|
wintypes.HANDLE, wintypes.DWORD, ctypes.c_int,
|
||||||
|
ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(wintypes.DWORD),
|
||||||
|
]
|
||||||
|
wts.WTSQuerySessionInformationW.restype = wintypes.BOOL
|
||||||
|
wts.WTSFreeMemory.argtypes = [ctypes.c_void_p]
|
||||||
|
|
||||||
|
buf = ctypes.c_void_p()
|
||||||
|
size = wintypes.DWORD()
|
||||||
|
ok = wts.WTSQuerySessionInformationW(
|
||||||
|
WTS_CURRENT_SERVER_HANDLE, session_id, WTS_CONNECT_STATE,
|
||||||
|
ctypes.byref(buf), ctypes.byref(size))
|
||||||
|
if not ok or not buf:
|
||||||
|
print('UNKNOWN')
|
||||||
|
return 1
|
||||||
|
try:
|
||||||
|
state = ctypes.cast(buf, ctypes.POINTER(ctypes.c_uint)).contents.value
|
||||||
|
finally:
|
||||||
|
wts.WTSFreeMemory(buf)
|
||||||
|
print(WTS_STATES.get(state, 'Unknown%d' % state))
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main(sys.argv))
|
||||||
Reference in New Issue
Block a user