





















@@ -133,6 +133,8 @@ export type { ResolvedSessionMaintenanceConfig, SessionMaintenanceWarning };
133133type SaveSessionStoreOptions = {
134134/** Skip pruning, capping, and rotation (e.g. during one-time migrations). */
135135skipMaintenance?: boolean;
136+/** Internal hot paths can hand writer-owned stores to the cache after persistence. */
137+takeCacheOwnership?: boolean;
136138/** Active session key for warn-only maintenance. */
137139activeSessionKey?: string;
138140/**
@@ -198,6 +200,7 @@ function updateSessionStoreWriteCaches(params: {
198200storePath: string;
199201store: Record<string, SessionEntry>;
200202serialized: string;
203+takeOwnership?: boolean;
201204}): void {
202205const fileStat = getFileStatSnapshot(params.storePath);
203206setSerializedSessionStore(params.storePath, params.serialized, fileStat?.sizeBytes);
@@ -212,6 +215,7 @@ function updateSessionStoreWriteCaches(params: {
212215mtimeMs: fileStat?.mtimeMs,
213216sizeBytes: fileStat?.sizeBytes,
214217serialized: params.serialized,
218+takeOwnership: params.takeOwnership,
215219});
216220dropSessionStoreSnapshotCache(params.storePath);
217221}
@@ -437,15 +441,25 @@ async function saveSessionStoreUnlocked(
437441await fs.promises.mkdir(path.dirname(storePath), { recursive: true });
438442const json = JSON.stringify(store, null, 2);
439443if (getSerializedSessionStore(storePath) === json) {
440-updateSessionStoreWriteCaches({ storePath, store, serialized: json });
444+updateSessionStoreWriteCaches({
445+ storePath,
446+ store,
447+serialized: json,
448+takeOwnership: opts?.takeCacheOwnership,
449+});
441450return;
442451}
443452444453// Windows: keep retry semantics because rename can fail while readers hold locks.
445454if (process.platform === "win32") {
446455for (let i = 0; i < 5; i++) {
447456try {
448-await writeSessionStoreAtomic({ storePath, store, serialized: json });
457+await writeSessionStoreAtomic({
458+ storePath,
459+ store,
460+serialized: json,
461+takeOwnership: opts?.takeCacheOwnership,
462+});
449463return;
450464} catch (err) {
451465const code = getErrorCode(err);
@@ -465,15 +479,25 @@ async function saveSessionStoreUnlocked(
465479}
466480467481try {
468-await writeSessionStoreAtomic({ storePath, store, serialized: json });
482+await writeSessionStoreAtomic({
483+ storePath,
484+ store,
485+serialized: json,
486+takeOwnership: opts?.takeCacheOwnership,
487+});
469488} catch (err) {
470489const code = getErrorCode(err);
471490472491if (code === "ENOENT") {
473492// In tests the temp session-store directory may be deleted while writes are in-flight.
474493// Best-effort: try a direct write (recreating the parent dir), otherwise ignore.
475494try {
476-await writeSessionStoreAtomic({ storePath, store, serialized: json });
495+await writeSessionStoreAtomic({
496+ storePath,
497+ store,
498+serialized: json,
499+takeOwnership: opts?.takeCacheOwnership,
500+});
477501} catch (err2) {
478502const code2 = getErrorCode(err2);
479503if (code2 === "ENOENT") {
@@ -586,6 +610,7 @@ async function writeSessionStoreAtomic(params: {
586610storePath: string;
587611store: Record<string, SessionEntry>;
588612serialized: string;
613+takeOwnership?: boolean;
589614}): Promise<void> {
590615// Stage the temp as `sessions.json.<pid>.<uuid>.tmp` (not the generic
591616// `.fs-safe-replace.*`) so a temp orphaned by a crash between write and rename
@@ -599,6 +624,7 @@ async function writeSessionStoreAtomic(params: {
599624storePath: params.storePath,
600625store: params.store,
601626serialized: params.serialized,
627+takeOwnership: params.takeOwnership,
602628});
603629}
604630此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。