[SERVER-54119] Destroy SharedStateBase as we unfold continuations Created: 28/Jan/21  Updated: 02/May/22  Resolved: 02/May/22

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Amirsaman Memaripour Assignee: Matt Diener (Inactive)
Resolution: Duplicate Votes: 0
Labels: servicearch-wfbf-day
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-66126 Clear Future/ExecutorFuture continuat... Open
Related
is related to SERVER-53500 Investigate making nested futures equ... Backlog
is related to SERVER-52942 Evaluate whether we could change the ... Closed
is related to SERVER-53538 Clarify semantics of when destructor ... Closed
Sprint: Service Arch 2022-04-18, Service Arch 2022-05-02, Service Arch 2022-05-16
Participants:

 Description   

The current implementation for our future library postpones destroying the lambdas captured by instances of SharedStateBase (see here) until the execution of the entire continuation is complete, and it is safe to destroy the entire continuation (see below for example).

It would be easier to reason about each continuation if we destroy the SharedStateBase, and the captured lambda, as we go through each continuation. This would translate to destroying futures as soon as they are executed or skipped.

class Token {
  public:
    explicit Token(int id) : _id(id) { LOGV2(7777701, "Constructing", "id"_attr = _id); }
    ~Token() { LOGV2(7777702, "Destroying", "id"_attr = _id); }
    void run() const { LOGV2(7777703, "Running", "id"_attr = _id); }
  private:
    int _id;
};
 
auto pf = makePromiseFuture<void>();
auto future = std::move(pf.future)
    .then([handle = std::make_unique<Token>(1)] { handle->run(); })
    .then([handle = std::make_unique<Token>(2)] { handle->run(); })
    .then([handle = std::make_unique<Token>(3)] { handle->run(); })
    .onError([handle = std::make_unique<Token>(4)](Status) { handle->run(); });
 
LOGV2(7777704, "Setting promise");
pf.promise.emplaceValue();
LOGV2(7777704, "Finished running the continuation");

This code will generate the following:

{"t":{"$date":"2021-01-28T18:53:02.270Z"},"s":"I",  "c":"TEST",     "id":7777701, "ctx":"main","msg":"Constructing","attr":{"id":1}}
{"t":{"$date":"2021-01-28T18:53:02.270Z"},"s":"I",  "c":"TEST",     "id":7777701, "ctx":"main","msg":"Constructing","attr":{"id":2}}
{"t":{"$date":"2021-01-28T18:53:02.271Z"},"s":"I",  "c":"TEST",     "id":7777701, "ctx":"main","msg":"Constructing","attr":{"id":3}}
{"t":{"$date":"2021-01-28T18:53:02.271Z"},"s":"I",  "c":"TEST",     "id":7777701, "ctx":"main","msg":"Constructing","attr":{"id":4}}
{"t":{"$date":"2021-01-28T18:53:02.271Z"},"s":"I",  "c":"TEST",     "id":7777704, "ctx":"main","msg":"Setting promise"}
{"t":{"$date":"2021-01-28T18:53:02.271Z"},"s":"I",  "c":"TEST",     "id":7777703, "ctx":"main","msg":"Running","attr":{"id":1}}
{"t":{"$date":"2021-01-28T18:53:02.271Z"},"s":"I",  "c":"TEST",     "id":7777703, "ctx":"main","msg":"Running","attr":{"id":2}}
{"t":{"$date":"2021-01-28T18:53:02.271Z"},"s":"I",  "c":"TEST",     "id":7777703, "ctx":"main","msg":"Running","attr":{"id":3}}
{"t":{"$date":"2021-01-28T18:53:02.271Z"},"s":"I",  "c":"TEST",     "id":7777704, "ctx":"main","msg":"Finished running the continuation"}
{"t":{"$date":"2021-01-28T18:53:02.271Z"},"s":"I",  "c":"TEST",     "id":7777702, "ctx":"main","msg":"Destroying","attr":{"id":1}}
{"t":{"$date":"2021-01-28T18:53:02.271Z"},"s":"I",  "c":"TEST",     "id":7777702, "ctx":"main","msg":"Destroying","attr":{"id":2}}
{"t":{"$date":"2021-01-28T18:53:02.271Z"},"s":"I",  "c":"TEST",     "id":7777702, "ctx":"main","msg":"Destroying","attr":{"id":3}}
{"t":{"$date":"2021-01-28T18:53:02.271Z"},"s":"I",  "c":"TEST",     "id":7777702, "ctx":"main","msg":"Destroying","attr":{"id":4}}



 Comments   
Comment by Lauren Lewis (Inactive) [ 18/Mar/22 ]

We haven’t heard back from you for some time, so we're going to close this ticket. If this is still an issue for you, please provide additional information and we will reopen the ticket.

Generated at Thu Feb 08 05:32:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.