Re: [capnproto] Promise destructed while it’s being processed

2022-09-04 Thread Jens Alfke


> On Sep 4, 2022, at 1:08 PM, 'Kenton Varda' via Cap'n Proto 
>  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

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/capnproto/83AA81A6-153A-4660-ADAD-83D47706890A%40mooseyard.com.


Re: [capnproto] Promise destructed while it’s being processed

2022-09-04 Thread 'Kenton Varda' via 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

On Sun, Sep 4, 2022 at 12:29 PM Jens Alfke  wrote:

> 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;  // 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
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/capnproto/8CC19E73-C543-4689-ABA9-5D612D98C026%40mooseyard.com
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/capnproto/CAJouXQ%3DVa5hAQem2soijngw%3D4dnuYP1LYzgzX2m2oEvhUEjVoA%40mail.gmail.com.


[capnproto] Promise destructed while it’s being processed

2022-09-04 Thread Jens Alfke
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;  // 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

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/capnproto/8CC19E73-C543-4689-ABA9-5D612D98C026%40mooseyard.com.