Skip to content

Commit d96398d

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

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

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

+36-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
22
using Grpc.Core;
33
#endif
4+
5+
#if MLA_SUPPORTED_TRAINING_PLATFORM
6+
using Grpc.Core;
47
#if UNITY_EDITOR
58
using UnityEditor;
69
#endif
@@ -49,9 +52,7 @@ internal class RpcCommunicator : ICommunicator
4952
# if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
5053
/// The Unity to External client.
5154
UnityToExternalProto.UnityToExternalProtoClient m_Client;
52-
#endif
53-
/// The communicator parameters sent at construction
54-
CommunicatorInitParameters m_CommunicatorInitParameters;
55+
Channel m_Channel;
5556

5657
/// <summary>
5758
/// Initializes a new instance of the RPCCommunicator class.
@@ -231,6 +232,10 @@ public UnityRLInitParameters Initialize(CommunicatorInitParameters initParameter
231232
);
232233
throw new UnityAgentsException("Incompatible trainer version.");
233234
}
235+
initParametersOut = new UnityRLInitParameters();
236+
m_Channel.ShutdownAsync().Wait();
237+
return false;
238+
}
234239

235240
var packageVersionSupported = CheckPythonPackageVersionIsSupported(pythonPackageVersion);
236241
if (!packageVersionSupported)
@@ -262,7 +267,14 @@ public UnityRLInitParameters Initialize(CommunicatorInitParameters initParameter
262267
}
263268

264269
UpdateEnvironmentWithInput(input.RlInput);
265-
return initializationInput.RlInitializationInput.ToUnityRLInitParameters();
270+
initParametersOut = initializationInput.RlInitializationInput.ToUnityRLInitParameters();
271+
// Be sure to shut down the grpc channel when the application is quitting.
272+
Application.quitting += NotifyQuitAndShutDownChannel;
273+
return true;
274+
#else
275+
initParametersOut = new UnityRLInitParameters();
276+
return false;
277+
#endif
266278
}
267279

268280
/// <summary>
@@ -296,24 +308,33 @@ UnityInputProto Initialize(UnityOutputProto unityOutput,
296308
{
297309
# if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
298310
m_IsOpen = true;
299-
var channel = new Channel(
300-
"localhost:" + m_CommunicatorInitParameters.port,
301-
ChannelCredentials.Insecure);
311+
m_Channel = new Channel($"localhost:{port}", ChannelCredentials.Insecure);
302312

303-
m_Client = new UnityToExternalProto.UnityToExternalProtoClient(channel);
313+
m_Client = new UnityToExternalProto.UnityToExternalProtoClient(m_Channel);
304314
var result = m_Client.Exchange(WrapMessage(unityOutput, 200));
305315
unityInput = m_Client.Exchange(WrapMessage(null, 200)).UnityInput;
306316
#if UNITY_EDITOR
307317
EditorApplication.playModeStateChanged += HandleOnPlayModeChanged;
308318
#endif
319+
if (result.Header.Status != 200 || inputMessage.Header.Status != 200)
320+
{
321+
m_IsOpen = false;
322+
NotifyQuitAndShutDownChannel();
323+
}
309324
return result.UnityInput;
310325
#else
311326
throw new UnityAgentsException(
312327
"You cannot perform training on this platform.");
313328
#endif
314329
}
315330

316-
#endregion
331+
void NotifyQuitAndShutDownChannel()
332+
{
333+
QuitCommandReceived?.Invoke();
334+
m_Channel.ShutdownAsync().Wait();
335+
}
336+
337+
#endregion
317338

318339
#region Destruction
319340

@@ -352,10 +373,10 @@ void SendCommandEvent(CommandProto command)
352373
switch (command)
353374
{
354375
case CommandProto.Quit:
355-
{
356-
QuitCommandReceived?.Invoke();
357-
return;
358-
}
376+
{
377+
NotifyQuitAndShutDownChannel();
378+
return;
379+
}
359380
case CommandProto.Reset:
360381
{
361382
foreach (var brainName in m_OrderedAgentsRequestingDecisions.Keys)
@@ -540,13 +561,13 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
540561
// Not sure if the quit command is actually sent when a
541562
// non 200 message is received. Notify that we are indeed
542563
// quitting.
543-
QuitCommandReceived?.Invoke();
564+
NotifyQuitAndShutDownChannel();
544565
return message.UnityInput;
545566
}
546567
catch
547568
{
548569
m_IsOpen = false;
549-
QuitCommandReceived?.Invoke();
570+
NotifyQuitAndShutDownChannel();
550571
return null;
551572
}
552573
#else

0 commit comments

Comments
 (0)