The Railway deployment was failing with a CRITICAL WORKER TIMEOUT error during payment verification.
Error Trace: smtplib.SMTP(self.mail.server, self.mail.port) -> socket.create_connection hanging.
The email sending was synchronous (blocking). The connection to the Gmail SMTP server was taking longer than the Gunicorn worker timeout (likely 30s), causing the application worker to be killed by the master process before it could respond to the payment verification request.
Converted email sending to be asynchronous using background threads.
- Imported
threading: Added support for background execution. - Updated
send_booking_confirmation_email:- Instead of sending immediately (blocking), it now starts a detached thread to handle the SMTP connection and sending.
- The main request returns immediately, preventing the worker timeout.
- Updated
send_order_confirmation_email:- Applied the same async pattern to product order emails.
- Zero Latency: The user/payment gateway gets an instant success response.
- Resilience: Even if the email server is slow or momentarily unreachable, it won't crash the web server process.
- Scalability: Prevents a backlog of requests waiting for email delivery.
Deploy the changes. The CRITICAL WORKER TIMEOUT error should disappear, and emails will still be delivered in the background.