{"record":{"id":"5257068ece643a94","repo":"stride3d/stride","slug":"couldn-t-unlock-file","errorCode":null,"errorMessage":"Couldn't unlock file.","messagePattern":"Couldn't unlock file\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.IO/NativeLockFile.cs","lineNumber":154,"sourceCode":"        if (Platform.Type == PlatformType.Android)\n        {\n            count = (count + offset > int.MaxValue) ? int.MaxValue - offset : count;\n        }\n\n        if (OperatingSystem.IsWindows())\n        {\n            var countLow = (uint)count;\n            var countHigh = (uint)(count >> 32);\n\n            var overlapped = new NativeOverlapped()\n            {\n                OffsetLow = (int)(offset & uint.MaxValue),\n                OffsetHigh = (int)(offset >> 32),\n            };\n\n            if (!UnlockFileEx(fileStream.SafeFileHandle, 0, countLow, countHigh, ref overlapped))\n            {\n                throw new IOException(\"Couldn't unlock file.\");\n            }\n        }\n        else\n        {\n            UnixUnlock(fileStream, offset, count);\n        }\n    }\n\n    /// <summary>\n    ///   Lock on Unix: on Linux/Android, tries OFD locks first (per-handle, like Windows LockFileEx),\n    ///   falls back to standard fcntl (per-process) if OFD is not supported.\n    ///   On macOS/iOS, uses standard fcntl directly (OFD not available on XNU).\n    /// </summary>\n    private static FileLockResult UnixLock(FileStream fileStream, long offset, long count, bool exclusive)\n    {\n        int fd = fileStream.SafeFileHandle.DangerousGetHandle().ToInt32();\n\n        var lockInfo = new Flock","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.IO/NativeLockFile.cs#L136-L172","documentation":"TryUnlockFile releases a byte-range lock previously taken on a file. On Windows it calls UnlockFileEx; if the OS reports the unlock failed (e.g. the range was not locked by this handle), an IOException is thrown.","triggerScenarios":"Calling TryUnlockFile on a range (offset/count) that was never locked via the matching TryLockFile on the same FileStream/SafeFileHandle, or with an offset/count that doesn't exactly match the locked region.","commonSituations":"Double-unlock after an exception, unlocking after the stream was reopened (locks are per-handle), mismatched offset/count values between lock and unlock calls.","solutions":["Ensure every unlock uses the exact offset and count used when locking, on the same FileStream instance","Guard against double-unlock with a flag or try/finally pairing lock/unlock","Verify the handle still owns the lock (no intermediate close/reopen)","Check the underlying Win32 error via the inner details; treat EINVAL-style 'not locked' as a logic bug in the caller"],"exampleFix":"// before\nlockFile.TryLockFile(0, len);\n// ... exception path skips unlock ...\nlockFile.TryUnlockFile(0, len); // may throw if never locked\n// after\nlockFile.TryLockFile(0, len);\ntry { /* work */ }\nfinally { if (isLocked) lockFile.TryUnlockFile(0, len); }","handlingStrategy":"try-catch","validationCode":"// track locked ranges in a HashSet<(long offset,long count)> owned by the same FileStream\nbool owned = lockedRanges.Contains((offset, count));","typeGuard":null,"tryCatchPattern":"try { lockFile.TryUnlockFile(offset, count); }\ncatch (IOException) { /* range not locked by this handle — log and continue */ }\nfinally { lockedRanges.Remove((offset, count)); }","preventionTips":["Pair every TryLockFile with exactly one unlock using identical offset/count","Use try/finally so exceptions never skip unlock","Never unlock on a different FileStream instance than the one that locked"],"tags":["io","file-locking","windows"],"backgroundTag":"file-open-failed","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}