D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7446 - [TDPL] Trivial asynchronous file copy example crashes with OwnerTerminated
Summary: [TDPL] Trivial asynchronous file copy example crashes with OwnerTerminated
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: bootcamp, pull, TDPL
Depends on:
Blocks:
 
Reported: 2012-02-05 14:56 UTC by Vladimir Panteleev
Modified: 2020-01-02 08:20 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Vladimir Panteleev 2012-02-05 14:56:13 UTC
import std.concurrency, std.stdio;

void main() {
   enum bufferSize = 1024 * 100;
   auto tid = spawn(&fileWriter);
   // Read loop
   foreach (ubyte[] buffer; stdin.byChunk(bufferSize)) {
      send(tid, buffer.idup);
   }
}

void fileWriter() {
   // Write loop
   for (;;) {
      auto buffer = receiveOnly!(immutable(ubyte)[])();
      stdout.rawWrite(buffer);
   }
}

TDPL says: "The writer thread exits with the OwnerTerminated exception, which is recognized by the runtime system, which simply ignores it."

This program will cat stdin to stdout as expected, however on exit it will apparently crash with an OwnerTerminated exception.

I should point out another oddity: the exception information (class, message, stack trace) are printed to STDOUT, not STDERR. When using this example program to copy a file using pipes, the exception is appended to the destination file.
Comment 1 Martin Nowak 2013-11-14 01:36:13 UTC
This is a phobos bug, druntime can't know anything about phobos exception types.
If this is intened behavior the OwnerTerminated exception should be catched in spawn's exec function (https://github.com/D-Programming-Language/phobos/blob/a0c7d834434222018c0f01ee72bc51842513fa23/std/concurrency.d#L487).
Comment 2 berni44 2019-12-27 09:18:16 UTC
I can't find the original documentation about runtime anymore, but IMHO the receive-functions should mention the exception OwnerTerminated in its documentation. I'll file a PR.
Comment 3 Dlang Bot 2019-12-27 10:37:59 UTC
@berni44 created dlang/phobos pull request #7333 "Fix Issue 7446 - [TDPL] Trivial asynchronous file copy example crashes" fixing this issue:

- Fix Issue 7446 - [TDPL] Trivial asynchronous file copy example crashes
  with OwnerTerminated

https://github.com/dlang/phobos/pull/7333
Comment 4 Atila Neves 2019-12-28 09:43:49 UTC
A trivial workaround of course is to do this:


void fileWriter() {
   // Write loop
   for (;;) {
       try {
           auto buffer = receiveOnly!(immutable(ubyte)[])();
           stdout.rawWrite(buffer);
       } catch(OwnerTerminated _) {
           return;
       }
   }
}
Comment 5 Dlang Bot 2020-01-02 08:20:13 UTC
dlang/phobos pull request #7333 "Fix Issue 7446 - [TDPL] Trivial asynchronous file copy example crashes" was merged into master:

- 9340d7a6b170dc92a47ef8c9905b50a31430509d by Bernhard Seckinger:
  Fix Issue 7446 - [TDPL] Trivial asynchronous file copy example crashes
  with OwnerTerminated

https://github.com/dlang/phobos/pull/7333