vibe.d: 尝试向已停止的任务发送消息

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

向停止的 vibe.d 任务发送消息时,应用程序出现分段错误。我没想到消息会送达,但会收到有关发送尝试失败的通知(或至少不会崩溃)。

下面的例子说明了这个问题。

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");
}

当运行上述代码时,输​​出为:

Hi
Program exited with code -11

... 而不是:

Hi
Bye

调用堆栈如下所示。

#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
...

Task 有一个 running 属性 可以使用。我相信这是预期的行为,因为错误处理通常应该在应用程序级别,而不是库级别。

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");
}