tongyi plugin temp file #809

Closed
opened 2026-02-16 10:20:34 -05:00 by yindo · 1 comment
Owner

Originally created by @WzzP on GitHub (Nov 17, 2025).

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues Dify issues & Dify Official Plugins, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.8.1

Plugin version

0.1.1

Cloud or Self Hosted

Cloud

Steps to reproduce

Steps to reproduce

  1. Use Tongyi (Qwen) model plugin
  2. Send messages with base64-encoded images or videos
  3. Or upload documents to Tongyi
  4. Check the system temporary directory (via tempfile.gettempdir())
  5. Observe that temporary files accumulate and are never deleted

Expected behavior

Temporary files created for processing base64 images, videos, and document uploads should be cleaned up after the API call completes.

Actual behavior

Temporary files are created but never deleted, leading to:

  • Disk space leakage
  • Accumulation of orphaned files in the temp directory
  • Potential issues for long-running services

Root Cause

File: models/tongyi/models/llm/llm.py

Issue 1: _save_base64_to_file method (lines 570-585)

This method creates temporary files when processing base64-encoded images and videos but has no cleanup mechanism:

def _save_base64_to_file(self, base64_data: str) -> str:
    (mime_type, encoded_string) = (
        base64_data.split(",")[0].split(";")[0].split(":")[1],
        base64_data.split(",")[1],
    )
    temp_dir = tempfile.gettempdir()
    file_path = os.path.join(temp_dir, f"{uuid.uuid4()}.{mime_type.split('/')[1]}")
    Path(file_path).write_bytes(base64.b64decode(encoded_string))
    return f"file://{file_path}"  # File created, never cleaned up

This method is called in two places:

  • Line 517: Processing IMAGE content type
  • Line 526: Processing VIDEO content type

Issue 2: _upload_file_to_tongyi method (lines 606-621)

This method uses tempfile.NamedTemporaryFile(delete=False) but never cleans up the file after upload:

with tempfile.NamedTemporaryFile(delete=False) as temp_file:
    # ... write file content ...
    temp_file.flush()
response = client.files.create(file=temp_file, purpose="file-extract")
return response.id  # Temp file never deleted

Proposed Solution

Add temporary file tracking and cleanup mechanism:

  1. Track temporary files in a class instance variable
  2. Add cleanup method to safely delete tracked files
  3. Ensure cleanup in response handlers using try-finally blocks
  4. Clean up immediately after file upload completes

Implementation Overview

class TongyiLargeLanguageModel(LargeLanguageModel):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._temp_files = []  # Track temporary files
    
    def _save_base64_to_file(self, base64_data: str) -> str:
        # ... create file ...
        self._temp_files.append(file_path)  # Track for cleanup
        return f"file://{file_path}"
    
    def _cleanup_temp_files(self):
        """Clean up temporary files"""
        for file_path in self._temp_files:
            try:
                if os.path.exists(file_path):
                    os.remove(file_path)
            except Exception:
                pass  # Don't fail on cleanup errors
        self._temp_files.clear()
    
    def _handle_generate_response(self, ...):
        try:
            # ... process response ...
        finally:
            self._cleanup_temp_files()  # Ensure cleanup

Impact

  • Severity: Medium - doesn't cause immediate failure but accumulates over time
  • Affected users: All users of Tongyi plugin who process images/videos or upload documents
  • Long-term effect: Disk space exhaustion for long-running services

Environment

  • OS: macOS / Linux / Windows
  • Python version: 3.10+
  • Dify version: >= v1.0.0
  • Plugin: Tongyi (models/tongyi)

Additional Context

This is a resource leak issue that becomes more problematic with:

  • High-frequency usage of multimodal features
  • Long-running production services
  • Limited disk space environments

Willingness to Contribute

I am willing to submit a PR to fix this issue if needed.

✔️ Error log

No response

Originally created by @WzzP on GitHub (Nov 17, 2025). ### Self Checks - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [Dify issues](https://github.com/langgenius/dify/issues) & [Dify Official Plugins](https://github.com/langgenius/dify-official-plugins/issues), including closed ones. - [x] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [x] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version 1.8.1 ### Plugin version 0.1.1 ### Cloud or Self Hosted Cloud ### Steps to reproduce ## Steps to reproduce 1. Use Tongyi (Qwen) model plugin 2. Send messages with base64-encoded images or videos 3. Or upload documents to Tongyi 4. Check the system temporary directory (via `tempfile.gettempdir()`) 5. Observe that temporary files accumulate and are never deleted ## Expected behavior Temporary files created for processing base64 images, videos, and document uploads should be cleaned up after the API call completes. ## Actual behavior Temporary files are created but never deleted, leading to: - Disk space leakage - Accumulation of orphaned files in the temp directory - Potential issues for long-running services ## Root Cause **File**: `models/tongyi/models/llm/llm.py` ### Issue 1: `_save_base64_to_file` method (lines 570-585) This method creates temporary files when processing base64-encoded images and videos but has no cleanup mechanism: ```python def _save_base64_to_file(self, base64_data: str) -> str: (mime_type, encoded_string) = ( base64_data.split(",")[0].split(";")[0].split(":")[1], base64_data.split(",")[1], ) temp_dir = tempfile.gettempdir() file_path = os.path.join(temp_dir, f"{uuid.uuid4()}.{mime_type.split('/')[1]}") Path(file_path).write_bytes(base64.b64decode(encoded_string)) return f"file://{file_path}" # File created, never cleaned up ``` This method is called in two places: - Line 517: Processing IMAGE content type - Line 526: Processing VIDEO content type ### Issue 2: `_upload_file_to_tongyi` method (lines 606-621) This method uses `tempfile.NamedTemporaryFile(delete=False)` but never cleans up the file after upload: ```python with tempfile.NamedTemporaryFile(delete=False) as temp_file: # ... write file content ... temp_file.flush() response = client.files.create(file=temp_file, purpose="file-extract") return response.id # Temp file never deleted ``` ## Proposed Solution Add temporary file tracking and cleanup mechanism: 1. **Track temporary files** in a class instance variable 2. **Add cleanup method** to safely delete tracked files 3. **Ensure cleanup** in response handlers using try-finally blocks 4. **Clean up immediately** after file upload completes ### Implementation Overview ```python class TongyiLargeLanguageModel(LargeLanguageModel): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._temp_files = [] # Track temporary files def _save_base64_to_file(self, base64_data: str) -> str: # ... create file ... self._temp_files.append(file_path) # Track for cleanup return f"file://{file_path}" def _cleanup_temp_files(self): """Clean up temporary files""" for file_path in self._temp_files: try: if os.path.exists(file_path): os.remove(file_path) except Exception: pass # Don't fail on cleanup errors self._temp_files.clear() def _handle_generate_response(self, ...): try: # ... process response ... finally: self._cleanup_temp_files() # Ensure cleanup ``` ## Impact - **Severity**: Medium - doesn't cause immediate failure but accumulates over time - **Affected users**: All users of Tongyi plugin who process images/videos or upload documents - **Long-term effect**: Disk space exhaustion for long-running services ## Environment - **OS**: macOS / Linux / Windows - **Python version**: 3.10+ - **Dify version**: >= v1.0.0 - **Plugin**: Tongyi (models/tongyi) ## Additional Context This is a resource leak issue that becomes more problematic with: - High-frequency usage of multimodal features - Long-running production services - Limited disk space environments ## Willingness to Contribute I am willing to submit a PR to fix this issue if needed. ### ✔️ Error log _No response_
yindo added the bug label 2026-02-16 10:20:34 -05:00
yindo closed this issue 2026-02-16 10:20:34 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Dec 3, 2025):

Hi, @WzzP. I'm Dosu, and I'm helping the dify-official-plugins team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You reported that the Tongyi plugin (v0.1.1) in Dify 1.8.1 cloud deployment creates temporary files for base64-encoded media uploads.
  • These temporary files are not deleted, leading to disk space leakage and orphaned files in the system temp directory.
  • The root cause is the _save_base64_to_file method in models/tongyi/models/llm/llm.py lacking a cleanup process.
  • No further comments or updates have been provided since the initial report.

Next Steps:

  • Please let me know if this issue is still relevant with the latest version of the dify-official-plugins repository by commenting here.
  • If I do not hear back within 5 days, I will automatically close this issue.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Dec 3, 2025): Hi, @WzzP. I'm [Dosu](https://dosu.dev), and I'm helping the dify-official-plugins team manage their backlog and am marking this issue as stale. **Issue Summary:** - You reported that the Tongyi plugin (v0.1.1) in Dify 1.8.1 cloud deployment creates temporary files for base64-encoded media uploads. - These temporary files are not deleted, leading to disk space leakage and orphaned files in the system temp directory. - The root cause is the `_save_base64_to_file` method in `models/tongyi/models/llm/llm.py` lacking a cleanup process. - No further comments or updates have been provided since the initial report. **Next Steps:** - Please let me know if this issue is still relevant with the latest version of the dify-official-plugins repository by commenting here. - If I do not hear back within 5 days, I will automatically close this issue. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-official-plugins#809