Skip to content

Commit 383202e

Browse files
committed
Only update the native library and the c# calls that need to be made to clean up channels.
1 parent 8bf91f9 commit 383202e

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

com.unity.ml-agents/Runtime/Communicator/RpcCommunicator.cs

+17-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define MLA_SUPPORTED_TRAINING_PLATFORM
33
#endif
44

5-
# if MLA_SUPPORTED_TRAINING_PLATFORM
5+
#if MLA_SUPPORTED_TRAINING_PLATFORM
66
using Grpc.Core;
77
#if UNITY_EDITOR
88
using UnityEditor;
@@ -50,6 +50,7 @@ internal class RpcCommunicator : ICommunicator
5050

5151
/// The Unity to External client.
5252
UnityToExternalProto.UnityToExternalProtoClient m_Client;
53+
Channel m_Channel;
5354

5455
/// <summary>
5556
/// Initializes a new instance of the RPCCommunicator class.
@@ -140,6 +141,7 @@ out input
140141
Debug.Log($"Unexpected exception when trying to initialize communication: {ex}");
141142
}
142143
initParametersOut = new UnityRLInitParameters();
144+
m_Channel.ShutdownAsync().Wait();
143145
return false;
144146
}
145147

@@ -181,6 +183,8 @@ out input
181183

182184
UpdateEnvironmentWithInput(input.RlInput);
183185
initParametersOut = initializationInput.RlInitializationInput.ToUnityRLInitParameters();
186+
// Be sure to shut down the grpc channel when the application is quitting.
187+
Application.quitting += NotifyQuitAndShutDownChannel;
184188
return true;
185189
#else
186190
initParametersOut = new UnityRLInitParameters();
@@ -217,9 +221,9 @@ void UpdateEnvironmentWithInput(UnityRLInputProto rlInput)
217221
UnityInputProto Initialize(int port, UnityOutputProto unityOutput, out UnityInputProto unityInput)
218222
{
219223
m_IsOpen = true;
220-
var channel = new Channel($"localhost:{port}", ChannelCredentials.Insecure);
224+
m_Channel = new Channel($"localhost:{port}", ChannelCredentials.Insecure);
221225

222-
m_Client = new UnityToExternalProto.UnityToExternalProtoClient(channel);
226+
m_Client = new UnityToExternalProto.UnityToExternalProtoClient(m_Channel);
223227
var result = m_Client.Exchange(WrapMessage(unityOutput, 200));
224228
var inputMessage = m_Client.Exchange(WrapMessage(null, 200));
225229
unityInput = inputMessage.UnityInput;
@@ -229,11 +233,17 @@ UnityInputProto Initialize(int port, UnityOutputProto unityOutput, out UnityInpu
229233
if (result.Header.Status != 200 || inputMessage.Header.Status != 200)
230234
{
231235
m_IsOpen = false;
232-
QuitCommandReceived?.Invoke();
236+
NotifyQuitAndShutDownChannel();
233237
}
234238
return result.UnityInput;
235239
}
236240

241+
void NotifyQuitAndShutDownChannel()
242+
{
243+
QuitCommandReceived?.Invoke();
244+
m_Channel.ShutdownAsync().Wait();
245+
}
246+
237247
#endregion
238248

239249
#region Destruction
@@ -269,7 +279,7 @@ void SendCommandEvent(CommandProto command)
269279
{
270280
case CommandProto.Quit:
271281
{
272-
QuitCommandReceived?.Invoke();
282+
NotifyQuitAndShutDownChannel();
273283
return;
274284
}
275285
case CommandProto.Reset:
@@ -456,7 +466,7 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
456466
// Not sure if the quit command is actually sent when a
457467
// non 200 message is received. Notify that we are indeed
458468
// quitting.
459-
QuitCommandReceived?.Invoke();
469+
NotifyQuitAndShutDownChannel();
460470
return message.UnityInput;
461471
}
462472
catch (Exception ex)
@@ -488,7 +498,7 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
488498
}
489499

490500
m_IsOpen = false;
491-
QuitCommandReceived?.Invoke();
501+
NotifyQuitAndShutDownChannel();
492502
return null;
493503
}
494504
}

0 commit comments

Comments
 (0)