Skip to content

feat(pdf): add timeout guard for PDF generation (#762)#882

Open
CyberXpert607 wants to merge 2 commits into
rinafcode:mainfrom
CyberXpert607:feat/pdf-timeout-guard
Open

feat(pdf): add timeout guard for PDF generation (#762)#882
CyberXpert607 wants to merge 2 commits into
rinafcode:mainfrom
CyberXpert607:feat/pdf-timeout-guard

Conversation

@CyberXpert607

Copy link
Copy Markdown

Summary

Implements #762: Adds timeout guard for PDF generation to prevent hanging connections.

Changes Made

1. Timeout Utility (src/lib/timeout.ts)

  • ✅ Reusable withTimeout function using Promise.race
  • ✅ Proper timer cleanup to prevent memory leaks

2. PDF Generation Service (src/services/pdf-generation.ts)

  • ✅ Full Puppeteer-based PDF generation
  • page.setDefaultTimeout(25000)
  • ✅ Browser cleanup in finally block
  • ✅ Removed any type with proper Certificate interface

3. Certificate Download Route (src/app/api/certificates/[id]/download/route.ts)

  • ✅ Wrapped PDF generation with 30s timeout
  • ✅ HTTP 504 on timeout with retry message
  • ✅ Proper unknown error handling

4. Generic PDF Generation Route (src/app/api/generate-pdf/route.ts)

  • ✅ Same timeout handling and 504 response
  • ✅ Proper unknown error handling

5. Integration Test

  • ✅ Mocks slow PDF generation
  • ✅ Asserts 504 response with error payload

6. Environment Variable

  • PDF_TIMEOUT_MS=30000 added

Scope Note

Only files directly related to #762 were modified. No other files were touched.

Related Issue

Closes #762

Checklist

  • PDF generation bounded by 30-second timeout
  • HTTP 504 returned on timeout with retry message
  • Puppeteer timeout set to 25 seconds
  • Integration test added
  • Browser cleanup on timeout
  • Timeout configurable via environment variable
  • No out-of-scope files modified

- Add withTimeout utility with proper timer cleanup
- Implement PDF generation service with Puppeteer
- Add 30s timeout to certificate download route
- Add 30s timeout to generic PDF generation route
- Return HTTP 504 on timeout with retry message
- Add integration test for timeout scenario
- Set page.setDefaultTimeout(25000)
- Add PDF_TIMEOUT_MS environment variable
- Remove any types from modified files
@drips-wave

drips-wave Bot commented Jul 1, 2026

Copy link
Copy Markdown

@CyberXpert607 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@RUKAYAT-CODER

RUKAYAT-CODER commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Great job so far

There’s just one blocker — workflow failing. Could you take a look and resolve it?

Happy to review again once that’s done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement] PDF generation in certificate download has no timeout guard

2 participants