2
2
#define MLA_SUPPORTED_TRAINING_PLATFORM
3
3
#endif
4
4
5
- # if MLA_SUPPORTED_TRAINING_PLATFORM
5
+ #if MLA_SUPPORTED_TRAINING_PLATFORM
6
6
using Grpc . Core ;
7
7
#if UNITY_EDITOR
8
8
using UnityEditor ;
@@ -50,6 +50,7 @@ internal class RpcCommunicator : ICommunicator
50
50
51
51
/// The Unity to External client.
52
52
UnityToExternalProto . UnityToExternalProtoClient m_Client ;
53
+ Channel m_Channel ;
53
54
54
55
/// <summary>
55
56
/// Initializes a new instance of the RPCCommunicator class.
@@ -140,6 +141,7 @@ out input
140
141
Debug . Log ( $ "Unexpected exception when trying to initialize communication: { ex } ") ;
141
142
}
142
143
initParametersOut = new UnityRLInitParameters ( ) ;
144
+ m_Channel . ShutdownAsync ( ) . Wait ( ) ;
143
145
return false ;
144
146
}
145
147
@@ -181,6 +183,8 @@ out input
181
183
182
184
UpdateEnvironmentWithInput ( input . RlInput ) ;
183
185
initParametersOut = initializationInput . RlInitializationInput . ToUnityRLInitParameters ( ) ;
186
+ // Be sure to shut down the grpc channel when the application is quitting.
187
+ Application . quitting += NotifyQuitAndShutDownChannel ;
184
188
return true ;
185
189
#else
186
190
initParametersOut = new UnityRLInitParameters ( ) ;
@@ -217,9 +221,9 @@ void UpdateEnvironmentWithInput(UnityRLInputProto rlInput)
217
221
UnityInputProto Initialize ( int port , UnityOutputProto unityOutput , out UnityInputProto unityInput )
218
222
{
219
223
m_IsOpen = true ;
220
- var channel = new Channel ( $ "localhost:{ port } ", ChannelCredentials . Insecure ) ;
224
+ m_Channel = new Channel ( $ "localhost:{ port } ", ChannelCredentials . Insecure ) ;
221
225
222
- m_Client = new UnityToExternalProto . UnityToExternalProtoClient ( channel ) ;
226
+ m_Client = new UnityToExternalProto . UnityToExternalProtoClient ( m_Channel ) ;
223
227
var result = m_Client . Exchange ( WrapMessage ( unityOutput , 200 ) ) ;
224
228
var inputMessage = m_Client . Exchange ( WrapMessage ( null , 200 ) ) ;
225
229
unityInput = inputMessage . UnityInput ;
@@ -229,11 +233,17 @@ UnityInputProto Initialize(int port, UnityOutputProto unityOutput, out UnityInpu
229
233
if ( result . Header . Status != 200 || inputMessage . Header . Status != 200 )
230
234
{
231
235
m_IsOpen = false ;
232
- QuitCommandReceived ? . Invoke ( ) ;
236
+ NotifyQuitAndShutDownChannel ( ) ;
233
237
}
234
238
return result . UnityInput ;
235
239
}
236
240
241
+ void NotifyQuitAndShutDownChannel ( )
242
+ {
243
+ QuitCommandReceived ? . Invoke ( ) ;
244
+ m_Channel . ShutdownAsync ( ) . Wait ( ) ;
245
+ }
246
+
237
247
#endregion
238
248
239
249
#region Destruction
@@ -269,7 +279,7 @@ void SendCommandEvent(CommandProto command)
269
279
{
270
280
case CommandProto . Quit :
271
281
{
272
- QuitCommandReceived ? . Invoke ( ) ;
282
+ NotifyQuitAndShutDownChannel ( ) ;
273
283
return ;
274
284
}
275
285
case CommandProto . Reset :
@@ -456,7 +466,7 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
456
466
// Not sure if the quit command is actually sent when a
457
467
// non 200 message is received. Notify that we are indeed
458
468
// quitting.
459
- QuitCommandReceived ? . Invoke ( ) ;
469
+ NotifyQuitAndShutDownChannel ( ) ;
460
470
return message . UnityInput ;
461
471
}
462
472
catch ( Exception ex )
@@ -488,7 +498,7 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
488
498
}
489
499
490
500
m_IsOpen = false ;
491
- QuitCommandReceived ? . Invoke ( ) ;
501
+ NotifyQuitAndShutDownChannel ( ) ;
492
502
return null ;
493
503
}
494
504
}
0 commit comments