2015-11-25 18 views
8

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でこのサービスをインストールし、それがイベントにエラーを記録開始ログ。

+0

ブラインド:

は、ここでの問題を示し、最小限の非サービスコンソールプログラムです。サービスにはコンソールがないため、Console.Write/Line()を呼び出すポイントはありません。バン、問題解決。 –

+0

実際、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

+0

Console.Outは遅れて作成されます。これにより、SyncTextWriter.WriteLine()が依存する[MethodImpl(MethodImplOptions.Synchronized)]実装でスレッド競争が発生する可能性があります。それを避ける最も簡単な方法は、Console.Outを再割り当てする以外に、MainメソッドにConsole.WriteLine()ステートメントを追加することです。このバグをconnect.microsoft.com btwに提出することを検討してください。 –

答えて

10

Console.OutとConsole.Errorはどちらも、コンソール出力とエラーストリームTextWriterのスレッドセーフラッパー(TextWriter.Synchronized経由)を返すため、スレッドセーフです。ただし、このスレッドセーフティは、Console.OutおよびConsole.Errorが異なるTextWritersの場合にのみ適用されます。ストリーム。

Windowsサービスとして実行するときにコードが例外をスローする理由は、出力およびエラーのTextWritersが両方ともStreamWriter.Null(シングルトン)に設定されているためです。コードでConsole.WriteLineとConsole.Error.WriteLineの両方が呼び出され、別のスレッドがConsole.Error.WriteLineを呼び出すと同時に1つのスレッドがConsole.WriteLineを呼び出すと、例外が発生します。これにより、同じストリームが同時に2つのスレッドから書き込まれ、「メモリのコピー中にI/O競合状態が検出される」という結果になります。例外。 Console.WriteLineのみを使用するか、Console.Error.WriteLineのみを使用すると、例外が発生しなくなります。スタックトレースせずに推測

using System; 
using System.IO; 
using System.Threading.Tasks; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     var oldOut = Console.Out; 
     var oldError = Console.Error; 

     Console.SetOut(StreamWriter.Null); 
     Console.SetError(StreamWriter.Null); 
     Parallel.For(0, 2, new ParallelOptions() { MaxDegreeOfParallelism = 2 }, (_) => 
     { 
      try 
      { 
       while(true) 
       { 
        Console.WriteLine("test message to the out stream"); 
        Console.Error.WriteLine("Test message to the error stream"); 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.SetOut(oldOut); 
       Console.SetError(oldError); 
       Console.WriteLine(ex); 
       Environment.Exit(1); 
      } 
     }); 
    } 
} 
関連する問題