Skip to content

Commit

Permalink
Kill EA on ObjDispException (#1254)
Browse files Browse the repository at this point in the history
  • Loading branch information
varunpuranik authored May 24, 2019
1 parent e458e14 commit bbc8d3c
Showing 1 changed file with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Microsoft.Azure.Devices.Edge.Agent.IoTHub
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client;
Expand All @@ -24,6 +25,12 @@ public class ModuleClient : IModuleClient
static readonly RetryStrategy TransientRetryStrategy =
new ExponentialBackoff(int.MaxValue, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(4));

static readonly Type[] NonRecoverableExceptions =
{
typeof(NullReferenceException),
typeof(ObjectDisposedException)
};

readonly Client.ModuleClient deviceClient;

ModuleClient(Client.ModuleClient deviceClient)
Expand All @@ -48,9 +55,41 @@ public Task SetMethodHandlerAsync(string methodName, MethodCallback callback) =>

public void Dispose() => this.deviceClient.Dispose();

public Task<Twin> GetTwinAsync() => this.deviceClient.GetTwinAsync();
public async Task<Twin> GetTwinAsync()
{
try
{
return await this.deviceClient.GetTwinAsync();
}
catch (Exception ex)
{
if (NonRecoverableExceptions.Any(e => e.IsInstanceOfType(ex)))
{
Events.NonRecoverableException(ex, "GetTwinAsync");
Environment.Exit(1);
}

public Task UpdateReportedPropertiesAsync(TwinCollection reportedProperties) => this.deviceClient.UpdateReportedPropertiesAsync(reportedProperties);
throw;
}
}

public async Task UpdateReportedPropertiesAsync(TwinCollection reportedProperties)
{
try
{
await this.deviceClient.UpdateReportedPropertiesAsync(reportedProperties);
}
catch (Exception ex)
{
if (NonRecoverableExceptions.Any(e => e.IsInstanceOfType(ex)))
{
Events.NonRecoverableException(ex, "UpdateReportedProperties");
Environment.Exit(1);
}

throw;
}
}

internal static Task<Client.ModuleClient> CreateDeviceClientForUpstreamProtocol(
Option<UpstreamProtocol> upstreamProtocol,
Expand Down Expand Up @@ -197,7 +236,8 @@ enum EventIds
DeviceClientCreated,
DeviceConnectionError,
RetryingDeviceClientConnection,
DeviceClientSetupFailed
DeviceClientSetupFailed,
NonRecoverableException
}

public static void AttemptingConnectionWithTransport(TransportType transport)
Expand Down Expand Up @@ -231,6 +271,11 @@ public static void DeviceClientSetupFailed(Exception ex)
{
Log.LogError((int)EventIds.DeviceClientSetupFailed, ex, "Device client threw non-transient exception during setup");
}

public static void NonRecoverableException(Exception ex, string operation)
{
Log.LogError((int)EventIds.NonRecoverableException, ex, $"Got non-recoverable exception during operation {operation}");
}
}
}
}

0 comments on commit bbc8d3c

Please sign in to comment.