[phobos] stdin.byChunk seems broken

Andrei Alexandrescu andrei at erdani.com
Fri Jun 4 16:02:31 PDT 2010


The reason is very simple - byChunk reuses the buffer for each chunk. 
That's why you are getting garbled data, and that's why you can't use 
immutable with byChunk.

There are two issues now:

1. send() should NEVER accept const(ubyte)[]. So you have a bug. The 
program as written shouldn't compile.

2. We need a sort of byImmutableChunk or something that creates a new 
buffer every pass, or simply recommend that people use .idup when they 
want to send stuff over.


Andrei

On 06/04/2010 05:48 PM, Sean Kelly wrote:
> Given the sample:
>
>      import std.algorithm, std.concurrency, std.stdio;
>
>
>      void main()
>      {
>          enum bufferSize = 1;
>          auto tid = spawn(&fileWriter );
>          // Read loop
>          /+ BUG: stdio can't handle the immutable buffer
>          foreach( immutable(ubyte)[] buffer; stdin.byChunk( bufferSize ) )
>              send( tid, buffer );
>          +/
>          foreach( const(ubyte)[] buffer; stdin.byChunk( bufferSize ) )
>              send( tid, buffer );
>      }
>
>
>      void fileWriter()
>      {
>          // Write loop
>          for( ; ; )
>          {
>              // BUG: stdio can't handle the immutable buffer
>              //auto buffer = receiveOnly!(immutable(ubyte)[])();
>              auto buffer = receiveOnly!(const(ubyte)[])();
>              writeln( "rx: ", buffer.field[0] );
>          }
>      }
>
> The output I see is:
>
> 	abacus:tdpl sean$ ch13_7
> 	aaaa
> 	rx: 10
> 	rx: 10
> 	rx: 10
> 	rx: 10
> 	rx: 10
> 	^C
> 	abacus:tdpl sean$
>
> Why if I send 4 'a' characters do I receive 5 \n characters?  Seems like the last character in the buffer is being copied over the preceding data.  I just thought I'd mention this in case someone has the time and inclination to look into it.
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos


More information about the phobos mailing list