Skip to content

Commit 3dfdbc3

Browse files
lmicciniclaude
andcommitted
Handle SIGTERM in AMQP proxy for graceful pod termination
The Python AMQP proxy sidecar only handled KeyboardInterrupt (SIGINT), not SIGTERM. Since kubelet sends SIGTERM to stop containers, the proxy hung indefinitely preventing pod termination. Register a SIGTERM handler that cancels all proxy tasks and catches the resulting CancelledError during shutdown. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 2a70e75 commit 3dfdbc3

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

  • internal/controller/rabbitmq/data

internal/controller/rabbitmq/data/proxy.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import asyncio
1919
import argparse
2020
import logging
21+
import signal
2122
import struct
2223
import sys
2324
import ssl
@@ -909,14 +910,21 @@ async def main():
909910
# Start periodic stats
910911
stats_task = asyncio.create_task(periodic_stats(proxy, args.stats_interval))
911912

913+
# Handle SIGTERM (sent by kubelet) the same as SIGINT so the proxy
914+
# shuts down gracefully instead of hanging until the
915+
# terminationGracePeriodSeconds expires.
916+
loop = asyncio.get_running_loop()
917+
loop.add_signal_handler(signal.SIGTERM, server.close)
918+
912919
try:
913920
async with server:
914921
await server.serve_forever()
915-
except KeyboardInterrupt:
922+
except (KeyboardInterrupt, asyncio.CancelledError):
916923
logger.info("Shutting down...")
917924
finally:
918925
stats_task.cancel()
919926
proxy.print_stats()
927+
logger.info("Proxy stopped")
920928

921929

922930
if __name__ == '__main__':

0 commit comments

Comments
 (0)