www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Bug in generator

reply Profile Anaysis <PA gotacha.com> writes:
the code from 
https://dlang.org/library/std/concurrency/generator.html

gives a seg fault at the end.

import std.concurrency;
import std.stdio;


void main()
{
     auto tid = spawn(
     {
         while (true)
         {
             writeln(receiveOnly!int());
         }
     });

     auto r = new Generator!int(
     {
         foreach (i; 1 .. 10)
             yield(i);
     });

     foreach (e; r)
     {
         tid.send(e);
     }
}


0x00007FF7BDDA05E5 in 
std.concurrency.receiveOnly!int.receiveOnly.__lambda3 at 
\..\..\src\phobos\std\concurrency.d(805)
0x00007FF7BDDA1997 in void std.concurrency.Message.map!(pure 
 nogc  safe void 
function(std.concurrency.OwnerTerminated)*).map(pure  nogc  safe 
void function(std.concurrency.OwnerTerminated)*) at 
\..\..\src\phobos\std\concurrency.d(163)
0x00007FF7BDDA0B04 in 
D3std11concurrency10MessageBox160__T3getTDFNaNbNiNfiZvTPFNaNiNfC3std11concurrency14LinkTerminatedZvTPFNaNiNfC3std11concurrency15OwnerTerminatedZvTPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ3getMFDFNaNbNiNfiZvPFNaNiNfC3std11concurrency14LinkTerminatedZvPFNaNiNfC3std11concurrency15OwnerTerminatedZvPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ13onStandardMsgMFKS3std11concurrency7MessageZb
0x00007FF7BDDA0EA0 in 
D3std11concurrency10MessageBox160__T3getTDFNaNbNiNfiZvTPFNaNiNfC3std11concurrency14LinkTerminatedZvTPFNaNiNfC3std11concurrency15OwnerTerminatedZvTPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ3getMFDFNaNbNiNfiZvPFNaNiNfC3std11concurrency14LinkTerminatedZvPFNaNiNfC3std11concurrency15OwnerTerminatedZvPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ13onLinkDeadMsgMFKS3std11c
ncurrency7MessageZb at \..\..\src\phobos\std\concurrency.d(1988)
0x00007FF7BDDA0F4A in 
D3std11concurrency10MessageBox160__T3getTDFNaNbNiNfiZvTPFNaNiNfC3std11concurrency14LinkTerminatedZvTPFNaNiNfC3std11concurrency15OwnerTerminatedZvTPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ3getMFDFNaNbNiNfiZvPFNaNiNfC3std11concurrency14LinkTerminatedZvPFNaNiNfC3std11concurrency15OwnerTerminatedZvPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ12onControlMsgMFKS3std11c
ncurrency7MessageZb at \..\..\src\phobos\std\concurrency.d(2000)
0x00007FF7BDDA0FFA in 
D3std11concurrency10MessageBox160__T3getTDFNaNbNiNfiZvTPFNaNiNfC3std11concurrency14LinkTerminatedZvTPFNaNiNfC3std11concurrency15OwnerTerminatedZvTPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ3getMFDFNaNbNiNfiZvPFNaNiNfC3std11concurrency14LinkTerminatedZvPFNaNiNfC3std11concurrency15OwnerTerminatedZvPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ4scanMFKS3std11concurrency36__T4ListTS3std11concurr
ncy7MessageZ4ListZb at \..\..\src\phobos\std\concurrency.d(2016)
0x00007FF7BDDA094C in bool std.concurrency.MessageBox.get!(pure 
nothrow  nogc  safe void delegate(int), pure  nogc  safe void 
function(std.concurrency.LinkTerminated)*, pure  nogc  safe void 
function(std.concurrency.OwnerTerminated)*, void 
function(std.variant.VariantN!(20uL).VariantN)*).get(pure nothrow 
 nogc  safe void delegate(int), pure  nogc  safe void 
function(std.concurrency.LinkTerminated)*, pure  nogc  safe void 
function(std.concurrency.OwnerTerminated)*, void 
function(std.variant.VariantN!(20uL).VariantN)*) at 
\..\..\src\phobos\std\concurrency.d(2115)
0x00007FF7BDDA0579 in std.concurrency.receiveOnly!int.receiveOnly 
at \..\..\src\phobos\std\concurrency.d(806)
0x00007FF7BDD9F50D in main.main.__lambda1 at main.d(82)
0x00007FF7BDDA46C2 in std.concurrency._spawn!(void 
function())._spawn.exec at 
\..\..\src\phobos\std\concurrency.d(538)
0x00007FF7BDDE9302 in void core.thread.Thread.run()
0x00007FF7BDDD5CBD in thread_entryPoint
0x00007FF7BDE5D96D in thread_start<unsigned int (__cdecl*)(void * 
__ptr64)> at 
d:\th\minkernel\crts\ucrt\src\appcrt\startup\thread.cpp(115)
0x00007FF9EB178364 in BaseThreadInitThunk
0x00007FF9EBD570D1 in RtlUserThreadStart


Also, if one tries to create a global generator an error about 
PAGESIZE not being a compile time value is given.
Jan 30 2017
next sibling parent ag0aep6g <anonymous example.com> writes:
On 01/30/2017 11:58 AM, Profile Anaysis wrote:
 the code from https://dlang.org/library/std/concurrency/generator.html

 gives a seg fault at the end.

 import std.concurrency;
 import std.stdio;


 void main()
 {
     auto tid = spawn(
     {
         while (true)
         {
             writeln(receiveOnly!int());
         }
     });

     auto r = new Generator!int(
     {
         foreach (i; 1 .. 10)
             yield(i);
     });

     foreach (e; r)
     {
         tid.send(e);
     }
 }
[...] I don't see a segfault, but an exception: "std.concurrency.OwnerTerminated std/concurrency.d(241): Owner terminated". Which makes sense because the spawned thread still tries to receive ints after the main thread has finished. So, the example is bad and should be fixed. I've filed an issue: https://issues.dlang.org/show_bug.cgi?id=17127
Jan 30 2017
prev sibling parent ag0aep6g <anonymous example.com> writes:
On 01/30/2017 11:58 AM, Profile Anaysis wrote:
 Also, if one tries to create a global generator an error about PAGESIZE
 not being a compile time value is given.
That means you can't initialize it statically, because PAGESIZE is not known statically. But you can have a global (module scope, thread local) Generator. You just have to initialize it dynamically. ---- import std.concurrency: Generator, yield; /* No: */ /* Generator!int g = new Generator!int({ yield(42); }); */ /* Yes: */ Generator!int g1; void main() { g1 = new Generator!int({ yield(42); }); } /* Also yes: */ Generator!int g2; static this() { g2 = new Generator!int({ yield(42); }); } ----
Jan 30 2017