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

时间:2016-11-19 00:15:04

标签: d phobos vibed

当向已停止的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

callstack看起来像这样。

#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
...
  • 如何防止段错?是否有检查发送功能?怎么可以在vibe.d或phobos2中打补丁?
  • 是vibe.d或phobos2的错误吗?

1 个答案:

答案 0 :(得分:0)

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