Skip to content

Memory leak in batch processing loop #9

Description

@ToddMenier

content: The batch_processing function in data_processor.py has a memory leak due to unclosed file handles and unreleased database connections in the loop.
file: app/processors/data_processor.py
code:

def batch_processing(data_batch):
    for data in data_batch:
        file = open(f"temp/{data.id}.txt", "w")
        file.write(str(data))
        db_conn = get_db_connection()
        db_conn.execute("INSERT INTO processed_data VALUES (?)", (data.id,))

description: Each iteration opens a file and gets a DB connection but never closes them. Over time, this exhausts file descriptors and DB connections, leading to system crashes. Close files with 'with' statements and release DB connections after use.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions