potential problem with generated serializer code
Using Orleans 1.4.1, I am getting these exceptions.
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at Orleans.Serialization.BuiltInTypes.SerializeList[T](Object obj, ISerializationContext context, Type expected)
at Orleans.Serialization.SerializationManager.SerializeInner(Object obj, ISerializationContext context, Type expected)
at NotificationServer.Model.OrleansCodeGenNotificationServer_Model_WellSubscriptionEventSerializer.Serializer(Object untypedInput, ISerializationContext context, Type expected) in C:\Source\myproject\NotificationServer\NotificationServer.Model\obj\Debug\NotificationServer.Model.orleans.g.cs:line 414
at Orleans.Serialization.SerializationManager.SerializeInner(Object obj, ISerializationContext context, Type expected)
at Orleans.Serialization.BuiltInTypes.SerializeImmutable[T](Object original, ISerializationContext context, Type expected)
at Orleans.Serialization.SerializationManager.SerializeInner(Object obj, ISerializationContext context, Type expected)
at Orleans.Serialization.BuiltInTypes.SerializeInvokeMethodRequest(Object obj, ISerializationContext context, Type expected)
at Orleans.Serialization.SerializationManager.SerializeInner(Object obj, ISerializationContext context, Type expected)
at Orleans.Serialization.SerializationManager.Serialize(Object raw, BinaryTokenStreamWriter stream)
at Orleans.Runtime.Message.Serialize_Impl(Int32& headerLengthOut, Int32& bodyLengthOut)
at Orleans.Runtime.Messaging.Gateway.GatewaySender.Send(Message msg, Socket sock)
The line of code referred to is part of the generated code file (*.g.cs). Line 414 of it says:
global::Orleans.Serialization.SerializationManager.@SerializeInner(input.@WellIDs, context, typeof (global::System.Collections.Generic.List<global::System.Guid>));
Here is the whole generated class:
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Orleans-CodeGenerator", "1.4.1.0"), global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute, global::Orleans.CodeGeneration.SerializerAttribute(typeof (global::NotificationServer.Model.WellSubscriptionEvent))]
internal class OrleansCodeGenNotificationServer_Model_WellSubscriptionEventSerializer
{
[global::Orleans.CodeGeneration.CopierMethodAttribute]
public static global::System.Object DeepCopier(global::System.Object original, global::Orleans.Serialization.ICopyContext context)
{
global::NotificationServer.Model.WellSubscriptionEvent input = ((global::NotificationServer.Model.WellSubscriptionEvent)original);
global::NotificationServer.Model.WellSubscriptionEvent result = new global::NotificationServer.Model.WellSubscriptionEvent();
context.@RecordCopy(original, result);
result.@SubscribeAction = input.@SubscribeAction;
result.@WellIDs = (global::System.Collections.Generic.List<global::System.Guid>)global::Orleans.Serialization.SerializationManager.@DeepCopyInner(input.@WellIDs, context);
return result;
}
[global::Orleans.CodeGeneration.SerializerMethodAttribute]
public static void Serializer(global::System.Object untypedInput, global::Orleans.Serialization.ISerializationContext context, global::System.Type expected)
{
global::NotificationServer.Model.WellSubscriptionEvent input = (global::NotificationServer.Model.WellSubscriptionEvent)untypedInput;
global::Orleans.Serialization.SerializationManager.@SerializeInner(input.@SubscribeAction, context, typeof (global::NotificationServer.Model.WellSubscribeAction));
global::Orleans.Serialization.SerializationManager.@SerializeInner(input.@WellIDs, context, typeof (global::System.Collections.Generic.List<global::System.Guid>)); // line 414
}
[global::Orleans.CodeGeneration.DeserializerMethodAttribute]
public static global::System.Object Deserializer(global::System.Type expected, global::Orleans.Serialization.IDeserializationContext context)
{
global::NotificationServer.Model.WellSubscriptionEvent result = new global::NotificationServer.Model.WellSubscriptionEvent();
context.@RecordObject(result);
result.@SubscribeAction = (global::NotificationServer.Model.WellSubscribeAction)global::Orleans.Serialization.SerializationManager.@DeserializeInner(typeof (global::NotificationServer.Model.WellSubscribeAction), context);
result.@WellIDs = (global::System.Collections.Generic.List<global::System.Guid>)global::Orleans.Serialization.SerializationManager.@DeserializeInner(typeof (global::System.Collections.Generic.List<global::System.Guid>), context);
return (global::NotificationServer.Model.WellSubscriptionEvent)result;
}
}
The serializer's target type has just two (public) properties: an enum and a List<Guid>. The class is public and has the Serializable attribute on it.
What am I missing? Why would I get this error from the generated code?
Is it possible that this instance is being shared between multiple threads? It seems like a concurrency issue.
Shared instance of what?
The serialized object is created just prior to sending it via streamHandle.OnNextAsync(msgObj). Each time GetStream
There's only one grain (of one grain type) that makes these calls to OnNextAsync. So I was hoping Orleans would provide the needed concurrency control. But shouldn't publishing messages to a virtual stream be thread-safe, in case other grains on the same machine are publishing to the same virtual stream on a silo?
Is the static nature of the generated code a source of concern for concurrency?
Oddly, the rest of the object is serialized, and the List<Guid> on the other end is usually an empty but initialized List<Guid>. Sometimes it's populated.
The grain type is [Reentrant] but the relevant methods making those calls... aren't awaiting any Task calls.
Shared instance of the object being serialized. Do you mind showing me some of the code involved in the repro?
Serialized type:
[Serializable]
public class WellSubscriptionEvent
{
public WellSubscribeAction SubscribeAction { get; set; }
public List<Guid> WellIDs { get; set; }
public override string ToString() => "...";
}
And from one of the locations in code that is publishing these messages:
WellSubscriptionEventsStream = GetStreamProvider(StreamProviderNames.SimpleMessageStreamProvider)
.GetStream<WellSubscriptionEvent>(Guid.Empty, StreamNames.WellSubscriptionEvents);
if (WellSubscriptionEventsStream != null)
{
var subscription = new WellSubscriptionEvent
{
SubscribeAction = WellSubscribeAction.Unsubscribe,
WellIDs = idBatch
};
var subscriptionTask = WellSubscriptionEventsStream.OnNextAsync(subscription);
var continueTask = subscriptionTask.ContinueWith(task =>
{
if (task.Status == TaskStatus.RanToCompletion)
logger.IncrementMetric(Metrics.WellSubscriptionAddRequests);
else
logger.IncrementMetric(Metrics.WellSubscriptionAddRequestsFailed);
});
}
idBatch is a batch of 1-50 Guids.
The code above isn't awaiting OnNextAsync, capturing metrics for failures vs successes (both exist) instead of failing the whole process.
I did originally await the OnNextAsync calls, and they failed quite often.
I am also getting a lot of these TimeoutExceptions related to the same SMS stream provider:
System.TimeoutException: Response did not arrive on time in 00:00:30 for message: Request S192.168.21.93:11111:233361443grn/6E69F565/00000000@ad52d08b->S192.168.21.95:11111:233361451cli/69b25a93@929308b8d277760c-b4c4-40bd-8b69-98a2540ea601 #75341: global::Orleans.Streams.IStreamConsumerExtension:DeliverImmutable(). Target History is: S192.168.21.95:11111:233361451:*cli/69b25a93:@929308b8.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Orleans.Providers.Streams.SimpleMessageStream.SimpleMessageStreamProducerExtension.StreamConsumerExtensionCollection.d__7.MoveNext() --- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Orleans.Providers.Streams.SimpleMessageStream.SimpleMessageStreamProducer`1.d__19.MoveNext()
could you show me where idBatch is being created and modified?
var idBatch = new List<Guid>();
var index = 0;
while (index < ids.Count)
{
idBatch.Add(ids[index]);
if (index == ids.Count - 1 || (index > 0 && index % 50 == 0))
{
if (idBatch.Count > 0)
{
WellSubscriptionEventsStream = GetStreamProvider(StreamProviderNames.SimpleMessageStreamProvider)
.GetStream<WellSubscriptionEvent>(Guid.Empty, StreamNames.WellSubscriptionEvents);
if (WellSubscriptionEventsStream != null)
{
var subscription = new WellSubscriptionEvent
{
SubscribeAction = WellSubscribeAction.Unsubscribe,
WellIDs = idBatch
};
var subscriptionTask = WellSubscriptionEventsStream.OnNextAsync(subscription);
var continueTask = subscriptionTask.ContinueWith(task =>
{
if (task.Status == TaskStatus.RanToCompletion)
logger.IncrementMetric(Metrics.WellSubscriptionRemoveRequests);
else
logger.IncrementMetric(Metrics.WellSubscriptionRemoveRequestsFailed);
});
}
else
logger.TrackEvent("RT.WellSubscriptionGrain.ReportUnneededWellSubscriptions:WellSubscriptionEventsStream.IsNull");
idBatch.Clear();
}
}
index++;
}
Thanks, @danvanderboom, I'll investigate with the team. In the meantime, as a workaround, change this part:
var subscription = new WellSubscriptionEvent
{
SubscribeAction = WellSubscribeAction.Unsubscribe,
WellIDs = idBatch
};
To:
var subscription = new WellSubscriptionEvent
{
SubscribeAction = WellSubscribeAction.Unsubscribe,
WellIDs = new List<Guid>(idBatch)
};
One more question, could you show me how you've configured the stream provider? Thanks
On the ClusterConfiguration object:
config.AddSimpleMessageStreamProvider(StreamProviderNames.SimpleMessageStreamProvider);
StreamProviderNames.SimpleMessageStreamProvider = "SMSProvider"
Here's another example of it happening, this time with no List of anything within the serialized object, just a few primitive data types:
System.TimeoutException: Response did not arrive on time in 00:00:30 for message: Request S192.168.21.93:40000:0*grn/37B24DAA/36e6459b@6d95de86->*cli/cf349c89@d4560798e81cb8d2-a367-49e3-9173-af93d4878118 #2345: global::Orleans.Streams.IStreamConsumerExtension:DeliverImmutable(). Target History is: <*cli/cf349c89:@d4560798>.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Orleans.Providers.Streams.SimpleMessageStream.SimpleMessageStreamProducerExtension.StreamConsumerExtensionCollection.d__7.MoveNext() --- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Orleans.Providers.Streams.SimpleMessageStream.SimpleMessageStreamProducer`1.d__19.MoveNext() --- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at NotificationServer.Grains.WellGrain.<SaveChannelAlarms>d__20.MoveNext() in C:\Source\myproject\NotificationServer.Grains\Grains\WellGrain.cs:line 155
The SaveChannelAlarms method starts like this:
public async Task SaveChannelAlarms(IList<ChannelAlarm> alarms)
{
try
{
await ChannelAlarmsData.SaveChannelAlarms(alarms);
foreach (var alarm in alarms)
{
try
{
await ChannelAlarmUpdateStream.OnNextAsync(new ChannelAlarmUpdate
{
Update = UpdateType.AddOrEdit,
UserID = alarm.UserID,
WellID = alarm.WellID,
ChannelID = alarm.ChannelID
});
}
It's the call to ChannelAlarmUpdateStream.OnNextAsync (on line 155) that causes the timeout.
ChannelAlarmUpdate is simply:
[Serializable]
public class ChannelAlarmUpdate
{
public UpdateType Update { get; set; } // AddOrEdit, Remove
public long UserID { get; set; }
public Guid WellID { get; set; }
public string ChannelID { get; set; }
}
That seems to be something different. For that one, could you show me the generated serializer?
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Orleans-CodeGenerator", "1.4.1.0"), global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute, global::Orleans.CodeGeneration.SerializerAttribute(typeof (global::NotificationServer.Model.ChannelAlarmUpdate))]
internal class OrleansCodeGenNotificationServer_Model_ChannelAlarmUpdateSerializer
{
[global::Orleans.CodeGeneration.CopierMethodAttribute]
public static global::System.Object DeepCopier(global::System.Object original, global::Orleans.Serialization.ICopyContext context)
{
global::NotificationServer.Model.ChannelAlarmUpdate input = ((global::NotificationServer.Model.ChannelAlarmUpdate)original);
global::NotificationServer.Model.ChannelAlarmUpdate result = new global::NotificationServer.Model.ChannelAlarmUpdate();
context.@RecordCopy(original, result);
result.@ChannelID = input.@ChannelID;
result.@Update = input.@Update;
result.@UserID = input.@UserID;
result.@WellID = (global::System.Guid)global::Orleans.Serialization.SerializationManager.@DeepCopyInner(input.@WellID, context);
return result;
}
[global::Orleans.CodeGeneration.SerializerMethodAttribute]
public static void Serializer(global::System.Object untypedInput, global::Orleans.Serialization.ISerializationContext context, global::System.Type expected)
{
global::NotificationServer.Model.ChannelAlarmUpdate input = (global::NotificationServer.Model.ChannelAlarmUpdate)untypedInput;
global::Orleans.Serialization.SerializationManager.@SerializeInner(input.@ChannelID, context, typeof (global::System.String));
global::Orleans.Serialization.SerializationManager.@SerializeInner(input.@Update, context, typeof (global::NotificationServer.Model.UpdateType));
global::Orleans.Serialization.SerializationManager.@SerializeInner(input.@UserID, context, typeof (global::System.Int64));
global::Orleans.Serialization.SerializationManager.@SerializeInner(input.@WellID, context, typeof (global::System.Guid));
}
[global::Orleans.CodeGeneration.DeserializerMethodAttribute]
public static global::System.Object Deserializer(global::System.Type expected, global::Orleans.Serialization.IDeserializationContext context)
{
global::NotificationServer.Model.ChannelAlarmUpdate result = new global::NotificationServer.Model.ChannelAlarmUpdate();
context.@RecordObject(result);
result.@ChannelID = (global::System.String)global::Orleans.Serialization.SerializationManager.@DeserializeInner(typeof (global::System.String), context);
result.@Update = (global::NotificationServer.Model.UpdateType)global::Orleans.Serialization.SerializationManager.@DeserializeInner(typeof (global::NotificationServer.Model.UpdateType), context);
result.@UserID = (global::System.Int64)global::Orleans.Serialization.SerializationManager.@DeserializeInner(typeof (global::System.Int64), context);
result.@WellID = (global::System.Guid)global::Orleans.Serialization.SerializationManager.@DeserializeInner(typeof (global::System.Guid), context);
return (global::NotificationServer.Model.ChannelAlarmUpdate)result;
}
}
I'm also seeing this error. Virtual stream related. Maybe the exception being thrown here has something to do with a downstream TimeoutException?
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Orleans.Providers.Streams.SimpleMessageStream.SimpleMessageStreamProducerExtension.StreamConsumerExtensionCollection.<DeliverToRemote>d__7.MoveNext() --- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Orleans.Providers.Streams.SimpleMessageStream.SimpleMessageStreamProducer`1.<OnNextAsync>d__19.MoveNext()
After updating SMS config to this:
config.AddSimpleMessageStreamProvider(StreamProviderNames.SimpleMessageStreamProvider, fireAndForgetDelivery: true);
... we are no longer getting the TimeoutExceptions and NullReferenceExceptions reported in this issue.
I believe we have an Orleans client, who subscribes to virtual streams, and isn't closing down its connection gracefully before trying to reinitialize the client and re-establish a lost (or apparently lost) connection. Awaiting a potentially misbehaving client stream subscriber/consumer seems like a bad idea, at least in our scenario.
So far, we're not seeing the deluge of exceptions in our logs, but I'll be doing lots of testing to be sure.
@danvanderboom one of the issues you're experiencing has been fixed in #3048. As for the timeout/nullref issue, I'm not sure - maybe @xiazen has an idea, or @jason-bragg
if you turn on fireAndForgetDelivery, the producer will fire the async deliver call, but ignore the result. task.Ignore. That's probably why you stopped receiving those TimeOut and NullReferenceExceptions.
It is possible that there's a bug or a timing issue in the scenario you brought up. I will look into it.
For the TimeOutException, can you show me what the consumer grain and its observer onNextAsync looks like?
If the consumer grain's onNextAsync logic has multiple await async call, it is possible they never make it back in time or not at all
EDIT: producer send a remote message to consumer to deliver the item. this call will trigger the observer's onNextAsync method, if that method took too long time doing storage saving or anything alike, then the producer will receive a timeout. I realize I might be speaking too vogue without proper explanation. so added the EDIT
for the NullReferenceExceptions , it is strange. The client you mentioned which disconnect ungracefully, is it a producer or a consumer?
The initial issue was fixed in #3048, so I suggest we close this and open separate issues for the other issues. Does that sound fine to you, @danvanderboom?