Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-54119

Destroy SharedStateBase as we unfold continuations

    • 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}}
      

            Assignee:
            matt.diener@mongodb.com Matt Diener (Inactive)
            Reporter:
            amirsaman.memaripour@mongodb.com Amirsaman Memaripour
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: