Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CalendarSerializer.SerializeToString is not thread safe #553

Open
IvaskevychYuriy opened this issue Aug 11, 2022 · 0 comments
Open

CalendarSerializer.SerializeToString is not thread safe #553

IvaskevychYuriy opened this issue Aug 11, 2022 · 0 comments

Comments

@IvaskevychYuriy
Copy link

Issue

Here's a small test:

[Test]
public void ConcurrentSerialization()
{
    var serializer = new CalendarSerializer();                      // (1)

    var results = Enumerable.Range(1, 100)
        .AsParallel()
        .Select(x =>
        {
            try
            {
                var cal = new Calendar();                                 // (2)
                cal.Events.Add(new CalendarEvent
                {
                    Start = new CalDateTime(DateTime.UtcNow),
                    End = new CalDateTime(DateTime.UtcNow.AddHours(1))
                });

                serializer.SerializeToString(cal);                       // (3)
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        })
        .ToList();

    Assert.IsEmpty(results.Where(x => !x));
}

Basically, having one serializer instance (line marked (1)) serialize (3) calendars (2) in different threads is not thread safe - exceptions are thrown all over the place (index out of range, null reference and others)
That's scenario A: (1) shared, (2) created every time

Other combinations are not thread safe too:
Scenario B: (1) shared, (2) also shared
Scenario C: (1) created every time, (2) shared

Workaround?

The only case it works is when new instances of serializer (1) and calendar (2) are created every time, so basically (1), (2) and (3) are all inside try block in above example.

Thoughts

I'm not sure if this can be addressed and fixed easily unfortunatelly. This happens as far as I can tell because serializer mutates a calendar, here's even an issue related to this

Anyway, I'll leave this here in case someone finds it helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant