I'm working on a python script to collect and insert data on a remote postgres server and I randomly started getting this error even for the script that use to work before.
Python(42869,0x1ff51bac0) malloc: double free for ptr 0x12d939800 Python(42869,0x1ff51bac0) malloc: *** set a breakpoint in malloc_error_break to debug
here is my database file
import psycopg2 as pg
import json
# function to make a query by specifying the database name
import config
def get_db_conn():
"""
Connects to the specified database and returns the connection object.
Parameters:
dbname (str): The name of the database to connect to.
Returns:
psycopg2.extensions.connection: The connection object.
Raises:
psycopg2.Error: If there is an error connecting to the database.
"""
conn = pg.connect(
host=config.DB_HOST ,
port=config.DB_PORT,
user=config.DB_USER,
password=config.DB_PASS,
database=config.DB_NAME)
return conn
def db_operations(query, isread=True, return_data=False):
"""
Perform database operations based on the provided query.
Args:
database (str): The name of the database to connect to.
query (str): The SQL query to execute.
isread (bool, optional): Specifies whether the query is a read operation or not. Defaults to True.
return_data (bool, optional): Specifies whether to return the data fetched from the query. Defaults to False.
Returns:
tuple: A tuple containing a boolean value indicating the success of the operation and the result of the operation.
If the operation is successful, the first element of the tuple is True. Otherwise, it is False.
If the operation is a read operation and return_data is True, the second element of the tuple is a JSON string
containing the fetched data. If return_data is False, the second element of the tuple is 'OK'.
If the operation is not a read operation, the second element of the tuple is 'OK' if the operation is successful,
otherwise it contains the error message.
"""
conn = get_db_conn()
cursor = conn.cursor()
try:
cursor.execute(query)
if isread:
data = [dict(zip([column[0] for column in cursor.description], row)) for row in cursor.fetchall()]
return True, json.dumps(data)
else:
conn.commit()
if return_data:
data = [dict(zip([column[0] for column in cursor.description], row)) for row in cursor.fetchall()]
return True, json.dumps(data)
return True, 'OK'
except (Exception) as error:
conn.rollback()
conn.commit()
print("Error while connecting to PostgreSQL:", error)
return False, error
finally:
cursor.close()
conn.close()
I'm using macbook air m2, python version 3.9.6 (even tried with 3.11.6 and 3.12.3), psycopg2 version 2.9.9 and postgres version 14.11
I was trying to insert data in my remote postgresql server but got malloc Double Free Error
Likely this is due to this bug in psql, which is fixed in the latest versions.
For example from the release notes for 13.14:
Fix incompatibility with OpenSSL 3.2 (Tristan Partin, Bo Andreson)
Use the BIO “app_data” field for our private storage, instead of assuming it's okay to use the “data” field. This mistake didn't cause problems before, but with 3.2 it leads to crashes and complaints about double frees.
There is an issue for it on homebrew which has some suggested workarounds, the main one being upgrade to the latest maintenance release, or hopefully by now they have received the patch.