[PR #4352] fix: add timeout to SMTPClient to prevent worker blocking #24493

Closed
opened 2026-02-21 20:23:07 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/4352

State: closed
Merged: Yes


Description

This PR adds a timeout parameter to the SMTP constructor in the SMTPClient class to prevent the worker from blocking indefinitely when the SMTP server becomes unresponsive. Currently, if the server hangs, the worker blocks permanently, causing reduced system responsiveness and potential disruptions.

The SMTPClient will wait for a maximum of 10 seconds for the server to respond. If no response is received within that time, a TimeoutError exception is raised, allowing the worker to gracefully handle the situation and proceed with other tasks.

The PR also includes improved error handling by catching specific exceptions (SMTPException, TimeoutError, and unexpected exceptions) and logging error messages for better visibility and debugging.

Type of Change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update, included: Dify Document
  • Improvement, including but not limited to code refactoring, performance optimization, and UI/UX improvement
  • Dependency upgrade

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Tested SMTPClient with a valid SMTP server, verifying emails are sent within the specified timeout.
  • Simulated an unresponsive SMTP server scenario, confirming TimeoutError is raised and worker doesn't block indefinitely.
  • Verified caught exceptions are properly logged using the logging module.

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
  • optional I have made corresponding changes to the documentation
  • optional I have added tests that prove my fix is effective or that my feature works
  • optional New and existing unit tests pass locally with my changes
**Original Pull Request:** https://github.com/langgenius/dify/pull/4352 **State:** closed **Merged:** Yes --- # Description This PR adds a timeout parameter to the `SMTP` constructor in the `SMTPClient` class to prevent the worker from blocking indefinitely when the SMTP server becomes unresponsive. Currently, if the server hangs, the worker blocks permanently, causing reduced system responsiveness and potential disruptions. The `SMTPClient` will wait for a maximum of 10 seconds for the server to respond. If no response is received within that time, a `TimeoutError` exception is raised, allowing the worker to gracefully handle the situation and proceed with other tasks. The PR also includes improved error handling by catching specific exceptions (`SMTPException`, `TimeoutError`, and unexpected exceptions) and logging error messages for better visibility and debugging. ## Type of Change Please delete options that are not relevant. - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] Improvement, including but not limited to code refactoring, performance optimization, and UI/UX improvement - [ ] Dependency upgrade # How Has This Been Tested? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration - [x] Tested SMTPClient with a valid SMTP server, verifying emails are sent within the specified timeout. - [x] Simulated an unresponsive SMTP server scenario, confirming TimeoutError is raised and worker doesn't block indefinitely. - [x] Verified caught exceptions are properly logged using the logging module. # Suggested Checklist: - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [x] My changes generate no new warnings - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods - [ ] `optional` I have made corresponding changes to the documentation - [ ] `optional` I have added tests that prove my fix is effective or that my feature works - [ ] `optional` New and existing unit tests pass locally with my changes
yindo added the pull-request label 2026-02-21 20:23:07 -05:00
yindo closed this issue 2026-02-21 20:23:07 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#24493