一度に数 (3) スレッドから高速でネットワーク共有にファイルを書き込むプログラムがあります。
しばらく (通常は短時間) 実行すると、これらのスレッドの一部が動かなくなります。Process Monitor を使用すると、応答のない 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