Improve Backend Infrastructure: Graceful Shutdown, Testing, and Error Handling#24
Improve Backend Infrastructure: Graceful Shutdown, Testing, and Error Handling#24DevMuhammed3 wants to merge 2 commits intomainfrom
Conversation
- Added graceful shutdown handling for SIGINT and SIGTERM - Set up Vitest testing infrastructure and added sample unit tests - Enhanced global error handling for Zod validation errors Co-authored-by: DevMuhammed3 <[email protected]>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Review Summary by QodoAdd graceful shutdown, testing infrastructure, and validation error handling
WalkthroughsDescription• Added graceful shutdown handling for SIGINT and SIGTERM signals • Integrated Vitest testing framework with sample unit tests • Enhanced global error handler for Zod validation errors • Added vitest dependency to backend package.json Diagramflowchart LR
A["Backend Infrastructure"] --> B["Graceful Shutdown"]
A --> C["Testing Setup"]
A --> D["Error Handling"]
B --> B1["SIGINT/SIGTERM Handlers"]
B --> B2["Server & DB Cleanup"]
C --> C1["Vitest Integration"]
C --> C2["resolveAssetUrl Tests"]
D --> D1["Zod Validation Errors"]
D --> D2["Field-specific Feedback"]
File Changes1. apps/backend/src/app.ts
|
Code Review by Qodo
|
| const gracefulShutdown = async (signal: string) => { | ||
| console.log(`\n${signal} received. Shutting down gracefully...`) | ||
|
|
||
| clearInterval(presenceCleanup) | ||
|
|
||
| server.close(async (err) => { | ||
| if (err) { | ||
| console.error('Error closing server:', err) | ||
| process.exit(1) | ||
| } | ||
|
|
||
| console.log('HTTP server closed.') | ||
|
|
||
| try { | ||
| await prisma.$disconnect() | ||
| console.log('Database connection closed.') | ||
| process.exit(0) | ||
| } catch (dbErr) { | ||
| console.error('Error during database disconnection:', dbErr) | ||
| process.exit(1) | ||
| } | ||
| }) | ||
|
|
||
| // Force close after 10 seconds if graceful shutdown fails | ||
| setTimeout(() => { | ||
| console.error('Could not close connections in time, forcefully shutting down') | ||
| process.exit(1) | ||
| }, 10000) |
There was a problem hiding this comment.
1. Shutdown skips socket.io close 🐞 Bug ☼ Reliability
In apps/backend/src/index.ts, gracefulShutdown() calls server.close() but never closes the Socket.IO server, so long-lived websocket connections can prevent the close callback from firing and the code will hit the 10s forced process.exit(1). This defeats the “graceful” shutdown and can terminate clients/cleanup abruptly even when the only remaining work is open sockets.
Agent Prompt
### Issue description
`gracefulShutdown()` closes the HTTP server and Prisma but leaves Socket.IO running. With active websocket connections, `server.close()` may never invoke its callback, causing the 10s timeout to force `process.exit(1)`.
### Issue Context
The backend uses Socket.IO for real-time features; sockets are long-lived and need an explicit shutdown to let the HTTP server fully close.
### Fix Focus Areas
- apps/backend/src/index.ts[114-141]
### Suggested fix
- Add an idempotency guard (e.g., `let isShuttingDown = false`) to avoid double shutdown on repeated signals.
- Explicitly stop Socket.IO before/alongside `server.close()`, e.g.:
- `io.disconnectSockets(true)` (optional, if you want to actively drop clients)
- `io.close()` to close the engine and release handles
- Store the force-timeout handle and `clearTimeout()` it when shutdown completes.
- Consider calling `await prisma.$disconnect()` in the force-timeout path (best-effort) before exiting.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
- Replaced Vitest with Jest and ts-jest in apps/backend - Configured Jest for ESM support using NODE_OPTIONS - Added GitHub Actions workflow (.github/workflows/ci.yml) for automated testing and linting - Migrated resolveAssetUrl tests to Jest format - Enhanced backend error handling for Zod validation errors - Implemented graceful shutdown in backend index.ts Co-authored-by: DevMuhammed3 <[email protected]>
I have implemented several key improvements to the backend codebase:
apps/backend/src/index.tsto handleSIGINTandSIGTERMsignals. This ensures the HTTP server and Prisma client are closed cleanly, and any active intervals are cleared before the process exits.testscript topackage.jsonand a comprehensive test suite for theresolveAssetUrlutility to establish a testing pattern for the project.apps/backend/src/app.tsto explicitly handle Zod validation errors. This provides API consumers with clearer, field-specific feedback when a request fails validation.These changes enhance the reliability, maintainability, and developer experience of the OpenChat platform.
PR created automatically by Jules for task 15727824381294086827 started by @DevMuhammed3