Repository navigation
fix: handle UNC image paths and watchers - #4778
Merged
Jocs merged 2 commits intoSep 3, 2026
Merged
Conversation
Jocs
approved these changes
Sep 3, 2026
|
Following up on this PR — it looks like it introduced a regression on real SMB shares. On 0.20.0-rc.2, saving a file opened from Since this PR makes Full details, logs and what I ruled out: #5322 (中文)本 PR 在真实 SMB 共享上引入了回归:rc.2 保存 🤖 Investigated with Claude Code |
4 of 7 tasks
Jocs
added a commit
that referenced
this pull request
Sep 23, 2026
…5520) * fix(desktop): retry a save whose rename an SMB share refused (#5322) On a Windows SMB share, MoveFileEx(REPLACE_EXISTING) — what fs.rename compiles to, and the last step of the atomic save — is refused with ACCESS_DENIED, surfaced as EPERM, while any handle is open on the target. MarkText's own file watcher stats the open document, so a save races its own watcher: the temp file is written, the rename over the document is rejected, and the edit is lost. Measured on a real Windows SMB share (loopback share, windows-latest): one open read handle held across every save 50/50 renames refused watcher stat storm at 1ms (~12000 stats) 102/300 refused watcher polling as the app configures it 1/200 refused no handle open at all 0/50 refused The same probes against Samba from a Linux client never fail, so this is the Windows SMB redirector's share-mode check, not a server-side one. This is a 0.19 -> 0.20 regression, and not from #4778 as the issue assumed. 0.19.x wrote the document in place with fs-extra's outputFile and never renamed, so it could not hit this. #4852 introduced the temp+rename atomic save (for #3786/#3828), which is what races; #4778 then forced usePolling for UNC paths, which raised the stat rate enough to turn an occasional lost save into a reliable one. Reverting #4778 would only push the failure back below the reporter's notice — it would not fix it, and it would leave mapped network drives (Z:\, not a UNC path, so never force-polled) failing occasionally either way. The handle is released within milliseconds, so back off and try the save again instead of dropping the edit. Retrying re-runs the whole atomic write, which is safe: write-file-atomic cleans up its temp file on failure. Three retries at 50/150/300ms take the 1ms-stat-storm failure rate from 102/300 to 3/300; at the app's real stat rate the remaining window is far smaller still. A process that holds a handle open continuously (an antivirus scanner, a sync client) is out of reach of any retry — that save still reports mt::tab-save-failure and the tab stays dirty, rather than silently losing the edit. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> * docs: correct what the rename refusal actually depends on (#5322) A follow-up probe showed the earlier explanation was wrong on two counts: MoveFileEx(REPLACE_EXISTING) is refused for an open target on local NTFS just as on a share (50/50 either way), and asking for FILE_SHARE_DELETE makes no difference (err=5 with and without it). What separates the two is how long the handle stays open - microseconds for a local stat, a network round trip for one over SMB - so only shares lose the race in practice. The retry itself is unchanged; only the reasoning it records. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> * fix(desktop): pick retry delays that land on a different phase (#5322) The watcher stats on a fixed period, so a retry only helps if its cumulative offset lands on a phase the stat is not holding. 50/150/300 accumulates to 50/200/500 - the last two are multiples of chokidar's 100ms interval, i.e. exactly the phase that just failed, so only the first retry was ever doing anything. Simulated against both periods the app uses (100ms watchFile, 150ms awaitWriteFinish), that loses 33/57/75% of first failures once a stat holds the file for 60/70/80ms. 50/75/50 accumulates to 50/125/175 - three distinct phases - and clears every first failure up to a ~70ms stat, while finishing in 175ms instead of 500ms. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4563
Summary
Fix UNC path handling for local images and project-tree watching.
Relative images opened from UNC-backed documents could lose the UNC host during path resolution, producing invalid URLs such as
file:///wsl.localhost/...instead of host-based file URLs such asfile://wsl.localhost/.... This PR normalizes local image paths into validfile://URLs for UNC paths, Windows drive paths, and POSIX paths.UNC roots also need polling for reliable directory watching. On WSL UNC paths, chokidar can fail to watch or enumerate files cleanly without polling, which can leave the project tree empty or produce watcher errors. This PR detects UNC roots and forces
usePolling: truefor those watchers.Type of change
Test plan
The following checks passed:
pnpm --filter @muyajs/core exec vitest run src/utils/__tests__/image.spec.tspnpm --filter marktext exec vitest run test/unit/specs/printService-image.spec.ts test/unit/specs/exportHtml.spec.ts test/unit/specs/watcher-await-write-finish.spec.tspnpm --filter @muyajs/core lint:typespnpm --filter marktext typecheckpnpm --filter @muyajs/core exec vitest run --testTimeout 30000git diff --checkManual WSL UNC verification:
\\wsl.localhost\HermesUbuntu\tmp\marktext-unc-real-test.doc-relative.mdandimg/desktop-image.pnginside that WSL filesystem.renders correctly.Real SMB / Windows network share verification was not performed because no actual
\\server\shareenvironment was available. The same UNC-path fix is expected to address #4577 as well, and the ordinary UNC share case is covered by the same detection and file URL normalization logic with unit tests for both\\server\share\...and//server/share/...path forms. However, because #4577 was not verified against a real SMB share, this PR only closes #4563.Notes for reviewers [optional]
The core issue is not WSL-specific. WSL UNC paths and ordinary SMB share paths both use the same UNC shape:
The image fix preserves the
host/shareportion when resolving local image paths and converts local paths to valid file URLs:For watcher behavior, UNC roots force polling because native watching on WSL UNC paths was unreliable during verification. A direct chokidar probe on the WSL UNC test folder showed non-polling watchers emitted
EISDIRerrors, while polling discovered the same files without watcher errors.By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.