From 3182160a9a018658834d22758b2e185caa854fc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Thu, 19 Nov 2020 16:06:49 +0100 Subject: [PATCH] Avoid bogus "A task cannot interrupt itself" assertion failure. Calling Task.interrupt() on a task that is already finished, but ran on the same fiber would trigger an assertion failure instead of returning silently. --- source/vibe/core/task.d | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/source/vibe/core/task.d b/source/vibe/core/task.d index 9dfbfbc..3d53201 100644 --- a/source/vibe/core/task.d +++ b/source/vibe/core/task.d @@ -97,7 +97,14 @@ struct Task { void join() @trusted { if (m_fiber) m_fiber.join!true(m_taskCounter); } void joinUninterruptible() @trusted nothrow { if (m_fiber) m_fiber.join!false(m_taskCounter); } - void interrupt() @trusted nothrow { if (m_fiber) m_fiber.interrupt(m_taskCounter); } + void interrupt() @trusted nothrow { if (m_fiber && this.running) m_fiber.interrupt(m_taskCounter); } + + unittest { // regression test for bogus "task cannot interrupt itself" + import vibe.core.core : runTask; + auto t = runTask({}); + t.join(); + runTask({ t.interrupt(); }).join(); + } string toString() const @safe { import std.string; return format("%s:%s", () @trusted { return cast(void*)m_fiber; } (), m_taskCounter); } @@ -138,7 +145,6 @@ struct Task { } } - /** Settings to control the behavior of newly started tasks. */ struct TaskSettings {