watched-source-failures.test.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. import { EventEmitter } from "node:events";
  2. import { mkdir, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import type { Logger } from "@zcode/contracts";
  6. import { afterEach, beforeEach, expect, it, vi } from "vitest";
  7. import {
  8. WATCH_BIND_TIMEOUT_MS,
  9. createRefcountedRegistry,
  10. createWatchedEntry,
  11. type WatchedEntry,
  12. type WatchedLoad,
  13. } from "../src/watch/watched-source.js";
  14. /**
  15. * 修复前就绪后报错立即重绑、新 watcher 又报错时无上限循环,flush 等不到 drain 结束,
  16. * 新会话创建被拖住(Windows 网络共享路径反复报 `UNKNOWN: unknown error, watch`)。
  17. * mock 按用例配置让 watcher 在就绪前或就绪后报错。
  18. *
  19. * 修复前网盘响应慢时,bind 等锚点 stat 和 watcher ready/error 都没有上限:
  20. * watcher 既不就绪也不报错("never"),或 stat 挂住(hungStats),条目创建一直不返回。
  21. * slowStats 模拟网盘慢但没挂住:多个目标共用一个锚点时,逐目标 stat 的累计等待会超过截止时间。
  22. */
  23. type Behavior = "ok" | "never" | "error-before-ready" | "error-after-ready" | "event-then-error";
  24. const state = vi.hoisted(() => ({
  25. behavior: "ok" as Behavior,
  26. watchers: [] as Array<EventEmitter & { anchor: string; closed: boolean }>,
  27. /** 路径 → 放行几次后挂住(0 为一开始就挂住);挂住的 stat 等用例调用 releaseStats。 */
  28. hungStats: new Map<string, number>(),
  29. /** 路径 → 每次 stat 延迟的毫秒数。 */
  30. slowStats: new Map<string, number>(),
  31. /** 挂住的 stat:放行时用真实 stat 结果兑现,模拟网盘迟到的响应。 */
  32. pendingStats: [] as Array<() => void>,
  33. statCalls: [] as string[],
  34. }));
  35. vi.mock("node:fs/promises", async (importOriginal) => {
  36. const actual = await importOriginal<typeof import("node:fs/promises")>();
  37. return {
  38. ...actual,
  39. stat: (path: string) => {
  40. state.statCalls.push(path);
  41. const calls = state.statCalls.filter((called) => called === path).length;
  42. const passes = state.hungStats.get(path);
  43. if (passes !== undefined && calls > passes)
  44. return new Promise((resolve, reject) => {
  45. state.pendingStats.push(() => actual.stat(path).then(resolve, reject));
  46. });
  47. const delay = state.slowStats.get(path);
  48. if (delay === undefined) return actual.stat(path);
  49. return new Promise((resolve) => setTimeout(resolve, delay)).then(() => actual.stat(path));
  50. },
  51. };
  52. });
  53. vi.mock("chokidar", () => ({
  54. watch: (anchor: string) => {
  55. const watcher = Object.assign(new EventEmitter(), {
  56. anchor,
  57. closed: false,
  58. close: vi.fn(async () => {
  59. watcher.closed = true;
  60. }),
  61. });
  62. state.watchers.push(watcher);
  63. const behavior = state.behavior;
  64. if (behavior === "never") return watcher;
  65. const fail = () => watcher.emit("error", watchFailure());
  66. if (behavior === "error-before-ready") {
  67. queueMicrotask(fail);
  68. return watcher;
  69. }
  70. queueMicrotask(() => {
  71. watcher.emit("ready");
  72. // 每次先发一个有效事件再报错:watcher 还没稳定,事件不能清零失败记录。
  73. if (behavior === "event-then-error")
  74. watcher.emit("raw", "change", CONFIG_NAME, { watchedPath: anchor });
  75. });
  76. if (behavior !== "ok") setImmediate(fail);
  77. return watcher;
  78. },
  79. }));
  80. const CONFIG_NAME = "config.json";
  81. const STOP_MESSAGE = "MCP configuration watcher stopped after repeated failures";
  82. const TIMEOUT_MESSAGE = "MCP configuration watcher bind timed out";
  83. /** 注入的短截止时间,让用例快;默认值另有一个用例按生产配置验证。 */
  84. const SHORT_BIND_TIMEOUT_MS = 200;
  85. /** 测试判定用的有限等待:修复前条目创建或 flush 不会结束。 */
  86. const SETTLE_LIMIT_MS = 2_000;
  87. type Loaded = WatchedLoad & { value: string };
  88. let root: string;
  89. let config: string;
  90. beforeEach(async () => {
  91. root = await mkdtemp(join(tmpdir(), "watched-source-failures-"));
  92. config = join(root, CONFIG_NAME);
  93. await writeFile(config, "v1");
  94. state.behavior = "ok";
  95. state.watchers = [];
  96. state.hungStats.clear();
  97. state.slowStats.clear();
  98. state.pendingStats = [];
  99. state.statCalls = [];
  100. });
  101. afterEach(async () => {
  102. state.behavior = "ok";
  103. state.hungStats.clear();
  104. releaseStats();
  105. await rm(root, { recursive: true, force: true });
  106. });
  107. function releaseStats(): void {
  108. for (const release of state.pendingStats.splice(0)) release();
  109. }
  110. function watchFailure(): Error {
  111. return Object.assign(new Error("UNKNOWN: unknown error, watch"), { code: "UNKNOWN" });
  112. }
  113. function loggerOf() {
  114. const warn = vi.fn();
  115. const logger: Logger = {
  116. debug: vi.fn(),
  117. info: vi.fn(),
  118. warn,
  119. error: vi.fn(),
  120. child: () => logger,
  121. };
  122. return { logger, warn };
  123. }
  124. function load(): Promise<Loaded> {
  125. return readFile(config, "utf8").then((value) => ({
  126. value,
  127. files: new Set([config]),
  128. commit: () => {},
  129. }));
  130. }
  131. function anchored(anchor: string) {
  132. return state.watchers.filter((watcher) => watcher.anchor === anchor);
  133. }
  134. async function settlesWithin<T>(promise: Promise<T>): Promise<boolean> {
  135. let timer: ReturnType<typeof setTimeout> | undefined;
  136. const limit = new Promise<false>((resolve) => {
  137. timer = setTimeout(() => resolve(false), SETTLE_LIMIT_MS);
  138. });
  139. try {
  140. return await Promise.race([promise.then(() => true), limit]);
  141. } finally {
  142. clearTimeout(timer);
  143. }
  144. }
  145. it.each(["error-after-ready", "event-then-error"] as const)(
  146. "stops rebinding after two failures (%s) so entry creation and flush return",
  147. async (behavior) => {
  148. state.behavior = behavior;
  149. const { logger, warn } = loggerOf();
  150. const pending = (async () => {
  151. const entry = await createWatchedEntry({
  152. load,
  153. bounds: [root],
  154. label: "MCP configuration",
  155. logger,
  156. });
  157. await entry.flush();
  158. return entry;
  159. })();
  160. try {
  161. expect(await settlesWithin(pending)).toBe(true);
  162. const entry = await pending;
  163. const watchers = state.watchers.length;
  164. // 第一次就绪后报错重绑一次,第二次停止:恰好 2 个,失败一次就停的退化也会被拦下。
  165. expect(watchers).toBe(2);
  166. // 停止监听后 flush 仍全量重读:新会话读到最新配置,且不再新建 watcher。
  167. await writeFile(config, "v2");
  168. await entry.flush();
  169. expect(entry.current().value).toBe("v2");
  170. expect(state.watchers).toHaveLength(watchers);
  171. expect(warn).toHaveBeenCalledWith(STOP_MESSAGE, expect.objectContaining({ path: root }));
  172. } finally {
  173. // 修复前的循环要等后续 watcher 成功才收敛;放开后再关闭条目,不把循环留给后面的用例。
  174. state.behavior = "ok";
  175. (await pending).close();
  176. }
  177. },
  178. );
  179. it("stops after two failures before ready and later flushes create no watcher", async () => {
  180. state.behavior = "error-before-ready";
  181. const { logger, warn } = loggerOf();
  182. const entry = await createWatchedEntry({
  183. load,
  184. bounds: [root],
  185. label: "MCP configuration",
  186. logger,
  187. });
  188. try {
  189. expect(anchored(root)).toHaveLength(2);
  190. expect(warn).toHaveBeenCalledWith(STOP_MESSAGE, expect.objectContaining({ path: root }));
  191. // 修复前就绪前报错虽不调度,但每次读盘都重建一个必然失败的 watcher。
  192. await entry.flush();
  193. await entry.flush();
  194. expect(anchored(root)).toHaveLength(2);
  195. } finally {
  196. entry.close();
  197. }
  198. });
  199. it("recovers from a single failure once the stable watcher saw a file event", async () => {
  200. const { logger, warn } = loggerOf();
  201. const entry = await createWatchedEntry({
  202. load,
  203. bounds: [root],
  204. label: "MCP configuration",
  205. logger,
  206. });
  207. try {
  208. const first = state.watchers.at(-1)!;
  209. first.emit("error", watchFailure());
  210. await entry.flush();
  211. const second = state.watchers.at(-1)!;
  212. expect(second).not.toBe(first);
  213. expect(second.closed).toBe(false);
  214. // 经历过一次读盘提交后收到有效事件,说明监听已恢复,失败记录清零。
  215. second.emit("raw", "change", CONFIG_NAME, { watchedPath: root });
  216. second.emit("error", watchFailure());
  217. await entry.flush();
  218. const third = state.watchers.at(-1)!;
  219. expect(third).not.toBe(second);
  220. expect(third.closed).toBe(false);
  221. expect(warn).not.toHaveBeenCalledWith(STOP_MESSAGE, expect.anything());
  222. // 没有事件就再次报错,算同一身份的第二次失败。
  223. third.emit("error", watchFailure());
  224. await entry.flush();
  225. expect(state.watchers.at(-1)).toBe(third);
  226. expect(warn).toHaveBeenCalledWith(STOP_MESSAGE, expect.objectContaining({ path: root }));
  227. } finally {
  228. entry.close();
  229. }
  230. });
  231. it("watches a recreated directory again after its old identity stopped", async () => {
  232. state.behavior = "error-before-ready";
  233. const skills = join(root, "skills");
  234. await mkdir(skills);
  235. const entry = await createWatchedEntry({
  236. load: async () => ({ files: new Set<string>(), directories: new Set([skills]), commit() {} }),
  237. bounds: [root],
  238. childName: () => true,
  239. label: "Skill catalog",
  240. });
  241. try {
  242. expect(anchored(skills)).toHaveLength(2);
  243. // 先建新目录再替换,保证 inode 不同;删除后立即重建可能复用同一个 inode。
  244. const replacement = join(root, "skills.next");
  245. await mkdir(replacement);
  246. await rm(skills, { recursive: true });
  247. await rename(replacement, skills);
  248. state.behavior = "ok";
  249. await entry.flush();
  250. expect(anchored(skills)).toHaveLength(3);
  251. expect(anchored(skills).at(-1)!.closed).toBe(false);
  252. } finally {
  253. entry.close();
  254. }
  255. });
  256. it("retries watching after every view is released and the entry is created again", async () => {
  257. state.behavior = "error-before-ready";
  258. const { logger } = loggerOf();
  259. const registry = createRefcountedRegistry<
  260. { root: string },
  261. WatchedEntry<Loaded>,
  262. { entry: WatchedEntry<Loaded>; release: () => void }
  263. >({
  264. label: "MCP configuration",
  265. key: (options) => options.root,
  266. create: () => createWatchedEntry({ load, bounds: [root], label: "MCP configuration", logger }),
  267. view: (entry, _options, release) => ({ entry, release }),
  268. });
  269. try {
  270. const first = await registry.acquire({ root });
  271. expect(anchored(root)).toHaveLength(2);
  272. // 复用条目时 flush 仍全量重读:停止监听的目录,新会话也读到最新配置。
  273. await writeFile(config, "v2");
  274. const second = await registry.acquire({ root });
  275. expect(second.entry).toBe(first.entry);
  276. expect(second.entry.current().value).toBe("v2");
  277. expect(anchored(root)).toHaveLength(2);
  278. // 失败记录跟随条目;最后一个 view 释放后条目关闭,重建时重新尝试监听。
  279. first.release();
  280. second.release();
  281. state.behavior = "ok";
  282. const third = await registry.acquire({ root });
  283. expect(third.entry).not.toBe(first.entry);
  284. expect(anchored(root)).toHaveLength(3);
  285. expect(anchored(root).at(-1)!.closed).toBe(false);
  286. third.release();
  287. } finally {
  288. registry.close();
  289. }
  290. });
  291. /** 计时等条目创建;修复前不会返回,有限等待判定失败,不把挂住的条目留给后面的用例。 */
  292. async function timedCreate(options: Parameters<typeof createWatchedEntry<Loaded>>[0]) {
  293. const started = performance.now();
  294. const pending = createWatchedEntry(options).then((entry) => ({
  295. entry,
  296. elapsed: performance.now() - started,
  297. }));
  298. expect(await settlesWithin(pending)).toBe(true);
  299. return pending;
  300. }
  301. it("trips the breaker when a watcher never becomes ready within the default deadline", async () => {
  302. state.behavior = "never";
  303. const { logger, warn } = loggerOf();
  304. const { entry, elapsed } = await timedCreate({
  305. load,
  306. bounds: [root],
  307. label: "MCP configuration",
  308. logger,
  309. });
  310. try {
  311. // 生产不注入:一次绑定最多多等约 1 秒。
  312. expect(elapsed).toBeGreaterThanOrEqual(WATCH_BIND_TIMEOUT_MS - 50);
  313. expect(elapsed).toBeLessThan(WATCH_BIND_TIMEOUT_MS * 2);
  314. expect(warn).toHaveBeenCalledWith(TIMEOUT_MESSAGE, expect.objectContaining({ path: root }));
  315. expect(warn).toHaveBeenCalledTimes(1);
  316. expect(anchored(root)).toHaveLength(1);
  317. expect(anchored(root)[0]!.closed).toBe(true);
  318. // 断路后 flush 不再建 watcher,仍全量重读:新会话读到最新配置。
  319. await writeFile(config, "v2");
  320. await entry.flush();
  321. expect(entry.current().value).toBe("v2");
  322. expect(anchored(root)).toHaveLength(1);
  323. expect(warn).toHaveBeenCalledTimes(1);
  324. } finally {
  325. entry.close();
  326. }
  327. });
  328. it("trips the breaker when stat of an anchor hangs and never stats it again", async () => {
  329. state.hungStats.set(root, 0);
  330. const { logger, warn } = loggerOf();
  331. const { entry, elapsed } = await timedCreate({
  332. load,
  333. bounds: [root],
  334. label: "MCP configuration",
  335. logger,
  336. bindTimeoutMs: SHORT_BIND_TIMEOUT_MS,
  337. });
  338. try {
  339. expect(elapsed).toBeLessThan(SHORT_BIND_TIMEOUT_MS * 2);
  340. expect(warn).toHaveBeenCalledWith(TIMEOUT_MESSAGE, expect.objectContaining({ path: root }));
  341. expect(state.statCalls.filter((path) => path === root)).toHaveLength(1);
  342. expect(state.watchers).toHaveLength(0);
  343. // 迟到的 stat 结果被丢弃;之后的绑定跳过超时标记的锚点,不再 stat、不建 watcher。
  344. releaseStats();
  345. await writeFile(config, "v2");
  346. await entry.flush();
  347. expect(entry.current().value).toBe("v2");
  348. expect(state.statCalls.filter((path) => path === root)).toHaveLength(1);
  349. expect(state.watchers).toHaveLength(0);
  350. expect(warn).toHaveBeenCalledTimes(1);
  351. } finally {
  352. entry.close();
  353. }
  354. });
  355. it("bounds the total wait by one deadline when several anchors hang", async () => {
  356. state.behavior = "never";
  357. const names = ["a", "b", "c", "d", "e"];
  358. const skills = names.map((name) => join(root, name));
  359. for (const directory of skills) await mkdir(directory);
  360. const { logger, warn } = loggerOf();
  361. const { entry, elapsed } = await timedCreate({
  362. load: async () => ({
  363. value: "",
  364. files: new Set<string>(),
  365. directories: new Set(skills),
  366. commit() {},
  367. }),
  368. bounds: [root],
  369. childName: () => true,
  370. label: "MCP configuration",
  371. logger,
  372. bindTimeoutMs: SHORT_BIND_TIMEOUT_MS,
  373. });
  374. try {
  375. // 所有锚点共用一个截止时间:总等待不随锚点数累加(5 个锚点逐个等满会是 5 倍)。
  376. expect(elapsed).toBeLessThan(SHORT_BIND_TIMEOUT_MS * 2);
  377. for (const directory of skills)
  378. expect(warn).toHaveBeenCalledWith(
  379. TIMEOUT_MESSAGE,
  380. expect.objectContaining({ path: directory }),
  381. );
  382. expect(warn).toHaveBeenCalledTimes(skills.length);
  383. expect(state.watchers.every((watcher) => watcher.closed)).toBe(true);
  384. const created = state.watchers.length;
  385. await entry.flush();
  386. expect(state.watchers).toHaveLength(created);
  387. } finally {
  388. entry.close();
  389. }
  390. });
  391. it("keeps live updates for directories that bind before the deadline", async () => {
  392. const { logger, warn } = loggerOf();
  393. const { entry } = await timedCreate({
  394. load,
  395. bounds: [root],
  396. label: "MCP configuration",
  397. logger,
  398. bindTimeoutMs: SHORT_BIND_TIMEOUT_MS,
  399. });
  400. try {
  401. const watcher = anchored(root).at(-1)!;
  402. expect(watcher.closed).toBe(false);
  403. const refreshed = new Promise<void>((resolve) => entry.listen(resolve));
  404. await writeFile(config, "v2");
  405. watcher.emit("raw", "change", CONFIG_NAME, { watchedPath: root });
  406. expect(await settlesWithin(refreshed)).toBe(true);
  407. expect(entry.current().value).toBe("v2");
  408. expect(anchored(root)).toHaveLength(1);
  409. expect(warn).not.toHaveBeenCalled();
  410. } finally {
  411. entry.close();
  412. }
  413. });
  414. it("keeps healthy watchers that the deadline skipped when another anchor's stat hangs", async () => {
  415. const slow = join(root, "a");
  416. const healthy = join(root, "b");
  417. for (const directory of [slow, healthy]) await mkdir(directory);
  418. const { logger, warn } = loggerOf();
  419. const { entry } = await timedCreate({
  420. load: async () => ({
  421. value: "",
  422. files: new Set<string>(),
  423. directories: new Set([slow, healthy]),
  424. commit() {},
  425. }),
  426. bounds: [root],
  427. childName: () => true,
  428. label: "MCP configuration",
  429. logger,
  430. bindTimeoutMs: SHORT_BIND_TIMEOUT_MS,
  431. });
  432. try {
  433. expect(anchored(slow)).toHaveLength(1);
  434. expect(anchored(healthy)).toHaveLength(1);
  435. // 两个目录都已稳定监听后,a 的 stat 开始挂住;a 排在 b 前面,b 本轮来不及 stat。
  436. state.hungStats.set(slow, state.statCalls.filter((path) => path === slow).length);
  437. await entry.flush();
  438. // 修复前截止时间一过,b 也被断路、正常的 watcher 被关掉,之后不再监听。
  439. expect(anchored(slow)[0]!.closed).toBe(true);
  440. expect(anchored(healthy)).toHaveLength(1);
  441. expect(anchored(healthy)[0]!.closed).toBe(false);
  442. expect(warn).toHaveBeenCalledTimes(1);
  443. expect(warn).toHaveBeenCalledWith(TIMEOUT_MESSAGE, expect.objectContaining({ path: slow }));
  444. // 下一轮:卡住的 a 不再 stat,b 照常 stat 并继续监听。
  445. await entry.flush();
  446. expect(anchored(healthy)).toHaveLength(1);
  447. expect(anchored(healthy)[0]!.closed).toBe(false);
  448. expect(warn).toHaveBeenCalledTimes(1);
  449. } finally {
  450. entry.close();
  451. }
  452. });
  453. /** 登记同一目录下的多个配置文件:它们都锚在 root 上(项目级配置文件通常都锚在 workspace 上)。 */
  454. function loadSharingAnchor(): Promise<Loaded> {
  455. return load().then((loaded) => ({
  456. ...loaded,
  457. files: new Set([config, join(root, "b.json"), join(root, "c.json"), join(root, "d.json")]),
  458. }));
  459. }
  460. function rootStats(): number {
  461. return state.statCalls.filter((path) => path === root).length;
  462. }
  463. it("stats a shared anchor once per bind so a slow but healthy directory keeps watching", async () => {
  464. // 单次 stat 小于截止时间,4 个目标逐个 stat 的累计等待超过截止时间。
  465. state.slowStats.set(root, SHORT_BIND_TIMEOUT_MS * 0.3);
  466. const { logger, warn } = loggerOf();
  467. const { entry } = await timedCreate({
  468. load: loadSharingAnchor,
  469. bounds: [root],
  470. label: "MCP configuration",
  471. logger,
  472. bindTimeoutMs: SHORT_BIND_TIMEOUT_MS,
  473. });
  474. try {
  475. // 修复前同一锚点逐目标 stat,第 4 次超时,健康的目录被断路。
  476. expect(warn).not.toHaveBeenCalled();
  477. expect(anchored(root)).toHaveLength(1);
  478. expect(anchored(root)[0]!.closed).toBe(false);
  479. const before = rootStats();
  480. await entry.flush();
  481. expect(rootStats()).toBe(before + 1);
  482. expect(anchored(root)).toHaveLength(1);
  483. expect(anchored(root)[0]!.closed).toBe(false);
  484. } finally {
  485. entry.close();
  486. }
  487. });
  488. it("trips a shared anchor once when its stat hangs after succeeding and never stats it again", async () => {
  489. // 第一次 stat 成功,之后挂住。
  490. state.hungStats.set(root, 1);
  491. const { logger, warn } = loggerOf();
  492. const { entry, elapsed } = await timedCreate({
  493. load: loadSharingAnchor,
  494. bounds: [root],
  495. label: "MCP configuration",
  496. logger,
  497. bindTimeoutMs: SHORT_BIND_TIMEOUT_MS,
  498. });
  499. try {
  500. // 修复前同一轮里先拿到身份、后又超时,超时标记被身份覆盖:同一锚点记 2 条 warn,
  501. // 下一次绑定再 stat、再等满截止时间,条目创建等 2 个截止时间。
  502. expect(elapsed).toBeLessThan(SHORT_BIND_TIMEOUT_MS * 2);
  503. expect(warn).toHaveBeenCalledWith(TIMEOUT_MESSAGE, expect.objectContaining({ path: root }));
  504. expect(warn).toHaveBeenCalledTimes(1);
  505. expect(state.watchers.every((watcher) => watcher.closed)).toBe(true);
  506. // 断路后 flush 不再 stat 这个锚点,也不再等截止时间;仍全量重读。
  507. const stats = rootStats();
  508. const watchers = state.watchers.length;
  509. releaseStats();
  510. await writeFile(config, "v2");
  511. const started = performance.now();
  512. await entry.flush();
  513. expect(performance.now() - started).toBeLessThan(SHORT_BIND_TIMEOUT_MS);
  514. expect(entry.current().value).toBe("v2");
  515. expect(rootStats()).toBe(stats);
  516. expect(state.watchers).toHaveLength(watchers);
  517. expect(warn).toHaveBeenCalledTimes(1);
  518. } finally {
  519. entry.close();
  520. }
  521. });