Skip to content

Commit 1f51210

Browse files
committed
fix(filesystem): treat an unusable path device as unknown on Windows (modelcontextprotocol#5154)
On Windows with Node 23, a path-based stat reports the device as 0 while the same file read through the opened handle reports the real volume serial. overwriteInPlace compared the two values, so every overwrite of an existing file was refused as "was replaced while it was being opened": write_file could only create files, and edit_file could not write at all. Measured on Windows 11 build 26200 with NTFS on C: and D:, stat only, no write involved, inode equal on both sides: node 23.6.1 lstat dev 0 fstat dev 716763443 node 23.11.1 lstat dev 0 fstat dev 716763443 node 22.20.0 lstat dev 716763443 fstat dev 716763443 node 24.9.0 lstat dev 716763443 fstat dev 716763443 node 25.1.0 lstat dev 716763443 fstat dev 716763443 The inode is what identifies the file, so the check keeps its meaning without a device the path-based stat cannot supply. The exception is narrow by design: Windows-only, only when the path-based stat reports 0, with the isFile() and inode comparisons unconditional, so any nonzero device mismatch still refuses on every platform. The repository's own filesystem suite on that machine under Node 23.6.1: 35 failed / 310 passed before, 10 failed / 339 passed after. The ten that remain are the symlink tests that need Developer Mode and fail the same way under Node 22, where the Windows CI leg runs. Signed-off-by: Ansu <[email protected]>
1 parent 38569bd commit 1f51210

3 files changed

Lines changed: 138 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-filesystem": patch
3+
---
4+
5+
Overwrite an existing file again on Windows with Node 23. There a path-based `lstat` reports the device as 0 while the same file read through the opened handle reports the volume serial, and the in-place overwrite's identity check read that difference as a replaced file, so `write_file` and `edit_file` refused every overwrite with "was replaced while it was being opened" (#5154).

‎src/filesystem/__tests__/lib.test.ts‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,119 @@ describe("Lib Functions", () => {
603603
expect(handle.writeFile).not.toHaveBeenCalled();
604604
});
605605

606+
// #5154: on Windows a path-based stat can report the device as 0 while
607+
// the same file through the opened handle reports the real volume
608+
// serial (Node 23; measured 23.6.1 and 23.11.1). The inode still
609+
// matches. Requiring the devices to be equal refused every overwrite of
610+
// an existing file there.
611+
it("overwrites when the path-based stat reports no device (Windows)", async () => {
612+
mockFs.writeFile.mockRejectedValueOnce(
613+
Object.assign(new Error("EEXIST"), { code: "EEXIST" }),
614+
);
615+
mockFs.lstat.mockResolvedValueOnce({
616+
...REGULAR_FILE,
617+
dev: 0,
618+
} as Stats);
619+
const handle = createMockWriteHandle({
620+
...REGULAR_FILE,
621+
dev: 716763443,
622+
} as Stats);
623+
mockFs.open.mockResolvedValueOnce(asWriteHandle(handle));
624+
625+
const platform = process.platform;
626+
Object.defineProperty(process, "platform", { value: "win32" });
627+
try {
628+
await writeFileContent("/test/file.txt", "new content");
629+
} finally {
630+
Object.defineProperty(process, "platform", { value: platform });
631+
}
632+
633+
expect(handle.truncate).toHaveBeenCalledWith(0);
634+
expect(handle.writeFile).toHaveBeenCalledWith("new content", "utf-8");
635+
});
636+
637+
it("still refuses a swap on Windows when the path-based stat has no device", async () => {
638+
mockFs.writeFile.mockRejectedValueOnce(
639+
Object.assign(new Error("EEXIST"), { code: "EEXIST" }),
640+
);
641+
mockFs.lstat.mockResolvedValueOnce({
642+
...REGULAR_FILE,
643+
dev: 0,
644+
} as Stats);
645+
const handle = createMockWriteHandle({
646+
...REGULAR_FILE,
647+
dev: 716763443,
648+
ino: 43,
649+
} as Stats);
650+
mockFs.open.mockResolvedValueOnce(asWriteHandle(handle));
651+
652+
const platform = process.platform;
653+
Object.defineProperty(process, "platform", { value: "win32" });
654+
try {
655+
await expect(
656+
writeFileContent("/test/file.txt", "new content"),
657+
).rejects.toThrow("was replaced while it was being opened");
658+
} finally {
659+
Object.defineProperty(process, "platform", { value: platform });
660+
}
661+
662+
expect(handle.writeFile).not.toHaveBeenCalled();
663+
});
664+
665+
it("still refuses a nonzero device mismatch on Windows", async () => {
666+
mockFs.writeFile.mockRejectedValueOnce(
667+
Object.assign(new Error("EEXIST"), { code: "EEXIST" }),
668+
);
669+
mockFs.lstat.mockResolvedValueOnce({
670+
...REGULAR_FILE,
671+
dev: 2,
672+
} as Stats);
673+
const handle = createMockWriteHandle({
674+
...REGULAR_FILE,
675+
dev: 3,
676+
} as Stats);
677+
mockFs.open.mockResolvedValueOnce(asWriteHandle(handle));
678+
679+
const platform = process.platform;
680+
Object.defineProperty(process, "platform", { value: "win32" });
681+
try {
682+
await expect(
683+
writeFileContent("/test/file.txt", "new content"),
684+
).rejects.toThrow("was replaced while it was being opened");
685+
} finally {
686+
Object.defineProperty(process, "platform", { value: platform });
687+
}
688+
689+
expect(handle.writeFile).not.toHaveBeenCalled();
690+
});
691+
692+
it("still refuses a deviceless path stat off Windows", async () => {
693+
mockFs.writeFile.mockRejectedValueOnce(
694+
Object.assign(new Error("EEXIST"), { code: "EEXIST" }),
695+
);
696+
mockFs.lstat.mockResolvedValueOnce({
697+
...REGULAR_FILE,
698+
dev: 0,
699+
} as Stats);
700+
const handle = createMockWriteHandle({
701+
...REGULAR_FILE,
702+
dev: 716763443,
703+
} as Stats);
704+
mockFs.open.mockResolvedValueOnce(asWriteHandle(handle));
705+
706+
const platform = process.platform;
707+
Object.defineProperty(process, "platform", { value: "linux" });
708+
try {
709+
await expect(
710+
writeFileContent("/test/file.txt", "new content"),
711+
).rejects.toThrow("was replaced while it was being opened");
712+
} finally {
713+
Object.defineProperty(process, "platform", { value: platform });
714+
}
715+
716+
expect(handle.writeFile).not.toHaveBeenCalled();
717+
});
718+
606719
it("closes the handle when the write fails", async () => {
607720
mockFs.writeFile.mockRejectedValueOnce(
608721
Object.assign(new Error("EEXIST"), { code: "EEXIST" }),

‎src/filesystem/lib.ts‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,15 @@ export async function writeFileContent(
323323
* included;
324324
* - O_NOFOLLOW makes the open itself fail with ELOOP on a symlink. It exists
325325
* only on POSIX: fs.constants.O_NOFOLLOW is undefined on Windows;
326-
* - the opened handle must be the very file lstat saw (same device and inode),
326+
* - the opened handle must be the very file lstat saw. Identity is the inode,
327327
* which catches a swap between the lstat and the open on every platform,
328-
* Windows included. Once the handle is verified, every write goes through
329-
* it, so a later swap of the path cannot redirect it.
328+
* Windows included. The device value is compared too, except where the
329+
* path-based stat cannot supply it: on Windows a path-based stat can report
330+
* device 0 while the same file read through the handle reports the real
331+
* volume serial (Node 23; #5154), so a zero device there is treated as
332+
* unknown and the inode alone carries the check. Any nonzero device
333+
* mismatch still refuses. Once the handle is verified, every write goes
334+
* through it, so a later swap of the path cannot redirect it.
330335
*
331336
* The trade-off is crash-atomicity: a crash mid-write leaves a partly written
332337
* file, where the rename left either the old content or the new.
@@ -347,10 +352,20 @@ async function overwriteInPlace(
347352
);
348353
try {
349354
const opened = await handle.stat();
355+
// A path-based stat on Windows can report device 0 where the same file,
356+
// read through the opened handle, reports the volume serial: Node 23
357+
// does (measured 23.6.1 and 23.11.1, both NTFS volumes), while 22.20.0,
358+
// 24.9.0 and 25.1.0 report the serial (#5154). The inode still identifies
359+
// the file, so the check keeps its meaning without comparing the two
360+
// device values. Windows-only, and only when the path-based stat says the
361+
// device is unknown: any nonzero mismatch still refuses, and isFile() and
362+
// the inode check stay unconditional.
363+
const deviceUnavailable =
364+
process.platform === "win32" && expected.dev === 0;
350365
if (
351366
!opened.isFile() ||
352-
opened.dev !== expected.dev ||
353-
opened.ino !== expected.ino
367+
opened.ino !== expected.ino ||
368+
(!deviceUnavailable && opened.dev !== expected.dev)
354369
) {
355370
throw new Error(
356371
`Refusing to write: ${filePath} was replaced while it was being opened`,

0 commit comments

Comments
 (0)