Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Unity editor hangs when closing named pipe

Discussion in 'Scripting' started by originalvector, Nov 8, 2019.

  1. originalvector

    originalvector

    Joined:
    Sep 29, 2019
    Posts:
    1
    I am using named pipes to handle IPC (inter-process communication) with an external application running outside the Unity environment. My class inherits from MonoBehavior and I have the following code in the Awake callback:

    Code (CSharp):
    1.             if (clientPipe == null)
    2.                 clientPipe = new NamedPipeClientStream(".", "externalProc",
    3.                                 PipeDirection.InOut, PipeOptions.WriteThrough | PipeOptions.Asynchronous);
    4.             clientPipe.ReadMode = PipeTransmissionMode.Message;
    5.             if (!clientPipe.IsConnected)
    6.             {
    7.                 clientPipe.Connect(1000 * 5);
    8.                 asyncRes = clientPipe.BeginRead(readBuffer, 0, readBuffer.Length, new AsyncCallback(OnReadFromExternalProc), null);
    9.             }
    10.  
    Then in the OnApplicationQuit callback, I have this:

    Code (CSharp):
    1.             appQuitting = true;
    2.             clientPipe.Close();
    3.  
    If I comment out the clientPipe.Close() call, the editor will not hang. If I leave it in there, the editor will hang and I have no idea why that is happening. I should be able to close the pipe when play mode is stopped in the editor or when the application is terminated. It will then cause an exception to be thrown in the Async method "OnReadFromExternalProc" where I can check the type of exception and the appQuitting value to determine that it should stop trying to read data and not execute another async BeginRead call again and then simply return ending the repeated asynchronous read cycle.

    I have used this approach in the past, seen it documented in numerous examples and in the documentation for named pipes in the .net framework. I also tried this approach in a console application and it works flawlessly.

    I'm running 2018.12f1 on Windows. Anyone have any ideas why the editor hangs when I try and close the pipe?
     
  2. LeBugu

    LeBugu

    Joined:
    Dec 25, 2020
    Posts:
    1
    Have you ever get to the bottom of this? I experience the same issue,