habanero-rice / hclib

A C/C++ task-based programming model for shared memory and distributed parallel computing.
http://habanero-rice.github.io/hclib/
BSD 3-Clause "New" or "Revised" License
71 stars 35 forks source link

Sporadic Deadlock When Using Async and Yield #90

Open Jason-KChen opened 4 years ago

Jason-KChen commented 4 years ago

I am running into a possible deadlock scenario when running the attached code without HCLIB_LOCALITY_FILE and with two workers.

The promises and waits were executed just fine but it seems like the tasks themselves never finish for some reason. I am able to reproduce this issue about 7 out of 10 runs on my machine. @srirajpaul was also able to reproduce this.

#include <chrono>
#include <cstdint>
#include <iostream>
#include <stdlib.h>
#include <thread>

#include "hclib_cpp.h"

int main(int argc, char** argv) {
    const char* deps[] = { "system" };
    hclib::launch(deps, 1, []() {
        hclib::finish([]() {
            hclib::async([]() {
                hclib::promise_t<int>* x = new hclib::promise_t<int>();
                hclib::promise_t<int>* y = new hclib::promise_t<int>();

                hclib::async([y] {
                    hclib::yield();
                    y->put(1);
                });

                hclib::async([x] {
                    x->get_future()->wait();
                });

                y->get_future()->wait();
                x->put(10);
            });
        });
        std::cout << "all finished " << std::endl;
    });
    return 0;
}
agrippa commented 4 years ago

@JKChenFZ I'm seeing sporadic hangs in some of our other tests as well (cpp/promise/async_future_await_at) that use put and wait. I'm working on finding when the issue was introduced, but at the moment I'm all the way back to this commit and the issue still exists:

commit 01aafbf4a516b20289e890b07b099537420cb23b Merge: ae2801e bfff5c4 Author: Max Grossman jmaxg3@gmail.com Date: Sun May 17 09:28:25 2020 -0700

Merge pull request #83 from habanero-rice/access_promise_from_future

Some new async APIs, small formatting and comments changes

Just wanted to say thanks for calling this out, and let you know I'm looking in to it.

agrippa commented 4 years ago

I suspect this may be the same issue as:

https://github.com/habanero-rice/hclib/issues/73