RejectedSoftware Forums

Sign up

Call of getResult on Future returned by async hangs worker task

This thread http://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/41588/ got a little messy, my fault, and I cleaned up the example so it's completely clear this hang is not due to refusal to hand on the baton under the mistaken assumption that async uses an independent thread, not a Fiber. Here is that example. I've reported this as a bug, with a link here.

The following test code is a modification of the first example here https://vibed.org/api/vibe.core.concurrency/async, with a worker task that repeatedly sleeps for 100 msec and reports how long since it was started, until it has run for 5000 msec. The main task runs for 2000 msec and then calls getResult on the worker task to wait the remaining 3000 msec for the worker task to finish. However, in practice the reporting every 100 msec in the worker task stops at that point, and getResult blocks indefinitely.

import vibe.vibe;
import vibe.core.core;
import vibe.core.log;


void test1()
{
    auto val = async({
        logInfo("Starting to compute value in worker task.");
        worker_task(5000); // simulate some lengthy computation
        logInfo("Finished computing value in worker task.");
        return 32;
    });

    logInfo("Starting computation in main task");
    sleep(2000.msecs); // simulate some lengthy computation
    logInfo("Finished computation in main task. Waiting for async value.");
    logInfo("Result: %s", val.getResult());
}


void worker_task(long msecs) {
    import std.datetime;
    StopWatch sw;
    sw.start();
    logInfo("worker task: start waiting");
    while( sw.peek().msecs <= msecs) {
        logInfo("worker task: %s msecs", sw.peek().msecs);
        sleep(100.msecs);
    }
    logInfo("worker task: done waiting");
}

The output with the hang obviated by a control-C is here. Notice reporting in the worker task stopping after about 2000 msec. Presumably the intent is for getResult to block until the worker task completes execution, but it seems to seize the baton of execution and not hand it back to the worker task.

[vibe-0(dCxD) INF] Starting to compute value in worker task.
[vibe-0(dCxD) INF] worker task: start waiting
[vibe-0(dCxD) INF] worker task: 0 msecs
[main(5T5o) INF] Starting computation in main task
[vibe-0(dCxD) INF] worker task: 100 msecs
[vibe-0(dCxD) INF] worker task: 201 msecs
[vibe-0(dCxD) INF] worker task: 301 msecs
[vibe-0(dCxD) INF] worker task: 401 msecs
[vibe-0(dCxD) INF] worker task: 502 msecs
[vibe-0(dCxD) INF] worker task: 606 msecs
[vibe-0(dCxD) INF] worker task: 706 msecs
[vibe-0(dCxD) INF] worker task: 807 msecs
[vibe-0(dCxD) INF] worker task: 907 msecs
[vibe-0(dCxD) INF] worker task: 1007 msecs
[vibe-0(dCxD) INF] worker task: 1107 msecs
[vibe-0(dCxD) INF] worker task: 1208 msecs
[vibe-0(dCxD) INF] worker task: 1309 msecs
[vibe-0(dCxD) INF] worker task: 1410 msecs
[vibe-0(dCxD) INF] worker task: 1510 msecs
[vibe-0(dCxD) INF] worker task: 1611 msecs
[vibe-0(dCxD) INF] worker task: 1711 msecs
[vibe-0(dCxD) INF] worker task: 1813 msecs
[vibe-0(dCxD) INF] worker task: 1913 msecs
[main(5T5o) INF] Finished computation in main task. Waiting for async value.
^C[main(5T5o) INF] [main(----) INF] Received signal 2. Shutting down.

Re: Call of getResult on Future returned by async hangs worker task

Reference: #1717

Re: Call of getResult on Future returned by async hangs worker task

On Thu, 23 Mar 2017 19:39:24 GMT, Sönke Ludwig wrote:

Reference: #1717

:)