LZorglub / TimeZone

Alternative to Windows .Net timezone
GNU General Public License v2.0
2 stars 2 forks source link

Optimized algorithm is not thread safe #6

Open rcm-adrien-gervaise opened 8 months ago

rcm-adrien-gervaise commented 8 months ago

Under heavily multi-threaded application, the optimized algorithm (optimize = true in e.g. TzTimeZone.ToUniversalTime) can throw NullReferenceException or other types of exceptions.

The cause is that, while only one thread at a time can populate the offset changes cache in UpdateDateChange, other threads can read the cache while it is being modified with List<TzTimeZoneRuleDate> knownDate = _zoneDates[datetime.Year];, which can result in non deterministic exceptions.

One simple solution would be to have UpdateDateChange return the list of offsets for the year, all accesses would then be from inside the lock on _zoneDates.

pmg23 commented 7 months ago

@LZorglub, would you be willing in principle to accept a PR that fixes this issue?

LZorglub commented 4 months ago

I dont see the use case of splitting years by blockcount and blocksize. Even in multi-threading threads work for close years. Isn't it simpler to not add the year in the dictionnary (UpdateDateChange) before fill it ?