Skip to content

fix(server): play_audio leaks file handle — open() without context manager #63

@alfonsodg

Description

@alfonsodg

Problem

play_audio() opens a file without a context manager, causing a file handle leak:

play(open(file_path, "rb").read())

The file descriptor is never explicitly closed.

Exact location

  • File: minimax_mcp/server.py
  • Function: play_audio(), the else branch

Impact

  • File descriptor leak on every call
  • On systems with low fd limits, repeated calls can exhaust file descriptors
  • ResourceWarning in Python 3.12+

Proposed solution

def play_audio(input_file_path: str, is_url: bool = False) -> TextContent:
    if is_url:
        response = requests.get(input_file_path, timeout=30)
        response.raise_for_status()
        play(response.content)
    else:
        file_path = process_input_file(input_file_path)
        with open(file_path, "rb") as f:
            play(f.read())
    return TextContent(type="text", text=f"Successfully played audio file: {input_file_path}")

Also adds timeout to the URL download and raise_for_status() for error handling.

Acceptance criteria

  • File opened with context manager (with statement)
  • No ResourceWarning when running with -Wd flag
  • URL download has timeout and error handling

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