写入文件卡在网络共享上

发布于 2024-11-09 09:26:44 字数 1917 浏览 3 评论 0原文

我有一个程序,可以同时从几 (3) 个线程将文件高速写入网络共享。

运行一段时间(通常是一小段时间)后,其中一些线程会卡住。使用进程监视器,我可以看到对 WriteFile 和 CloseFile 的调用根本没有答案。

此时,我根本无法关闭该进程,即使从任务管理器中杀死它也无济于事。

有趣的是,当托管共享的计算机运行 Windows Server 2008 (R2) 时,就会发生这种情况。如果我将共享移至 Windows 2003 计算机,则不会出现这些问题。另外,只有当程序在运行 Windows Server 2008 的计算机(与共享主机不同的计算机)上运行时,我才会看到此问题。

这是一个可以快速重现问题的简短程序。源目录中的文件大小范围为 1 到 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, 0, bytes.Length)
      End While
      output.Flush()
      output.Close()
      Console.WriteLine(Thread.CurrentThread.ManagedThreadId & " - " & targetName)
    End While
  End Sub

End Module

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, 0, bytes.Length)
      End While
      output.Flush()
      output.Close()
      Console.WriteLine(Thread.CurrentThread.ManagedThreadId & " - " & targetName)
    End While
  End Sub

End Module

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲欢浪云 2024-11-16 09:26:44

该问题是由 Symantec Antivirus 引起的。
显然他们还不支持 2008 R1。

我能够通过在客户端计算机上禁用 SMB 2.0 来解决该问题,如所述 此处

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文