Skip to content

Commit 3e0907d

Browse files
committed
Only update the native library and the c# calls that need to be made to clean up channels. (#5283)
1 parent 8c49de6 commit 3e0907d

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

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

+10-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.
@@ -182,6 +183,8 @@ out input
182183

183184
UpdateEnvironmentWithInput(input.RlInput);
184185
initParametersOut = initializationInput.RlInitializationInput.ToUnityRLInitParameters();
186+
// Be sure to shut down the grpc channel when the application is quitting.
187+
Application.quitting += NotifyQuitAndShutDownChannel;
185188
return true;
186189
#else
187190
initParametersOut = new UnityRLInitParameters();
@@ -218,9 +221,9 @@ void UpdateEnvironmentWithInput(UnityRLInputProto rlInput)
218221
UnityInputProto Initialize(int port, UnityOutputProto unityOutput, out UnityInputProto unityInput)
219222
{
220223
m_IsOpen = true;
221-
var channel = new Channel($"localhost:{port}", ChannelCredentials.Insecure);
224+
m_Channel = new Channel($"localhost:{port}", ChannelCredentials.Insecure);
222225

223-
m_Client = new UnityToExternalProto.UnityToExternalProtoClient(channel);
226+
m_Client = new UnityToExternalProto.UnityToExternalProtoClient(m_Channel);
224227
var result = m_Client.Exchange(WrapMessage(unityOutput, 200));
225228
var inputMessage = m_Client.Exchange(WrapMessage(null, 200));
226229
unityInput = inputMessage.UnityInput;
@@ -230,7 +233,7 @@ UnityInputProto Initialize(int port, UnityOutputProto unityOutput, out UnityInpu
230233
if (result.Header.Status != 200 || inputMessage.Header.Status != 200)
231234
{
232235
m_IsOpen = false;
233-
QuitCommandReceived?.Invoke();
236+
NotifyQuitAndShutDownChannel();
234237
}
235238
return result.UnityInput;
236239
}
@@ -283,7 +286,7 @@ void SendCommandEvent(CommandProto command)
283286
{
284287
case CommandProto.Quit:
285288
{
286-
QuitCommandReceived?.Invoke();
289+
NotifyQuitAndShutDownChannel();
287290
return;
288291
}
289292
case CommandProto.Reset:
@@ -470,7 +473,7 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
470473
// Not sure if the quit command is actually sent when a
471474
// non 200 message is received. Notify that we are indeed
472475
// quitting.
473-
QuitCommandReceived?.Invoke();
476+
NotifyQuitAndShutDownChannel();
474477
return message.UnityInput;
475478
}
476479
catch (Exception ex)
@@ -502,7 +505,7 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
502505
}
503506

504507
m_IsOpen = false;
505-
QuitCommandReceived?.Invoke();
508+
NotifyQuitAndShutDownChannel();
506509
return null;
507510
}
508511
}

0 commit comments

Comments
 (0)