-
Type: Task
-
Resolution: Duplicate
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Internal Code
-
Service Arch 2022-04-18, Service Arch 2022-05-02, Service Arch 2022-05-16
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}}
- duplicates
-
SERVER-66126 Clear Future/ExecutorFuture continuations as they run
- Open
- is related to
-
SERVER-53500 Investigate making nested futures equivalent to chained continuations
- Backlog
-
SERVER-52942 Evaluate whether we could change the destruction order of the closure in the ExecutorFuture class
- Closed
-
SERVER-53538 Clarify semantics of when destructor runs for ExecutorFuture chains
- Closed