Uploaded image for project: 'Mesos'
  1. Mesos
  2. MESOS-5021

Memory leak in subprocess when 'environment' argument is provided.

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Blocker
    • Resolution: Fixed
    • Affects Version/s: 0.23.0, 0.23.1, 0.24.0, 0.24.1, 0.25.0, 0.26.0, 0.27.0, 0.27.1, 0.27.2, 0.28.0
    • Fix Version/s: 0.24.2, 0.25.1, 0.26.1, 0.27.3, 0.28.1
    • Component/s: agent, libprocess
    • Labels:
      None

      Description

      A memory leak in process::subprocess was introduced here:
      https://github.com/apache/mesos/commit/14b49f31840ff1523b31007c21b12c604700323f

      This was found when Jie Yu and I examined a memory leak in the health check program (see MESOS-4869).

      The leak is here:
      https://github.com/apache/mesos/blob/0.28.0/3rdparty/libprocess/src/subprocess.cpp#L451-L456

        // Like above, we need to construct the environment that we'll pass
        // to 'os::execvpe' as it might not be async-safe to perform the
        // memory allocations.
        char** envp = os::raw::environment();
      
        if (environment.isSome()) {
          // NOTE: We add 1 to the size for a NULL terminator.
          envp = new char*[environment.get().size() + 1];
      
          size_t index = 0;
          foreachpair (const string& key, const string& value, environment.get()) {
            string entry = key + "=" + value;
            envp[index] = new char[entry.size() + 1];
            strncpy(envp[index], entry.c_str(), entry.size() + 1);
            ++index;
          }
      
          envp[index] = NULL;
        }
      
        ...
      
        // Need to delete 'envp' if we had environment variables passed to
        // us and we needed to allocate the space.
        if (environment.isSome()) {
          CHECK_NE(os::raw::environment(), envp);
          delete[] envp; // XXX Does not delete the sub arrays.
        }
      

      Auditing the code, it appears to affect a number of locations:

        Issue Links

          Activity

          Hide
          bmahler Benjamin Mahler added a comment -
          commit 4f073a066cd68207dc440eba82ce4d76869ae0ea
          Author: Benjamin Mahler <bmahler@apache.org>
          Date:   Wed Mar 23 20:30:32 2016 -0700
          
              Fixed a memory leak in process::subprocess.
          
              Review: https://reviews.apache.org/r/45267
          
          Show
          bmahler Benjamin Mahler added a comment - commit 4f073a066cd68207dc440eba82ce4d76869ae0ea Author: Benjamin Mahler <bmahler@apache.org> Date: Wed Mar 23 20:30:32 2016 -0700 Fixed a memory leak in process::subprocess. Review: https://reviews.apache.org/r/45267
          Show
          bmahler Benjamin Mahler added a comment - https://reviews.apache.org/r/45267/

            People

            • Assignee:
              bmahler Benjamin Mahler
              Reporter:
              bmahler Benjamin Mahler
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development