conversation-session-remote-zombie-lock-docker.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { mkdir, writeFile } from "node:fs/promises";
  2. import { resolve } from "node:path";
  3. import { readFileSync } from "node:fs";
  4. import { TID_DOCKER_CONTAINER_INPUT, TID_REMOTE_KIND_DOCKER } from "@zcode/shared/test-ids";
  5. import { readLastConnectRemoteError } from "#e2e/helpers/connect-remote-error-log";
  6. import { clearAppData, clickTestIdByDom } from "#e2e/helpers/desktop-app";
  7. import {
  8. execInRemoteContainer,
  9. readRemoteDockerContainer,
  10. } from "#e2e/helpers/remote-docker-target";
  11. import {
  12. installAndStartRemoteDaemon,
  13. isRemotePidAlive,
  14. killRemoteDaemon,
  15. pointStaleDaemonFilesAt,
  16. readRemoteDaemonFiles,
  17. readRemoteProcessIdentity,
  18. spawnZombieProcess,
  19. type RemoteAccount,
  20. } from "#e2e/helpers/remote-restart-stale-daemon";
  21. import {
  22. clickLastEnabledSSHDialogButton,
  23. openRemoteProjectSelection,
  24. waitForSSHDirectoryStep,
  25. } from "#e2e/helpers/ssh-remote-p0";
  26. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  27. // R3-ISSUE-034(ZCT-2117275161536565248)的真实 Docker transport 验证(本机 Docker runner):容器 PID 1
  28. // 不回收子进程时,Supervisor 与 Core 非正常死亡后成为僵尸,/proc/<pid>/stat 仍在、starttime 不变。
  29. // 残留锁指向僵尸 Supervisor(身份一致),ready 快照指向僵尸 Core,probe 的 kill -0 判定存活并复用旧端口;
  30. // 握手失败后 Desktop 执行 serve --daemon,新 Supervisor 必须把僵尸锁判为 stale 并接管,进入目录选择,
  31. // 不能报 "daemon exited before ready (code=1)"。runner 的 PID 1 是会回收子进程的 sshd,用例让不回收子进程的
  32. // sleep 做僵尸的父进程,锁与快照的字段形状与真实僵尸 Supervisor/Core 相同。
  33. const MARKER = "E2E_REMOTE_ZOMBIE_LOCK_DOCKER";
  34. // Desktop 的 Docker transport 以容器默认用户执行,本镜像为 root。
  35. const ROOT: RemoteAccount = { user: "root", home: "/root" };
  36. const desktopRoot = process.cwd();
  37. const artifactDir = resolve(
  38. process.env.ZCODE_E2E_ARTIFACT_DIR ?? ".e2e-artifacts/remote-zombie-lock-docker",
  39. );
  40. async function readProcessState(container: string, pid: number): Promise<string> {
  41. return (
  42. await execInRemoteContainer(
  43. container,
  44. ["sh", "-c", `awk '{print $3}' /proc/${pid}/stat 2>/dev/null || echo gone`],
  45. { user: "0" },
  46. )
  47. ).stdout.trim();
  48. }
  49. describe(`${MARKER}: 僵尸持锁时 Docker 连接接管残留锁`, () => {
  50. const facts: Record<string, unknown> = {};
  51. let container: string | undefined;
  52. let zombiePid: number | undefined;
  53. after(async () => {
  54. // 结束僵尸的父进程,僵尸转交给 PID 1(sshd)回收,不留给同一容器的后续用例。
  55. if (container && zombiePid) {
  56. await execInRemoteContainer(
  57. container,
  58. ["sh", "-c", `kill -9 "$(awk '{print $4}' /proc/${zombiePid}/stat)" 2>/dev/null || true`],
  59. { user: "0" },
  60. ).catch(() => undefined);
  61. }
  62. await mkdir(artifactDir, { recursive: true });
  63. await writeFile(
  64. resolve(artifactDir, "remote-zombie-lock-docker.json"),
  65. JSON.stringify(facts, null, 2),
  66. );
  67. await browser.electron.restoreAllMocks();
  68. await clearAppData();
  69. });
  70. it("锁的持有者与快照的 Core 都是僵尸时,serve --daemon 接管残留锁并进入目录选择", async function () {
  71. this.timeout(15 * 60_000);
  72. container = readRemoteDockerContainer(MARKER);
  73. const appVersion = (
  74. JSON.parse(readFileSync(resolve(desktopRoot, "../../package.json"), "utf8")) as {
  75. version: string;
  76. }
  77. ).version;
  78. await prepareV4ConversationE2E({ skipProvider: true });
  79. await installAndStartRemoteDaemon(container, ROOT, { desktopRoot, appVersion });
  80. const beforeKill = await readRemoteDaemonFiles(container, ROOT);
  81. facts.beforeKill = beforeKill;
  82. expect(beforeKill.status).toMatchObject({ state: "ready" });
  83. await killRemoteDaemon(container, ROOT);
  84. zombiePid = await spawnZombieProcess(container, ROOT);
  85. const zombieIdentity = await readRemoteProcessIdentity(container, ROOT, zombiePid);
  86. await pointStaleDaemonFilesAt(container, ROOT, {
  87. lockPid: zombiePid,
  88. lockIdentity: zombieIdentity,
  89. statusPid: zombiePid,
  90. });
  91. const staleFiles = await readRemoteDaemonFiles(container, ROOT);
  92. facts.zombiePid = zombiePid;
  93. facts.zombieIdentity = zombieIdentity;
  94. facts.staleFiles = staleFiles;
  95. expect(await readProcessState(container, zombiePid)).toBe("Z");
  96. expect(staleFiles.lock).toMatchObject({ pid: zombiePid, processIdentity: zombieIdentity });
  97. expect(staleFiles.status).toMatchObject({ state: "ready", pid: zombiePid });
  98. // kill -0 对僵尸同样成功:probe 会复用快照里的旧端口,握手失败后才回退 serve --daemon。
  99. expect(await isRemotePidAlive(container, ROOT, zombiePid)).toBe(true);
  100. await openRemoteProjectSelection();
  101. await clickTestIdByDom(TID_REMOTE_KIND_DOCKER, {
  102. timeout: 30_000,
  103. timeoutMsg: `${MARKER}: 无法选择 Docker transport`,
  104. });
  105. const input = $(`[data-testid="${TID_DOCKER_CONTAINER_INPUT}"]`);
  106. await input.waitForDisplayed({ timeout: 30_000 });
  107. await input.setValue(container);
  108. await clickLastEnabledSSHDialogButton(MARKER);
  109. let connectError: unknown;
  110. try {
  111. facts.flow = await waitForSSHDirectoryStep(MARKER);
  112. } catch (error) {
  113. connectError = error;
  114. facts.connectError = error instanceof Error ? error.message : String(error);
  115. facts.rawError = await readLastConnectRemoteError();
  116. }
  117. const afterConnect = await readRemoteDaemonFiles(container, ROOT);
  118. facts.afterConnect = afterConnect;
  119. facts.zombieStateAfterConnect = await readProcessState(container, zombiePid);
  120. if (connectError) throw connectError;
  121. expect(facts.flow).toBe("directory");
  122. expect(afterConnect.lock?.pid).not.toBe(zombiePid);
  123. expect(typeof afterConnect.lock?.processIdentity).toBe("string");
  124. expect(await isRemotePidAlive(container, ROOT, afterConnect.lock?.pid as number)).toBe(true);
  125. expect(afterConnect.status).toMatchObject({ state: "ready" });
  126. expect(afterConnect.status?.pid).not.toBe(zombiePid);
  127. // 接管只换掉锁文件,不碰僵尸及其父进程。
  128. expect(facts.zombieStateAfterConnect).toBe("Z");
  129. });
  130. });