APIs not working when environment is set to integrated (SafeHandle Error)

Good morning,

After updating to 2.7.3 and using the integrated environment as the default env for APIs/Endpoints I get a " The remote server returned an error: (500) Internal Server Error." message with the following error in the logs.

I also get the following notification:

Invalid configuration: endpoints.ps1	9 minutes ago	
Safe handle has been closed. Object name: 'SafeHandle'.

Error in logs:

2022-02-01 08:02:28.248 -06:00 [ERR] An unhandled exception has occurred while executing the request.
System.ObjectDisposedException: Safe handle has been closed.
Object name: 'SafeHandle'.
   at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
   at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
   at Grpc.Core.Internal.ChannelSafeHandle.CreateCall(CallSafeHandle parentCall, ContextPropagationFlags propagationMask, CompletionQueueSafeHandle cq, String method, String host, Timespec deadline, CallCredentialsSafeHandle credentials)
   at Grpc.Core.Internal.AsyncCall`2.CreateNativeCall(CompletionQueueSafeHandle cq)
   at Grpc.Core.Internal.AsyncCall`2.Initialize(CompletionQueueSafeHandle cq)
   at Grpc.Core.Internal.AsyncCall`2.UnaryCallAsync(TRequest msg)
   at Grpc.Core.Calls.AsyncUnaryCall[TRequest,TResponse](CallInvocationDetails`2 call, TRequest req)
   at Grpc.Core.DefaultCallInvoker.AsyncUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request)
   at Grpc.Core.Interceptors.InterceptingCallInvoker.<AsyncUnaryCall>b__4_0[TRequest,TResponse](TRequest req, ClientInterceptorContext`2 ctx)
   at Grpc.Core.ClientBase.ClientBaseConfiguration.ClientBaseConfigurationInterceptor.AsyncUnaryCall[TRequest,TResponse](TRequest request, ClientInterceptorContext`2 context, AsyncUnaryCallContinuation`2 continuation)
   at Grpc.Core.Interceptors.InterceptingCallInvoker.AsyncUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request)
   at RestProtocol.AgentService.AgentServiceClient.executeRequestAsync(apiRequest request, CallOptions options) in D:\a\universal\universal\src\Protocol\obj\Release\netstandard2.0\protos\RestGrpc.cs:line 321
   at RestProtocol.AgentService.AgentServiceClient.executeRequestAsync(apiRequest request, Metadata headers, Nullable`1 deadline, CancellationToken cancellationToken) in D:\a\universal\universal\src\Protocol\obj\Release\netstandard2.0\protos\RestGrpc.cs:line 317
   at Universal.Server.Services.ApiGrpc.ExecuteAsync(apiRequest request, CancellationToken cancellationToken) in D:\a\universal\universal\src\Universal.Server\Services\API\ApiGrpc.cs:line 150
   at Universal.Server.Services.ApiService.ExecuteAsync(HttpContext httpContext) in D:\a\universal\universal\src\Universal.Server\Services\API\ApiService.cs:line 322
   at Universal.Server.Middleware.RoutingMiddleware.Invoke(HttpContext httpContext, IPolicyEvaluator policyEvaluator) in D:\a\universal\universal\src\Universal.Server\Middleware\RoutingMiddleware.cs:line 79
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Universal.Server.Middleware.SwaggerAuthenticationMiddleware.InvokeAsync(HttpContext context, RequestDelegate next) in D:\a\universal\universal\src\Universal.Server\Middleware\SwaggerAuthMiddleware.cs:line 42
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass6_1.<<UseMiddlewareInterface>b__1>d.MoveNext()
--- End of stack trace from previous location ---
   at AspNetCoreRateLimit.RateLimitMiddleware`1.Invoke(HttpContext context) in D:\a\universal\universal\src\AspNetCoreRateLimit\Middleware\RateLimitMiddleware.cs:line 109
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
2022-02-01 08:02:28.251 -06:00 [WRN] No exception handler was found, rethrowing original exception.
2022-02-01 08:02:28.251 -06:00 [ERR] Connection id "0HMF52NP4F2OC", Request id "0HMF52NP4F2OC:00000002": An unhandled exception was thrown by the application.
System.ObjectDisposedException: Safe handle has been closed.
Object name: 'SafeHandle'.
   at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
   at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
   at Grpc.Core.Internal.ChannelSafeHandle.CreateCall(CallSafeHandle parentCall, ContextPropagationFlags propagationMask, CompletionQueueSafeHandle cq, String method, String host, Timespec deadline, CallCredentialsSafeHandle credentials)
   at Grpc.Core.Internal.AsyncCall`2.CreateNativeCall(CompletionQueueSafeHandle cq)
   at Grpc.Core.Internal.AsyncCall`2.Initialize(CompletionQueueSafeHandle cq)
   at Grpc.Core.Internal.AsyncCall`2.UnaryCallAsync(TRequest msg)
   at Grpc.Core.Calls.AsyncUnaryCall[TRequest,TResponse](CallInvocationDetails`2 call, TRequest req)
   at Grpc.Core.DefaultCallInvoker.AsyncUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request)
   at Grpc.Core.Interceptors.InterceptingCallInvoker.<AsyncUnaryCall>b__4_0[TRequest,TResponse](TRequest req, ClientInterceptorContext`2 ctx)
   at Grpc.Core.ClientBase.ClientBaseConfiguration.ClientBaseConfigurationInterceptor.AsyncUnaryCall[TRequest,TResponse](TRequest request, ClientInterceptorContext`2 context, AsyncUnaryCallContinuation`2 continuation)
   at Grpc.Core.Interceptors.InterceptingCallInvoker.AsyncUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request)
   at RestProtocol.AgentService.AgentServiceClient.executeRequestAsync(apiRequest request, CallOptions options) in D:\a\universal\universal\src\Protocol\obj\Release\netstandard2.0\protos\RestGrpc.cs:line 321
   at RestProtocol.AgentService.AgentServiceClient.executeRequestAsync(apiRequest request, Metadata headers, Nullable`1 deadline, CancellationToken cancellationToken) in D:\a\universal\universal\src\Protocol\obj\Release\netstandard2.0\protos\RestGrpc.cs:line 317
   at Universal.Server.Services.ApiGrpc.ExecuteAsync(apiRequest request, CancellationToken cancellationToken) in D:\a\universal\universal\src\Universal.Server\Services\API\ApiGrpc.cs:line 150
   at Universal.Server.Services.ApiService.ExecuteAsync(HttpContext httpContext) in D:\a\universal\universal\src\Universal.Server\Services\API\ApiService.cs:line 322
   at Universal.Server.Middleware.RoutingMiddleware.Invoke(HttpContext httpContext, IPolicyEvaluator policyEvaluator) in D:\a\universal\universal\src\Universal.Server\Middleware\RoutingMiddleware.cs:line 79
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Universal.Server.Middleware.SwaggerAuthenticationMiddleware.InvokeAsync(HttpContext context, RequestDelegate next) in D:\a\universal\universal\src\Universal.Server\Middleware\SwaggerAuthMiddleware.cs:line 42
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass6_1.<<UseMiddlewareInterface>b__1>d.MoveNext()
--- End of stack trace from previous location ---
   at AspNetCoreRateLimit.RateLimitMiddleware`1.Invoke(HttpContext context) in D:\a\universal\universal\src\AspNetCoreRateLimit\Middleware\RateLimitMiddleware.cs:line 109
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.HandleException(HttpContext context, ExceptionDispatchInfo edi)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
   at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

Changing api environemnt to anything other than integrated fixes this issue. Any ideas?

Product: PowerShell Universal
Version: 2.7.3
``

I’ve only seen this happen once before and it didn’t happen all the time. I take it restarting the service causes the same issue?

Restarting the services doesn’t trigger that error.

Setting the API environment to “integrated” does trigger that error when trying a basic get request at an endpoint.

Not sure if my integrated env is messed up somehow? Running a standard script with the integrated env seems to work fine.