Promise destructed while it’s being processed

36 views
Skip to first unread message

Jens Alfke

unread,
Sep 4, 2022, 1:29:13 PM9/4/22
to Cap'n Proto
I’m having some trouble with a Promise stored in a member variable, where the ’then’ block attached to the promise calls code that ends up clearing that variable. This then triggers a kj exception that a Promise is being destructed while it’s being processed. I understand why this is an error, but I can’t figure out how else to clean up that variable.

The situation is like:
std::optional<Promise<void>> _promise; // This is a data member of my class

// In a method of my class:
_promise = something.then([=] {
…do the work…
_promise.clear(); // <— boom
});
How can I make sure _promise is cleared after its handler completes?

I need a specific reference to the promise — I can’t just shove it into a TaskSet — because I have code in other methods that clears _promise when it decides that it’s no longer needed. (Specifically, this is an idle timer, so when an action occurs it needs to clear the timer.)

I can think of a few ways to fix this, but they feel like hacks. I get the feeling this is a situation that comes up in various circumstances — a promise whose resolution has to clear the Promise variable. Is there a more established best practice for handling it?

—Jens

Kenton Varda

unread,
Sep 4, 2022, 4:09:26 PM9/4/22
to Jens Alfke, Cap'n Proto
Hi Jens,

A couple options:

- You say you can't put the promise in a TaskSet because you need to be able to specifically cancel it. Could you instead use a kj::Canceler to control cancellation? The canceler only wraps the promise, then you could still put it in a TaskSet.

- As a really awful hack, if you know that you are deleting the promise that is currently running, you could use `promise.detach()` instead. `detach()` is usually very bad form because it makes the promise uncancelable, which usually leads to some violations of lifetime guarantees. However, if you know you're at the end of the promise chain anyway -- i.e. as soon as you return from the current function, then the promise is resolved -- then it may be easy to prove that there's no lifetime issues.

-Kenton

--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/8CC19E73-C543-4689-ABA9-5D612D98C026%40mooseyard.com.

Jens Alfke

unread,
Sep 4, 2022, 4:58:26 PM9/4/22
to Kenton Varda, Cap'n Proto


On Sep 4, 2022, at 1:08 PM, 'Kenton Varda' via Cap'n Proto <capn...@googlegroups.com> wrote:

- You say you can't put the promise in a TaskSet because you need to be able to specifically cancel it. Could you instead use a kj::Canceler to control cancellation? The canceler only wraps the promise, then you could still put it in a TaskSet.

This works, thanks; and I already have a TaskSet to put it into. The only nit is that the TaskSet’s ErrorHandler gets called with an exception. I suppose I can use a separate TaskSet just for the idle timer, with an ErrorHandler whose taskFailed method does nothing.

This does seem like a lot of complexity just to implement a cancelable one-shot timer...

—Jens
Reply all
Reply to author
Forward
0 new messages