Uploaded image for project: 'MariaDB Connector/C'
  1. MariaDB Connector/C
  2. CONC-131

Memory leak in mysql_close() with MYSQL_OPT_NONBLOCK

Details

    • Bug
    • Status: Closed (View Workflow)
    • Major
    • Resolution: Fixed
    • 2.1
    • 3.0.0, 2.2.0
    • None
    • None

    Description

      When a mysql connection is created and is set to not blocking, mysql_close() doesn't free the memory associate with async causing a severe memory leak.

      Depending from the the speed of your CPU and your available memory, the follow code can eat all available memory in just few seconds:

      #include <mysql.h>
      #include <stdio.h>
      int main() {
        while(1) {
        MYSQL *mysql=mysql_init(NULL);
        mysql_options(mysql, MYSQL_OPT_NONBLOCK, 0);  // this line triggers the bug
        mysql_close(mysql);
        }
        return 0;
      }

      Attachments

        Activity

          rcannao René Cannaò created issue -
          rcannao René Cannaò added a comment -

          Quick patch in mysql_close_options() :

          2194a2195,2202
          >     {
          >       struct mysql_async_context *ctxt;
          >       if ((ctxt = mysql->options.extension->async_context) != 0)
          >       {
          >         my_context_destroy(&ctxt->async_context);
          >         my_free((gptr)ctxt, MYF(0));
          >       }
          >     }

          rcannao René Cannaò added a comment - Quick patch in mysql_close_options() : 2194a2195,2202 > { > struct mysql_async_context *ctxt; > if ((ctxt = mysql->options.extension->async_context) != 0) > { > my_context_destroy(&ctxt->async_context); > my_free((gptr)ctxt, MYF(0)); > } > }
          georg Georg Richter made changes -
          Field Original Value New Value
          Fix Version/s 3.0.0 [ 18700 ]
          Fix Version/s 2.2.0 [ 19500 ]
          Resolution Fixed [ 1 ]
          Status Open [ 1 ] Closed [ 6 ]
          rcannao René Cannaò added a comment - - edited

          Because non blocking API is implemented using coroutine, it is possible that the stack is freed while being used by another function. This will lead to invalid read/write on memory already freed.
          I think this is a better patch:

          @@ -2192,6 +2192,16 @@
               my_free(mysql->options.extension->ssl_crlpath, MYF(MY_ALLOW_ZERO_PTR));
               if(hash_inited(&mysql->options.extension->connect_attrs))
                 hash_free(&mysql->options.extension->connect_attrs);
          +    {
          +     struct mysql_async_context *ctxt;
          +     if ((ctxt = mysql->options.extension->async_context) != 0) {
          +       if (ctxt->active==0) {
          +         my_context_destroy(&ctxt->async_context);
          +         my_free((gptr)ctxt, MYF(0));
          +         mysql->options.extension->async_context=NULL;
          +       }
          +     }
          +   }
             }
             my_free((gptr)mysql->options.extension, MYF(MY_ALLOW_ZERO_PTR));
             /* clear all pointer */

          rcannao René Cannaò added a comment - - edited Because non blocking API is implemented using coroutine, it is possible that the stack is freed while being used by another function. This will lead to invalid read/write on memory already freed. I think this is a better patch: @@ -2192,6 +2192,16 @@ my_free(mysql->options.extension->ssl_crlpath, MYF(MY_ALLOW_ZERO_PTR)); if(hash_inited(&mysql->options.extension->connect_attrs)) hash_free(&mysql->options.extension->connect_attrs); + { + struct mysql_async_context *ctxt; + if ((ctxt = mysql->options.extension->async_context) != 0) { + if (ctxt->active==0) { + my_context_destroy(&ctxt->async_context); + my_free((gptr)ctxt, MYF(0)); + mysql->options.extension->async_context=NULL; + } + } + } } my_free((gptr)mysql->options.extension, MYF(MY_ALLOW_ZERO_PTR)); /* clear all pointer */
          julien.fritsch Julien Fritsch made changes -
          Workflow MariaDB connectors [ 69912 ] MariaDB v4 [ 160991 ]

          People

            georg Georg Richter
            rcannao René Cannaò
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.