none
Deadlock at Microsoft.Win32.Win32Native.CreateFile RRS feed

  • Question

  • We have a WCF service that reads and writes documents on a network file storage (shared folder).
    Occasionally we see a situation where threads start to get stuck in the Win32Native code. Once a thread gets blocked, all the consequent calls are blocked as well. The interesting thing is that the issue does not go away after the windows service is restarted. Very first thread that tries to work with the document storage blocks and the issue repeats. Only after the server is rebooted, the service continues to work without issues for some time (sometimes a few days, and sometimes a few weeks).

    This only occurs in our production environment. I currently do not have any information about the server state at the time the issue occurs, about any backup processes or any windows event logs from the time.
    So any ideas on where to look and what could possibly cause this issue would be very helpful.

    An example of the code where the threads are blocked is when creating an empty file on the document storage:

    public void CreateFile( string filePath )
    {
    using ( FileStream stream = File.Create( filePath ) )
    {
    }
    }

    Blocked thread stack trace:
       at Microsoft.Win32.Win32Native.CreateFile(String lpFileName, Int32 dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, Int32 dwFlagsAndAttributes, IntPtr hTemplateFile)
       at Microsoft.Win32.Win32Native.CreateFile(String lpFileName, Int32 dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, Int32 dwFlagsAndAttributes, IntPtr hTemplateFile)
       at Microsoft.Win32.Win32Native.SafeCreateFile(String lpFileName, Int32 dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, Int32 dwFlagsAndAttributes, IntPtr hTemplateFile)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
       at MyService.DocumentProcessingComponent.CreateFile(string filePath)
       . . . Custom Code . . .
       at MyService.Service.DoSomething(Message requestMessage)
       at SyncInvokeInvokeService(Object , Object[] , Object[] )
       at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
       at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
       at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
       at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
       at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
       at System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(RequestContext request, Boolean cleanThread, OperationContext currentOperationContext)
       at System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(RequestContext request, OperationContext currentOperationContext)
       at System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult result)
       at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
       at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
       at System.ServiceModel.Channels.TransportDuplexSessionChannel.TryReceiveAsyncResult.OnReceive(IAsyncResult result)
       at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
       at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
       at System.ServiceModel.Channels.SynchronizedMessageSource.ReceiveAsyncResult.OnReceiveComplete(Object state)
       at System.ServiceModel.Channels.SessionConnectionReader.OnAsyncReadComplete(Object state)
       at System.ServiceModel.Channels.SocketConnection.OnReceiveAsync(Object sender, SocketAsyncEventArgs eventArgs)
       at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSuccess(SocketError socketError, Int32 bytesTransferred, SocketFlags flags)
       at System.Net.Sockets.SocketAsyncEventArgs.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
       at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

    Sometimes the threads are blocked in different Win32Native method like in the following example:

    public ReadFileResult ReadFileBlock( string filePath, int blockIndex )
    {
    if ( !File.Exists( filePath ) )
    return new ReadFileResult() { Error = "File not found" };

    byte[] content = null;
    int nextBlockIndex = 0;

    using ( FileStream fs = File.Open( filePath, FileMode.Open, FileAccess.Read, FileShare.Read ) )
    {
    int offset = _defaultBlockSize * blockIndex;

    if ( offset >= fs.Length )
    return new ReadFileResult() { Error = "Block index out of range" };

    fs.Seek( offset, SeekOrigin.Begin );
    int bytesToRead = ( int )Math.Min( _defaultBlockSize, fs.Length - fs.Position );
    content = new byte[ bytesToRead ];
    int bytesRead = fs.Read( content, 0, bytesToRead );

    if ( bytesRead < _defaultBlockSize || fs.Position == fs.Length )
    nextBlockIndex = -1;
    else
    nextBlockIndex = blockIndex + 1;
    }

    return new ReadFileResult() { Block = content, NextBlockIndex = nextBlockIndex };
    }

    Blocked thread stack trace: 
       at Microsoft.Win32.Win32Native.GetFileAttributesEx(String name, Int32 fileInfoLevel, WIN32_FILE_ATTRIBUTE_DATA& lpFileInformation)
       at Microsoft.Win32.Win32Native.GetFileAttributesEx(String name, Int32 fileInfoLevel, WIN32_FILE_ATTRIBUTE_DATA& lpFileInformation)
       at System.IO.File.FillAttributeInfo(String path, WIN32_FILE_ATTRIBUTE_DATA& data, Boolean tryagain, Boolean returnErrorOnNotFound)
       at System.IO.File.InternalExists(String path)
       at System.IO.File.InternalExistsHelper(String path, Boolean checkHost)
       at MyService.DocumentProcessingComponent.ReadFileBlock(string filePath, int blockIndex)
    Wednesday, December 10, 2014 2:26 PM

Answers

  • This looks like a problem with the remote file share or accessing the remote file share.  I would recommend figuring out what kind of share it is and what sharing protocol is in use, then use appropriate troubleshooting for that to figure out why the call isn't returning.

    WinSDK Support Team Blog: http://blogs.msdn.com/b/winsdk/


    Wednesday, December 10, 2014 5:12 PM