Visitar URL original
Possible race condition in WithLatestFrom when setting the latest · Issue #227 · dotnet/reactive · GitHub
Skip to content

Possible race condition in WithLatestFrom when setting the latest #227

Closed

Description

@akarnokd

If I understand C# memory model correctly (should be very similar to Java's), there is a potential race when reading _latest in the main 'rail' and writing _latest it from the other 'rail'.

Since the value is not volatile here, if hasLatest has been set for a previous element, a partially setup/constructed object may be visible on the reader side. For example:

class MyValue {
   int field;
}

MyValue v = new MyValue()
v.field = 5;
// other.onNext(v);
_parent._latest = v; // this is plain write
// <-- writer gets interrupted here
_parent._hasLatest = true;

Even if constructing v has a full barrier, it is possible v.field = 5 gets reordered with _parent._latest = v and reading _latest.field on the other side may yield 0.

A solution would be to wrap TSecond into an object that is written via volatile write to the _latest field. Then, there is no need for hasLatest but check for null (or other special value) to see if there is one latest available or not.

Activity

  1. akarnokd commented on Aug 5, 2016

    @akarnokd
    CollaboratorAuthor

    Related PR: #247.

  2. danielcweber commented on May 25, 2018

    @danielcweber
    Collaborator

    #247 was merged, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions