Skip to content

Conversation

@WzzP
Copy link
Contributor

@WzzP WzzP commented Nov 17, 2025

  • 添加临时文件追踪机制,在类初始化时创建 _temp_files 列表
  • 在 _save_base64_to_file 方法中记录创建的临时文件
  • 添加 _cleanup_temp_files 方法用于清理临时文件
  • 在 _handle_generate_response 和 _handle_generate_stream_response 中使用 try-finally 确保临时文件被清理
  • 修复 _upload_file_to_tongyi 方法中的临时文件泄漏问题

这个修复解决了处理 base64 编码的图片/视频和文档上传时临时文件累积的问题,避免长期运行服务时磁盘空间被占用。

Related Issues or Context

This PR contains Changes to Non-Plugin

  • Documentation
  • Other

This PR contains Changes to Non-LLM Models Plugin

  • I have Run Comprehensive Tests Relevant to My Changes

This PR contains Changes to LLM Models Plugin

  • My Changes Affect Message Flow Handling (System Messages and User→Assistant Turn-Taking)
  • My Changes Affect Tool Interaction Flow (Multi-Round Usage and Output Handling, for both Agent App and Agent Node)
  • My Changes Affect Multimodal Input Handling (Images, PDFs, Audio, Video, etc.)
  • My Changes Affect Multimodal Output Generation (Images, Audio, Video, etc.)
  • My Changes Affect Structured Output Format (JSON, XML, etc.)
  • My Changes Affect Token Consumption Metrics
  • My Changes Affect Other LLM Functionalities (Reasoning Process, Grounding, Prompt Caching, etc.)
  • Other Changes (Add New Models, Fix Model Parameters etc.)

Version Control (Any Changes to the Plugin Will Require Bumping the Version)

  • I have Bumped Up the Version in Manifest.yaml (Top-Level Version Field, Not in Meta Section)

Dify Plugin SDK Version

  • I have Ensured dify_plugin>=0.3.0,<0.6.0 is in requirements.txt (SDK docs)

Environment Verification (If Any Code Changes)

Local Deployment Environment

  • Dify Version is: , I have Tested My Changes on Local Deployment Dify with a Clean Environment That Matches the Production Configuration.

SaaS Environment

  • I have Tested My Changes on cloud.dify.ai with a Clean Environment That Matches the Production Configuration

- 添加临时文件追踪机制,在类初始化时创建 _temp_files 列表
- 在 _save_base64_to_file 方法中记录创建的临时文件
- 添加 _cleanup_temp_files 方法用于清理临时文件
- 在 _handle_generate_response 和 _handle_generate_stream_response 中使用 try-finally 确保临时文件被清理
- 修复 _upload_file_to_tongyi 方法中的临时文件泄漏问题

这个修复解决了处理 base64 编码的图片/视频和文档上传时临时文件累积的问题,避免长期运行服务时磁盘空间被占用。
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Nov 17, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @WzzP, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在解决通义千问模型在处理多模态输入(如 base64 编码的图片/视频和文档)时产生的临时文件泄漏问题。通过引入一套全面的临时文件追踪和清理机制,确保在模型响应生成和文件上传操作完成后,所有创建的临时文件都能被及时删除,从而有效防止服务长期运行导致磁盘空间被不必要地占用。

Highlights

  • 临时文件追踪机制: 在 TongyiLargeLanguageModel 类初始化时引入 _temp_files 列表,用于追踪所有创建的临时文件。
  • 临时文件记录: _save_base64_to_file 方法现在会将创建的临时文件路径记录到 _temp_files 列表中。
  • 统一清理方法: 新增 _cleanup_temp_files 方法,负责遍历并删除 _temp_files 列表中记录的所有临时文件。
  • 确保清理: 在 _handle_generate_response_handle_generate_stream_response 方法中,使用 try-finally 结构确保在响应生成完成后,无论成功与否,都会调用 _cleanup_temp_files 进行清理。
  • 修复上传文件泄漏: _upload_file_to_tongyi 方法现在也使用 try-finally 块,确保在文件上传到通义模型后,用于上传的临时文件会被删除。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@dosubot dosubot bot added the bug Something isn't working label Nov 17, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次 PR 旨在修复通义千问模型中的临时文件泄漏问题,做得很好。通过引入 _temp_files 列表来追踪临时文件,并在 _save_base64_to_file 中记录,最后通过 try...finally 结构调用新的 _cleanup_temp_files 方法来确保清理,这个方案是正确且可靠的。

我在代码中发现了一个严重问题和一些可以改进的地方:

  1. _upload_file_to_tongyi 方法中,client.files.create 尝试使用一个已经关闭的临时文件对象,这将导致 ValueError。此外,在上传前文件指针没有被重置,可能导致上传空文件。我提供了一个代码建议来修复此问题。
  2. _cleanup_temp_files_upload_file_to_tongyifinally 块中,异常被静默地忽略了。虽然注释说明要记录错误,但代码中并没有实现,这可能会掩盖潜在的文件清理失败问题。建议添加日志记录。

WzzP and others added 2 commits November 17, 2025 13:20
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
- 添加 logging 模块导入和 logger 实例
- 在 _cleanup_temp_files 方法中添加日志记录,记录删除失败的文件
- 在 _upload_file_to_tongyi 的 finally 块中添加日志记录
- 在 _cleanup_temp_files 结束时清空 _temp_files 列表

这些改进解决了 code review 中提到的异常被静默忽略的问题,便于调试和监控。
@crazywoola crazywoola changed the title 修复: 通义千问模型临时文件泄漏问题 Fix: Tongyi Qianwen model temporary file leak issue. Nov 19, 2025
@crazywoola
Copy link
Member

Please bump the version in the manifest as well.

@WzzP
Copy link
Contributor Author

WzzP commented Nov 19, 2025

Please bump the version in the manifest as well.

It has been changed, please check it

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Nov 20, 2025
@crazywoola crazywoola merged commit edf201e into langgenius:main Nov 20, 2025
1 check passed
@WzzP WzzP deleted the fix/tongyi-temp-file-cleanup branch November 20, 2025 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants