Bug: When WatchStaticMappings=true throws exceptions on updating the mapping files #183

Closed
opened 2025-12-29 08:23:33 +01:00 by adam · 6 comments
Owner

Originally created by @muzammilkm on GitHub (Jun 20, 2019).

When WatchStaticMappings = true in settings throws exceptions & application abnormally gets aborted on updating the mapping files while Mock Server is running.

Exception:

System.IO.IOException: The process cannot access the file ''  because it is being used by another process.
 at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
   at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
   at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks)
   at System.IO.File.InternalReadAllText(String path, Encoding encoding)
   at System.IO.File.ReadAllText(String path)
   at WireMock.Server.FluentMockServer.ReadStaticMappingAndAddOrUpdate(String path) in /Server/FluentMockServer.Admin.cs:line 217
   at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(WatcherChangeTypes changeType, String name)
   at System.IO.FileSystemWatcher.ParseEventBufferAndNotifyForEach(Byte[] buffer)
   at System.IO.FileSystemWatcher.ReadDirectoryChangesCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* overlappedPointer)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

This happens due to editor lock duration while writing to disk(saving). I tried capturing the duration for various editors

VS Code: ~400 ms - ~100 ms
Sublime: ~250ms - ~100 ms
Notepad: ~50ms - ~10ms.

Finding the lock duration & work around for this issue, added in WireMock.Net\Server\FluentMockServer.Admin.cs in ReadStaticMappingAndAddOrUpdate method at line 215:

Stopwatch sw = new Stopwatch();
sw.Start();
while (IsFileLocked(path) && sw.ElapsedMilliseconds < EnhancedFileSystemWatcherTimeoutMs) 
{
}
sw.Stop();
 _logger.Debg("Waited for {0} ms", sw.ElapsedMilliseconds);
if (sw.ElapsedMilliseconds > EnhancedFileSystemWatcherTimeoutMs)
{
    _logger.Error("Abort. waited for {0} ms", sw.ElapsedMilliseconds);
    return;
}

Refer for IsFileLocked
Avoiding File Concurrency when using System.IO.FileSystemWatcher

Originally created by @muzammilkm on GitHub (Jun 20, 2019). When WatchStaticMappings = true in settings throws exceptions & application abnormally gets aborted on updating the mapping files while Mock Server is running. **Exception:** ``` System.IO.IOException: The process cannot access the file '' because it is being used by another process. at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle) at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks) at System.IO.File.InternalReadAllText(String path, Encoding encoding) at System.IO.File.ReadAllText(String path) at WireMock.Server.FluentMockServer.ReadStaticMappingAndAddOrUpdate(String path) in /Server/FluentMockServer.Admin.cs:line 217 at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(WatcherChangeTypes changeType, String name) at System.IO.FileSystemWatcher.ParseEventBufferAndNotifyForEach(Byte[] buffer) at System.IO.FileSystemWatcher.ReadDirectoryChangesCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* overlappedPointer) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP) ``` This happens due to editor lock duration while writing to disk(saving). I tried capturing the duration for various editors VS Code: ~400 ms - ~100 ms Sublime: ~250ms - ~100 ms Notepad: ~50ms - ~10ms. Finding the lock duration & work around for this issue, added in WireMock.Net\Server\FluentMockServer.Admin.cs in ReadStaticMappingAndAddOrUpdate method at line 215: ```C# Stopwatch sw = new Stopwatch(); sw.Start(); while (IsFileLocked(path) && sw.ElapsedMilliseconds < EnhancedFileSystemWatcherTimeoutMs) { } sw.Stop(); _logger.Debg("Waited for {0} ms", sw.ElapsedMilliseconds); if (sw.ElapsedMilliseconds > EnhancedFileSystemWatcherTimeoutMs) { _logger.Error("Abort. waited for {0} ms", sw.ElapsedMilliseconds); return; } ``` Refer for [IsFileLocked](https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use) [Avoiding File Concurrency when using System.IO.FileSystemWatcher](https://www.intertech.com/Blog/avoiding-file-concurrency-using-system-io-filesystemwatcher/)
adam added the bug label 2025-12-29 08:23:33 +01:00
adam closed this issue 2025-12-29 08:23:33 +01:00
Author
Owner

@StefH commented on GitHub (Jun 20, 2019):

I noticed this also in another scenario. I will check the code...

@StefH commented on GitHub (Jun 20, 2019): I noticed this also in another scenario. I will check the code...
Author
Owner

@StefH commented on GitHub (Jun 20, 2019):

Should be solved in next preview version at MyGet...

@StefH commented on GitHub (Jun 20, 2019): Should be solved in next preview version at MyGet...
Author
Owner

@StefH commented on GitHub (Jun 22, 2019):

@muzammilkm Can you verify if this works?

@StefH commented on GitHub (Jun 22, 2019): @muzammilkm Can you verify if this works?
Author
Owner

@muzammilkm commented on GitHub (Jun 26, 2019):

Verified.
When can we expect nuget package ?

@muzammilkm commented on GitHub (Jun 26, 2019): Verified. When can we expect nuget package ?
Author
Owner

@StefH commented on GitHub (Jun 26, 2019):

Next week Wednesday.

@StefH commented on GitHub (Jun 26, 2019): Next week Wednesday.
Author
Owner

@StefH commented on GitHub (Jul 3, 2019):

https://github.com/WireMock-Net/WireMock.Net/pull/286

@StefH commented on GitHub (Jul 3, 2019): https://github.com/WireMock-Net/WireMock.Net/pull/286
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#183