Skip to content

Commit

Permalink
Gracefully handle SIGTERM in run_server.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mryab committed Nov 3, 2024
1 parent 80e3ec3 commit 30ad882
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 0 additions & 2 deletions hivemind/hivemind_cli/run_dht.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ def signal_handler(signum: int, _) -> None:
while not exit_event.is_set():
dht.run_coroutine(report_status, return_future=False)
exit_event.wait(args.refresh_period)
except KeyboardInterrupt:
logger.info("Caught KeyboardInterrupt, shutting down")
finally:
dht.shutdown()

Expand Down
16 changes: 14 additions & 2 deletions hivemind/hivemind_cli/run_server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from functools import partial
from pathlib import Path
from signal import SIGINT, SIGTERM, signal, strsignal
from threading import Event

import configargparse
import torch
Expand Down Expand Up @@ -102,12 +104,22 @@ def main():
compression_type = args.pop("compression")
compression = getattr(CompressionType, compression_type)

exit_event = Event()

server = Server.create(**args, optim_cls=optim_cls, start=True, compression=compression)

def signal_handler(signum: int, _) -> None:
logger.info(f"Caught signal {signum} ({strsignal(signum)}), shutting down")
exit_event.set()

signal(SIGTERM, signal_handler)
signal(SIGINT, signal_handler)

try:
exit_event.wait()
finally:
server.shutdown()
server.join()
except KeyboardInterrupt:
logger.info("Caught KeyboardInterrupt, shutting down")


if __name__ == "__main__":
Expand Down

0 comments on commit 30ad882

Please sign in to comment.