Windowsサービスがランダムにぶら下がっており、根本原因の分析に役立つことがあります。.NET 4.5で書かれたWindowsサービスでConsole.OutとConsole.Error競合状態エラーが発生しました
サービスはC#で書かれており、.NET 4.5を搭載したマシンにデプロイされています(.NET 4.5.1でも再現可能ですが)。報告
エラーがある:私はロガーにConsole.WriteLineを()とConsole.Error.WriteLine()への呼び出しに例外のソースを絞り込んいる
Probable I/O race condition detected while copying memory.
The I/O package is not thread safe by default.
In multithreaded applications, a stream must be accessed in a thread-safe way, such as a thread-safe wrapper returned by TextReader's or TextWriter's Synchronized methods.
This also applies to classes like StreamWriter and StreamReader.
。これらは複数のスレッドから呼び出され、負荷が高い場合、エラーが表示され始め、サービスがハングします。
しかし、MSDNによると、Consoleクラス全体はスレッドセーフです(以前は複数のスレッドから問題はなくなりました)。さらに、この問題はではなく、がコンソールアプリケーションと同じコードを実行しているときに表示されます。 Windowsサービスからのみ。最後に、例外のスタックトレースは、コンソールクラスのSyncTextWriterへの内部呼び出しを示します。これは、例外で言及された同期バージョンでなければなりません。
私は何か間違っているか、ここでポイントを逃しているかどうか知っていますか?考えられる回避策は、OutストリームとErrストリームを/ dev/nullにリダイレクトしているようですが、.NETの知識を超えていると思われるより詳細な解析が望まれます。
試したときにエラーを投げるrepro windowsサービスを作成しました。コードは以下のとおりです。
サービスクラス:
[RunInstaller(true)]
public partial class ParallelTest : ServiceBase
{
public ParallelTest()
{
InitializeComponent();
this.ServiceName = "ATestService";
}
protected override void OnStart(string[] args)
{
Thread t = new Thread(DoWork);
t.IsBackground = false;
this.EventLog.WriteEntry("Starting worker thread");
t.Start();
this.EventLog.WriteEntry("Starting service");
}
protected override void OnStop()
{
}
private void DoWork()
{
this.EventLog.WriteEntry("Starting");
Parallel.For(0, 1000, new ParallelOptions() { MaxDegreeOfParallelism = 10 }, (_) =>
{
try
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("test message to the out stream");
Thread.Sleep(100);
Console.Error.WriteLine("Test message to the error stream");
}
}
catch (Exception ex)
{
this.EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
//throw;
}
});
this.EventLog.WriteEntry("Finished");
}
}
メインクラス:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
// Remove comment below to stop the errors
//Console.SetOut(new StreamWriter(Stream.Null));
//Console.SetError(new StreamWriter(Stream.Null));
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new ParallelTest()
};
ServiceBase.Run(ServicesToRun);
}
}
インストーラクラス:
partial class ProjectInstaller
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "ATestServiceHere";
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
#endregion
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
}
InstallUtil.exeでこのサービスをインストールし、それがイベントにエラーを記録開始ログ。
ブラインド:
は、ここでの問題を示し、最小限の非サービスコンソールプログラムです。サービスにはコンソールがないため、Console.Write/Line()を呼び出すポイントはありません。バン、問題解決。 –
実際、Console.OutとConsole.Errをヌルストリームにリダイレクトして、それが問題を解決しました(サードパーティ製のいくつかのライブラリが直接書き込んでいます)。しかし、これがConsoleクラスのバグかどうか不思議です。 サンプルスタックトレース(Console.WriteLineをインライン化しているように見える): 'System.Buffer.InternalBlockCopy(配列src、のInt32 srcOffsetBytes、アレイDST、のInt32 dstOffsetBytes、のInt32:バイト) System.IO.StreamWriter.Write(シャア[]バッファ、Int32インデックス、Int32カウント) System.IO.TextWriter.WriteLine(文字列値) System.IO.TextWriter.SyncTextWriter.WriteLine(文字列値) ' – braintechd
Console.Outは遅れて作成されます。これにより、SyncTextWriter.WriteLine()が依存する[MethodImpl(MethodImplOptions.Synchronized)]実装でスレッド競争が発生する可能性があります。それを避ける最も簡単な方法は、Console.Outを再割り当てする以外に、MainメソッドにConsole.WriteLine()ステートメントを追加することです。このバグをconnect.microsoft.com btwに提出することを検討してください。 –