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

TryDequeue return True but get null result #2

Closed
lust4life opened this issue Jan 22, 2018 · 3 comments
Closed

TryDequeue return True but get null result #2

lust4life opened this issue Jan 22, 2018 · 3 comments
Labels

Comments

@lust4life
Copy link
Contributor

lust4life commented Jan 22, 2018

using System;
using System.Threading.Tasks;
using BenchmarkDotNet.Running;

namespace MPMCQueue.NET.Benchmarks
{
  class Program
  {
    static MPMCQueue queue { get; } = new MPMCQueue(2);
    static void Enqueue()
    {
      while (true) { queue.TryEnqueue(1); }
    }

    static void Dequeue()
    {
      while (true)
      {
        if (queue.TryDequeue(out object t) && t == null)
        {
          System.Console.WriteLine("Dequeue null");
          break;
        }
      }
    }

    static void Main(string[] args)
    {
      var t1 = Task.Run(() => Enqueue());
      var t2 = Task.Run(() => Dequeue());
      Task.WaitAll(t1, t2);
    }
  }
}

output:

λ  .\benchmarks\MPMCQueue.NET.Benchmarks\bin\Release\MPMCQueue.NET.Benchmarks.exe
Dequeue null
if (cell.Sequence == pos + 1 && Interlocked.CompareExchange(ref _dequeuePos, pos + 1, pos) == pos)
{
    result = Volatile.Read(ref cell.Element);
    buffer[index] = new Cell(pos + bufferMask + 1, null);  // maybe here is the problem, 
//    buffer[index].Element = null;
//    buffer[index].Sequence = pos + bufferMask + 1;
    return true;
}
00007ffe`162b0b6a e891325f5f      call    clr!JIT_CheckedWriteBarrier (00007ffe`758a3e00)
00007ffe`162b0b6f 8d443701        lea     eax,[rdi+rsi+1]
00007ffe`162b0b73 33d2            xor     edx,edx
00007ffe`162b0b75 8903            mov     dword ptr [rbx],eax
00007ffe`162b0b77 48895308        mov     qword ptr [rbx+8],rdx
00007ffe`162b0b7b b801000000      mov     eax,1
00007ffe`162b0b80 4883c420        add     rsp,20h

two mov here, if use struct constructor seems can not guarantee the order. Sequence should be modified after cell.Element

@lust4life
Copy link
Contributor Author

lust4life commented Jan 23, 2018

The C# ECMA specification guarantees that the following types will be written atomically: reference types, bool, char, byte, sbyte, short, ushort, uint, int and float. Values of other types—including user-defined value types—could be written into memory in multiple atomic writes. As a result, a reading thread could observe a torn value consisting of pieces of different values.

One caveat is that even the types that are normally read and written atomically (such as int) could be read or written non-atomically if the value is not correctly aligned in memory. Normally, C# will ensure that values are correctly aligned, but the user is able to override the alignment using the StructLayoutAttribute class (bit.ly/Tqa0MZ).

The C# Memory Model in Theory and Practice
atomicity-volatility-and-immutability-are-different-part-one

@alexandrnikitin
Copy link
Owner

@lust4life , thank you for the issue! Indeed, you are right! It happens because we first write sequence and then element in dequeue (which overwrites the enqueued element).

@alexandrnikitin
Copy link
Owner

Closed by #5

lust4life added a commit to lust4life/protoactor-dotnet that referenced this issue Jan 25, 2018
rogeralsing pushed a commit to asynkron/protoactor-dotnet that referenced this issue Sep 22, 2018
* Update build.cake

* implement stopping when max number of restarts exceeds allowed number in a period of time

* Removed _applySnapshot callback after persisting a snapshot.

* address issue (TryDequeue return True but get null result)

alexandrnikitin/MPMCQueue.NET#2

* No need to count messages

* Fix 2 csproj files causing Visual Studio for Mac not load bug.

* fix: utilize EventStream.Instance for DI example (#387)

* added ActorPropsRegistry.RegisterProps(Type, Func<Props, Props> (#378)

* Implement Forward.

* Fix csprojs.

* Reverse back RavenDB.

* Add ContextState.Stopped.

* Allow InvokeSystemMessageAsync in any state.

* Temp fix appveyor.yml cake version.

* .

* Send EndpointWatcher/EndpointWriter Stop message in Actor.

* Report error if receive user message after Actor already stooped.

* Fix test codes.

* Add StopAsync/Poison/PoisonAsync

* .

* Update dependencies

* Fix test codes.

* Update dependency for example/RemoteActivate.

* renamed RootContext => ActorClient. To be constitent with the class name.

* ActorClient.RequestAsync and example

* Moved Tell & Request from IContext to ISenderContext

* Mutable MessageEnvelope

* ISenderContext Message mutable

* static readonly mutable EmptyHeaders was really dangerous. Made it a new instance for each call.

* middlewares messages manipulation example

* Big Breaking Change

* tell -> send
* move send to extension
* revert mutating messages
* copyrights
* bugfix in actorcontext
* RootContext
* removed pid request
* cleanup router tests
* clean up tests
* cleanup
* more clean up
* even more
* immutable message headers
* better receive middleware
* looping spawn benchmark
* use props in actorcontext
* remove 1.5
* globaljson

* bugfix in rootcontext

* move Actor.Spawn to Context.Spawn

* Move props creating funcs from Actor to Props

* Code cleanup

* WIP : Single remote instance cluster- do not merge (#408)

* SingleRemoteInstanceProvider draft

* Should work but don't :-|

* working SingleRemoteInstanceProvider

* trimed csproj

* Docs

* formatting

* context proxy

* decorator context

* decorators works

* root context decorator

* fix IContext

* RootContext.With*

* WIP : OpenTracing support - do not merge (#406)

* OpenTracing first blood

* WithOpenTracingSenderTest

* oups

* OpenTracingReceiverMiddlewareTests

* jaeger working now ! (should I have read the manual sooner !)

* log messages

* ProtoTags targetPID & senderPID

* OpenTracing complex interactions example

* trying to play with decorator

* OpenTracing improvements with decorators

* fix non compiling tests

* remove globaljson

* Opentracing improvements (#412)

* OpenTracing first blood

* WithOpenTracingSenderTest

* oups

* OpenTracingReceiverMiddlewareTests

* jaeger working now ! (should I have read the manual sooner !)

* log messages

* ProtoTags targetPID & senderPID

* OpenTracing complex interactions example

* trying to play with decorator

* OpenTracing improvements with decorators

* OpenTracing improvements

* moved Proto.OpenTracing.Tests to "test" solution folder

* tried to have OpenTracing receive in ContextDecorator but failed

* OpenTracing back on tracks with middlewares

* unit tests unbroked and improved

* ensure decorator.Receive is called

* fix context decorator for actors without receive middleware

* finally receive decorator is working fine, but only triggered when at least one receive middleware (#414)

* Chainable context decorators (#417)

* Life cycle events : write deadletters, just in case

* OpenTracing commented code out

* Chainable context decorators

* Fixed issue with namespaces with deeper nesting (#419)

* Bump to 0.2.0

* Copy GuardianStrategy when copy Props.

* targeting netstandard2.0 and net452
rogeralsing added a commit to asynkron/protoactor-dotnet that referenced this issue Mar 6, 2020
* Release 0.2.0 (#421)

* Update build.cake

* implement stopping when max number of restarts exceeds allowed number in a period of time

* Removed _applySnapshot callback after persisting a snapshot.

* address issue (TryDequeue return True but get null result)

alexandrnikitin/MPMCQueue.NET#2

* No need to count messages

* Fix 2 csproj files causing Visual Studio for Mac not load bug.

* fix: utilize EventStream.Instance for DI example (#387)

* added ActorPropsRegistry.RegisterProps(Type, Func<Props, Props> (#378)

* Implement Forward.

* Fix csprojs.

* Reverse back RavenDB.

* Add ContextState.Stopped.

* Allow InvokeSystemMessageAsync in any state.

* Temp fix appveyor.yml cake version.

* .

* Send EndpointWatcher/EndpointWriter Stop message in Actor.

* Report error if receive user message after Actor already stooped.

* Fix test codes.

* Add StopAsync/Poison/PoisonAsync

* .

* Update dependencies

* Fix test codes.

* Update dependency for example/RemoteActivate.

* renamed RootContext => ActorClient. To be constitent with the class name.

* ActorClient.RequestAsync and example

* Moved Tell & Request from IContext to ISenderContext

* Mutable MessageEnvelope

* ISenderContext Message mutable

* static readonly mutable EmptyHeaders was really dangerous. Made it a new instance for each call.

* middlewares messages manipulation example

* Big Breaking Change

* tell -> send
* move send to extension
* revert mutating messages
* copyrights
* bugfix in actorcontext
* RootContext
* removed pid request
* cleanup router tests
* clean up tests
* cleanup
* more clean up
* even more
* immutable message headers
* better receive middleware
* looping spawn benchmark
* use props in actorcontext
* remove 1.5
* globaljson

* bugfix in rootcontext

* move Actor.Spawn to Context.Spawn

* Move props creating funcs from Actor to Props

* Code cleanup

* WIP : Single remote instance cluster- do not merge (#408)

* SingleRemoteInstanceProvider draft

* Should work but don't :-|

* working SingleRemoteInstanceProvider

* trimed csproj

* Docs

* formatting

* context proxy

* decorator context

* decorators works

* root context decorator

* fix IContext

* RootContext.With*

* WIP : OpenTracing support - do not merge (#406)

* OpenTracing first blood

* WithOpenTracingSenderTest

* oups

* OpenTracingReceiverMiddlewareTests

* jaeger working now ! (should I have read the manual sooner !)

* log messages

* ProtoTags targetPID & senderPID

* OpenTracing complex interactions example

* trying to play with decorator

* OpenTracing improvements with decorators

* fix non compiling tests

* remove globaljson

* Opentracing improvements (#412)

* OpenTracing first blood

* WithOpenTracingSenderTest

* oups

* OpenTracingReceiverMiddlewareTests

* jaeger working now ! (should I have read the manual sooner !)

* log messages

* ProtoTags targetPID & senderPID

* OpenTracing complex interactions example

* trying to play with decorator

* OpenTracing improvements with decorators

* OpenTracing improvements

* moved Proto.OpenTracing.Tests to "test" solution folder

* tried to have OpenTracing receive in ContextDecorator but failed

* OpenTracing back on tracks with middlewares

* unit tests unbroked and improved

* ensure decorator.Receive is called

* fix context decorator for actors without receive middleware

* finally receive decorator is working fine, but only triggered when at least one receive middleware (#414)

* Chainable context decorators (#417)

* Life cycle events : write deadletters, just in case

* OpenTracing commented code out

* Chainable context decorators

* Fixed issue with namespaces with deeper nesting (#419)

* Bump to 0.2.0

* Copy GuardianStrategy when copy Props.

* targeting netstandard2.0 and net452

* Upgrading code, packages, a bit of cleanup

* Merge, cleaning up mongo and logs

* Update Appveyor image

* Fixing the remote manager

* Fix target framework

* Fixes #403 (I hope)

Co-authored-by: Roger Johansson <rogeralsing@gmail.com>
Co-authored-by: Christian Palmstierna <christian.palmstierna@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants