Skip to content

Commit 0cfca63

Browse files
committed
Fix promise not completed
1 parent 6081cd9 commit 0cfca63

File tree

2 files changed

+19
-36
lines changed

2 files changed

+19
-36
lines changed

src/utils.cc

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,17 @@
2121

2222
void waitForAsyncResult(std::function<void(ResultCallback)> func) {
2323
auto promise = std::make_shared<std::promise<Result>>();
24-
std::weak_ptr<std::promise<Result>> weakPromise{promise};
24+
func([promise](Result result) { promise->set_value(result); });
25+
internal::waitForResult(*promise);
26+
}
2527

26-
func([weakPromise](Result result) {
27-
auto promise = weakPromise.lock();
28-
if (promise) {
29-
promise->set_value(result);
30-
}
31-
});
28+
namespace internal {
3229

33-
auto future = promise->get_future();
30+
void waitForResult(std::promise<pulsar::Result>& promise) {
31+
auto future = promise.get_future();
3432
while (true) {
3533
{
3634
py::gil_scoped_release release;
37-
3835
auto status = future.wait_for(std::chrono::milliseconds(100));
3936
if (status == std::future_status::ready) {
4037
CHECK_RESULT(future.get());
@@ -47,3 +44,5 @@ void waitForAsyncResult(std::function<void(ResultCallback)> func) {
4744
}
4845
}
4946
}
47+
48+
} // namespace internal

src/utils.h

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,42 +36,26 @@ inline void CHECK_RESULT(Result res) {
3636
}
3737
}
3838

39+
namespace internal {
40+
41+
void waitForResult(std::promise<pulsar::Result>& promise);
42+
43+
} // namespace internal
44+
3945
void waitForAsyncResult(std::function<void(ResultCallback)> func);
4046

4147
template <typename T>
4248
inline T waitForAsyncValue(std::function<void(std::function<void(Result, const T&)>)> func) {
4349
auto resultPromise = std::make_shared<std::promise<Result>>();
4450
auto valuePromise = std::make_shared<std::promise<T>>();
4551

46-
std::weak_ptr<std::promise<Result>> weakResultPromise{resultPromise};
47-
std::weak_ptr<std::promise<T>> weakValuePromise{valuePromise};
48-
49-
func([weakResultPromise, weakValuePromise](Result result, const T& value) {
50-
auto valuePromise = weakValuePromise.lock();
51-
if (valuePromise) {
52-
valuePromise->set_value(value);
53-
}
54-
auto resultPromise = weakResultPromise.lock();
55-
if (resultPromise) {
56-
resultPromise->set_value(result);
57-
}
52+
func([resultPromise, valuePromise](Result result, const T& value) {
53+
valuePromise->set_value(value);
54+
resultPromise->set_value(result);
5855
});
5956

60-
auto resultFuture = resultPromise->get_future();
61-
while (true) {
62-
{
63-
py::gil_scoped_release release;
64-
auto status = resultFuture.wait_for(std::chrono::milliseconds(100));
65-
if (status == std::future_status::ready) {
66-
CHECK_RESULT(resultFuture.get());
67-
return valuePromise->get_future().get();
68-
}
69-
}
70-
py::gil_scoped_acquire acquire;
71-
if (PyErr_CheckSignals() != 0) {
72-
raiseException(ResultInterrupted);
73-
}
74-
}
57+
internal::waitForResult(*resultPromise);
58+
return valuePromise->get_future().get();
7559
}
7660

7761
struct CryptoKeyReaderWrapper {

0 commit comments

Comments
 (0)