开发者

Writing files gets stuck on network share

I have a program that writes files to a network share at a high rate, from a few (3) threads at once.

After running for a while (usually a short while) some of these threads get stuck. Using Process Monitor, I can see that there are calls to WriteFile and CloseFile that simply have no answer.

At this point, I can't shut down the process at all, even killing it from the task manager does nothing.

The interesting thing is that this happens when the computer hosting the shares is running Windows Server 2008 (R2). If I move the shares to a Windows 2003 computer, I don't see these problems. Also, I only see this problem if the program is run on a computer that is running Windows Server 2008 (different computer than the share host).

Here is a short program that quickly reproduces the problem. The files in the source directory range in size from 1 to 20 MB:

Imports System.IO
Imports System.Threading

Module Module1

  Private m_sourceFiles As FileInfo()
  Private m_targetDir As String

  Sub Main(ByVal args As String())

    Dim sourceDir As New DirectoryInfo(args(0))
    m_sourceFiles = sourceDir.GetFiles()

    m_targetDir = args(1)

    For i As Integer = 0 To 2
      ThreadPool.QueueUserWorkItem(AddressOf DoWork)
    Next

    Console.ReadLine()
  End Sub

  Private Const BUFFER_SIZE As Integer = (128 * 1024)

  Private Sub DoWork(ByVal o As Object)
    Console.WriteLine(Thread.CurrentThread.ManagedThreadId)
    Dim random As New Random(Thread.CurrentThread.ManagedThreadId)
    While True
      Dim fileIndex As Integer = random.Next(m_sourceFiles.Count)
      Dim sourceFile As FileInfo = m_sourceFiles(fileIndex)
      Dim input As FileStream = sourceFile.OpenRead
      Dim targetName As String = sourceFile.Name.Replace(sourceFile.Extension, random.Next(Integer.MaxValue) & sourceFile.Extension)
      Dim targetPath As String = m_targetDir & "\" & targetName
      Dim output As FileStream = File.Create(targetPath)

      Dim bytes() As Byte = New Byte((BUFFER_SIZE) - 1) {}
      Dim read As Integer = input.Read(bytes, 0, bytes.Length)
      While read <> 0
        output.Write(bytes, 0, read)
        read = input.Read(bytes开发者_JAVA技巧, 0, bytes.Length)
      End While
      output.Flush()
      output.Close()
      Console.WriteLine(Thread.CurrentThread.ManagedThreadId & " - " & targetName)
    End While
  End Sub

End Module


The problem was caused by Symantec Antivirus. Apparently they don't support 2008 R1 yet.

I was able to workaround the issue by disabling SMB 2.0 on the client computer, as described here:

sc config lanmanworkstation depend= bowser/mrxsmb10/nsi
sc config mrxsmb20 start= disabled
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜