Bug 7446 – [TDPL] Trivial asynchronous file copy example crashes with OwnerTerminated
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-02-05T14:56:13Z
Last change time
2020-01-02T08:20:13Z
Keywords
bootcamp, pull, TDPL
Assigned to
No Owner
Creator
Vladimir Panteleev
Comments
Comment #0 by dlang-bugzilla — 2012-02-05T14:56:13Z
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.
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 by dlang-bot — 2019-12-27T10:37:59Z
@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 by atila.neves — 2019-12-28T09:43:49Z
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 by dlang-bot — 2020-01-02T08:20:13Z
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