Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
muzammilkm opened this issue Jun 20, 2019 · 6 comments
Labels

Comments

@muzammilkm
Copy link

muzammilkm commented 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

@StefH
Copy link
Collaborator

StefH commented Jun 20, 2019

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

@StefH
Copy link
Collaborator

StefH commented Jun 20, 2019

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

@StefH
Copy link
Collaborator

StefH commented Jun 22, 2019

@muzammilkm Can you verify if this works?

@muzammilkm
Copy link
Author

Verified.
When can we expect nuget package ?

@StefH
Copy link
Collaborator

StefH commented Jun 26, 2019

Next week Wednesday.

@StefH
Copy link
Collaborator

StefH commented Jul 3, 2019

#286

@StefH StefH closed this as completed Jul 3, 2019
@StefH StefH added the bug label Jul 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants