Uploaded image for project: 'MariaDB Connector/Python'
  1. MariaDB Connector/Python
  2. CONPY-275

mariadb Connector/Python 1.1.8 has memory leak when using python 'with' statementfor connection

Details

    • Bug
    • Status: Closed (View Workflow)
    • Critical
    • Resolution: Not a Bug
    • 1.1.8
    • N/A
    • Generic
    • Rocky8 x86_64
    • 3

    Description

      When called as follows, the mariadb-1.1.8 connector has a memory lead due to leaking connection and cursor objects:

      with mariadb.Connection( host='localhost',  user='root', password=db_pass, unix_socket=db_sock, connect_timeout=5 ) as db_conn:
          my_cur = db_conn.cursor()
          ...
      except mariadb.Error as ex:
          print(str(ex))
      

      Apparently the mariadb.Connection class does not handle (or properly) handle the special _enter_ and _exit_ methods to handle entry and exit from with statements.

      Attachments:
      memleak-with.py can be run (modify the db_pass to root password) to show the memory growth
      memleak-withfix.py contains a wrapper class which implements the _enter_ and _exit_ methods to close the connection and cursor, which does NOT have a memory leak.

      Attachments

        Activity

          georg Georg Richter added a comment -

          I don't think that closing the connection should also close the cursor. If a resource is not needed anymore, it should be closed immediately: Either by cursor.close() or using cursor context.

          See also:

          georg Georg Richter added a comment - I don't think that closing the connection should also close the cursor. If a resource is not needed anymore, it should be closed immediately: Either by cursor.close() or using cursor context. See also: CONPY-276 Why doesn't the MySQLdb Connection context manager close the cursor?
          georg Georg Richter added a comment -

          The reason for the memory leak is not in MariaDB Connector/Python, but rather because the cursor was not closed properly.

          If the context is exited, only the connection itself is released, but not objects returned by the connection. This is not just the case in MariaDB Connector/Python, but in all modules.

          See also PEP 343 - The "with" Statement

          georg Georg Richter added a comment - The reason for the memory leak is not in MariaDB Connector/Python, but rather because the cursor was not closed properly. If the context is exited, only the connection itself is released, but not objects returned by the connection. This is not just the case in MariaDB Connector/Python, but in all modules. See also PEP 343 - The "with" Statement

          People

            georg Georg Richter
            martin.reinhardt@mariadb.com Martin Reinhardt
            Votes:
            0 Vote for this issue
            Watchers:
            4 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.