Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net datetime randomly duplicate time offset. is a bug?

i have a website developed in asp.net 4 and sql server 2008 R2. The problem is very complicated. I have a field in db with datatime offset UTC (ex. 2015-09-30 18:24:53.1498113 +02:00). Randomly (i think when application pool restart) this value return corrupt after query in .net like this: 30/09/2015 18:21:00 +02:00 +02:00 Time offset repeated 2 times!

So, when i parse the date in c# obviously i receive an error "String was not recognized as a valid DateTime".

If i recycle pool applcation page work fine

Why? It's a bug? Have you ever seen similar problem?

Thanks a lot

like image 698
alex Avatar asked Oct 01 '15 09:10

alex


2 Answers

We observed the same behaviour in our asp.net-mvc application (.NET 4.5) hosted at IIS. One of the servers started to generate duplicated offsets (no versions were deployed for weeks) when serializing DateTimeOffset value from model in razor cshtml in this apparently harmless code:

@Html.HiddenFor(m => m.CreateDate)
<span>@Model.CreateDate</span>

In both places duplicated offsets were generated like 2017-11-20 12:34 +01:00 +01:00. Restarting IIS pool solved the problem, but I have no idea how to avoid it in the future.

like image 70
PanJanek Avatar answered Sep 22 '22 03:09

PanJanek


This is being caused by a race condition in the ToString() method of DateTimeOffset. The race is caused because the code is directly working against the dateTimeOffsetPattern member when building the pattern string. The pattern string is the concatenation of the short date and long time patterns. Some cultures include the offset in the long time pattern, so the code checks for that. In the case where the offset is not included in the string, it concatenates the offset format like so:

if (!foundZ) {
    dateTimeOffsetPattern = dateTimeOffsetPattern + " zzz";
}

Depending on execution order, sometimes this gets executed twice or executed in one thread and not in another.

This has since been fixed in newer versions of the .Net Framework, but can otherwise be worked around by providing your own format string.

like image 37
Rob Avatar answered Sep 23 '22 03:09

Rob