当我尝试在远程 postgresql 服务器上插入数据时,我在最近的 python 脚本中遇到了 malloc“双重释放错误”错误

问题描述 投票:0回答:1

我正在编写一个 python 脚本来在远程 postgres 服务器上收集和插入数据,并且我随机开始收到此错误,即使对于之前使用的脚本也是如此。

Python(42869,0x1ff51bac0) malloc: double free for ptr 0x12d939800 Python(42869,0x1ff51bac0) malloc: *** set a breakpoint in malloc_error_break to debug

这是我的数据库文件

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()

我正在使用 macbook air m2、python 版本 3.9.6(甚至尝试过 3.11.6 和 3.12.3)、psycopg2 版本 2.9.9 和 postgres 版本 14.11

我试图在远程 postgresql 服务器中插入数据,但出现了 malloc Double Free 错误

python postgresql ssl malloc psycopg2
1个回答
0
投票

这可能是由于 psql 中的这个错误造成的,该错误已在最新版本中修复。

例如来自 13.14 的发行说明

修复与 OpenSSL 3.2 的不兼容性(Tristan Partin、Bo Andreson)

使用 BIO“app_data”字段作为我们的私有存储,而不是假设可以使用“data”字段。这个错误以前不会造成问题,但在 3.2 中它会导致崩溃和关于双重释放的投诉。

自制软件上有一个问题,其中有一些建议的解决方法,或者希望现在他们已经收到了补丁。

© www.soinside.com 2019 - 2024. All rights reserved.