Commit 9cfb17d
committed
UN-2813 [FIX] Implement graceful shutdown for signal handler
Improve signal handler to properly shut down backend instead of
immediate exit that can leave tasks in inconsistent state.
**Problem:**
Signal handler immediately called sys.exit(0) without giving backend
a chance to finish current tasks or clean up resources:
```python
def _signal_handler(self, signum, frame):
logger.info("Initiating worker shutdown")
sys.exit(0) # ❌ Immediate exit, no cleanup
```
This can cause:
- Tasks left in inconsistent states
- Database connections not closed
- Incomplete work lost
- No proper cleanup of backend resources
**Solution:**
Implement graceful shutdown that:
1. Sets shutdown_requested flag
2. Calls backend.stop(graceful=True) if available
3. Logs errors with full traceback
4. Allows backend to finish current tasks
```python
def _signal_handler(self, signum, frame):
self.shutdown_requested = True
logger.info("Initiating graceful worker shutdown")
if self.backend and hasattr(self.backend, "stop"):
try:
self.backend.stop(graceful=True)
except Exception as e:
logger.error(f"Error during graceful shutdown: {e}", exc_info=True)
sys.exit(0)
```
**Changes:**
1. ✅ Added backend.stop(graceful=True) call before exit
2. ✅ Proper try/except with exc_info logging
3. ✅ Check for backend availability with hasattr
4. ✅ More descriptive log messages
5. ✅ shutdown_requested flag still set for future use
**Impact:**
- ✅ Tasks can complete gracefully before shutdown
- ✅ Backend resources properly cleaned up
- ✅ Better error visibility during shutdown
- ✅ Safer production deployments
**Note:**
Backend implementations should support stop() method for proper
graceful shutdown:
- Stop accepting new tasks
- Wait for in-progress tasks (with timeout)
- Clean up connections and resources
- Exit cleanly
Resolves: CodeRabbit comment #2404866807
Related: #15551 parent 3ee8100 commit 9cfb17d
1 file changed
+10
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
223 | 223 | | |
224 | 224 | | |
225 | 225 | | |
226 | | - | |
| 226 | + | |
227 | 227 | | |
228 | 228 | | |
229 | 229 | | |
230 | | - | |
231 | | - | |
232 | | - | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
233 | 239 | | |
234 | 240 | | |
235 | 241 | | |
| |||
0 commit comments