e2e-atomic-json-file.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { randomUUID } from "node:crypto";
  2. import { mkdir, rename, rm, writeFile } from "node:fs/promises";
  3. import { dirname } from "node:path";
  4. interface E2EFileReplaceOperations {
  5. rename(from: string, to: string): Promise<void> | void;
  6. wait(delayMs: number): Promise<void>;
  7. }
  8. interface E2EAtomicJsonFileOperations extends E2EFileReplaceOperations {
  9. mkdir(path: string): Promise<void> | void;
  10. remove(path: string, options: { force: true }): Promise<void> | void;
  11. write(path: string, content: string): Promise<void> | void;
  12. }
  13. const nodeFileOperations: E2EAtomicJsonFileOperations = {
  14. mkdir: async (path) => {
  15. await mkdir(path, { recursive: true });
  16. },
  17. remove: async (path, options) => {
  18. await rm(path, options);
  19. },
  20. rename,
  21. wait: (delayMs) => new Promise((resolveWait) => setTimeout(resolveWait, delayMs)),
  22. write: async (path, content) => {
  23. await writeFile(path, content, "utf-8");
  24. },
  25. };
  26. const FILE_REPLACE_RETRY_ATTEMPTS = 20;
  27. const FILE_REPLACE_RETRY_DELAY_MS = 25;
  28. const FILE_REPLACE_ERROR_CODES = new Set(["EBUSY", "EACCES", "EPERM"]);
  29. export async function writeE2EJsonFileAtomically(
  30. path: string,
  31. value: unknown,
  32. operations: E2EAtomicJsonFileOperations = nodeFileOperations,
  33. ): Promise<void> {
  34. await operations.mkdir(dirname(path));
  35. const temporaryPath = `${path}.${process.pid}.${randomUUID()}.tmp`;
  36. try {
  37. // Bug 根因:capture server 直接 truncate 后重写 JSON 时,另一个 WDIO worker
  38. // 可能读到半截内容。先完整写临时文件再 rename,reader 只会看到完整版本。
  39. await operations.write(temporaryPath, JSON.stringify(value, null, 2));
  40. // Bug 根因:Windows 不允许 rename 覆盖仍被另一个 worker 短暂持有读句柄的
  41. // capture.json。同步 rename 一次失败会打断 replay handler,并让 provider socket
  42. // 永久 pending;这里仅对 Windows 文件占用错误做有界异步重试,避免阻塞事件循环。
  43. await replaceE2EFileWithRetry(temporaryPath, path, operations);
  44. } catch (error) {
  45. await operations.remove(temporaryPath, { force: true });
  46. throw error;
  47. }
  48. }
  49. export async function replaceE2EFileWithRetry(
  50. temporaryPath: string,
  51. path: string,
  52. operations: E2EFileReplaceOperations = nodeFileOperations,
  53. ): Promise<void> {
  54. for (let attempt = 1; attempt <= FILE_REPLACE_RETRY_ATTEMPTS; attempt += 1) {
  55. try {
  56. await operations.rename(temporaryPath, path);
  57. return;
  58. } catch (error) {
  59. if (!isE2EFileReplaceConflict(error) || attempt === FILE_REPLACE_RETRY_ATTEMPTS) {
  60. throw error;
  61. }
  62. await operations.wait(FILE_REPLACE_RETRY_DELAY_MS);
  63. }
  64. }
  65. }
  66. /** Windows 文件占用导致的替换冲突(EBUSY / EACCES / EPERM)。 */
  67. export function isE2EFileReplaceConflict(error: unknown): boolean {
  68. return (
  69. typeof error === "object" &&
  70. error !== null &&
  71. "code" in error &&
  72. typeof error.code === "string" &&
  73. FILE_REPLACE_ERROR_CODES.has(error.code)
  74. );
  75. }