private async Task<ActivationData> CallGrainDeactivateAndCleanupStreams(ActivationData activation) { try { var grainTypeName = activation.GrainInstanceType.FullName; // Note: This call is being made from within Scheduler.Queue wrapper, so we are already executing on worker thread if (logger.IsVerbose) logger.Verbose(ErrorCode.Catalog_BeforeCallingDeactivate, "About to call {1} grain's OnDeactivateAsync() method {0}", activation, grainTypeName); // Call OnDeactivateAsync inline, but within try-catch wrapper to safely capture any exceptions thrown from called function try { // just check in case this activation data is already Invalid or not here at all. ActivationData ignore; if (TryGetActivationData(activation.ActivationId, out ignore) && activation.State == ActivationState.Deactivating) { await activation.GrainInstance.OnDeactivateAsync(); } if (logger.IsVerbose) logger.Verbose(ErrorCode.Catalog_AfterCallingDeactivate, "Returned from calling {1} grain's OnDeactivateAsync() method {0}", activation, grainTypeName); } catch (Exception exc) { logger.Error(ErrorCode.Catalog_ErrorCallingDeactivate, string.Format("Error calling grain's OnDeactivateAsync() method - Grain type = {1} Activation = {0}", activation, grainTypeName), exc); } if (activation.IsUsingStreams) { try { await activation.DeactivateStreamResources(); } catch (Exception exc) { logger.Warn(ErrorCode.Catalog_DeactivateStreamResources_Exception, String.Format("DeactivateStreamResources Grain type = {0} Activation = {1} failed.", grainTypeName, activation), exc); } } } catch(Exception exc) { logger.Error(ErrorCode.Catalog_FinishGrainDeactivateAndCleanupStreams_Exception, String.Format("CallGrainDeactivateAndCleanupStreams Activation = {0} failed.", activation), exc); } return activation; }