dphobosvibed

vibe.d: Try to send a Message to a stopped Task


When sending a Message to a stopped vibe.d Task, the application gets an segmentation fault. I did not expect the message to be delivered, but to get notified about the failed sending attempt (or at least not to crash).

The following example illustrates this Problem.

import std.stdio;
import core.thread;
import vibe.core.core;
import vibe.core.concurrency;

static this() {
    Task t = runTask({
        writeln("Hi");
    });
    t.join;
    t.send(42);
    writeln("Bye");
}

When running the code above, the output is:

Hi
Program exited with code -11

... instead of:

Hi
Bye

The callstack looks like this.

#0  0x00007ffff6dbd346 in std.concurrency.MessageBox.put(ref std.concurrency.Message) ()
   from /usr/lib64/libphobos2.so.0.71
#1  0x000000000051b0b3 in std.concurrency._send!(int)._send(std.concurrency.MsgType, std.concurrency.Tid, int) (_param_2=42, tid=..., type=<incomplete type>)
    at /opt/dmd-2.071/import/std/concurrency.d:640
#2  0x000000000051b06d in std.concurrency._send!(int)._send(std.concurrency.Tid, int) (
    _param_1=42, tid=...) at /opt/dmd-2.071/import/std/concurrency.d:629
#3  0x000000000051b04b in std.concurrency.send!(int).send(std.concurrency.Tid, int) (
    _param_1=42, tid=...) at /opt/dmd-2.071/import/std/concurrency.d:605
#4  0x000000000051b027 in vibe.core.concurrency.send!(int).send(vibe.core.task.Task, int) (
    _param_1=42, task=...)
    at /home/user/.dub/packages/vibe-d-0.7.30/vibe-d/source/vibe/core/concurrency.d:1239
#5  0x0000000000517b6b in app._staticCtor1() () at /tmp/test/source/app.d:11
...

Solution

  • Task has a running property that you could use. I believe it's intended behavior, as error-handling generally should be on application level, not library level.

    import std.stdio;
    import core.thread;
    import vibe.core.core;
    import vibe.core.concurrency;
    
    static this() {
        Task t = runTask({
            writeln("Hi");
        });
        t.join;
    
        if (t.running) {
            t.send(42);
        }
    
        writeln("Bye");
    }