Skip to content

Commit 6ec593e

Browse files
committed
fix: shutdown code was incorrect
1 parent 36585f9 commit 6ec593e

4 files changed

Lines changed: 41 additions & 7 deletions

File tree

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ publish: check-publish-token build
5959
@uv publish dist/* --index pypi
6060

6161

62+
# Install with python3 -m pip install --index-url https://test.pypi.org/simple/ roverlib
63+

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "roverlib"
3-
version = "0.6.1"
3+
version = "0.6.3"
44
description = "ASE roverlib"
55
readme = "README.md"
66
authors = [

src/roverlib/index.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,59 @@
88
from roverlib.bootinfo import Service, service_from_dict
99
from roverlib.callbacks import MainCallback, TerminationCallback
1010
from roverlib.configuration import ServiceConfiguration, NewServiceConfiguration
11-
from roverlib.streams import CONTEXT
11+
from roverlib.streams import write_streams, read_streams
1212

1313
import zmq
1414
from loguru import logger
1515
import roverlib.rovercom as rovercom
1616

1717

18+
def shutdown_streams():
19+
"""
20+
Properly shutdown all streams and close sockets
21+
"""
22+
global write_streams, read_streams
23+
24+
logger.info("Closing all streams...")
25+
26+
# Close all write stream sockets
27+
for name, write_stream in write_streams.items():
28+
if write_stream.stream.socket is not None:
29+
logger.debug(f"Closing write stream socket: {name}")
30+
write_stream.stream.socket.close()
31+
write_stream.stream.socket = None
32+
33+
# Close all read stream sockets
34+
for name, read_stream in read_streams.items():
35+
if read_stream.stream.socket is not None:
36+
logger.debug(f"Closing read stream socket: {name}")
37+
read_stream.stream.socket.close()
38+
read_stream.stream.socket = None
39+
40+
# Clear the stream dictionaries
41+
write_streams.clear()
42+
read_streams.clear()
43+
44+
logger.info("All streams shut down successfully")
45+
46+
1847
def handle_signals(on_terminate: TerminationCallback):
1948
def signal_handler(sig, frame):
2049
logger.warning(f"Signal received: {sig}")
2150

22-
# Close all sockets before we terminate
23-
CONTEXT.socket.destroy()
51+
# Close all streams and sockets properly
52+
try:
53+
shutdown_streams() # This properly closes all sockets and terminates context
54+
except Exception as e:
55+
logger.error(f"Error during stream shutdown: {e}")
2456

2557
# callback to the service
2658
try:
2759
on_terminate(sig)
28-
os.exit(0)
60+
exit(0)
2961
except Exception as e:
3062
logger.info(f"termination error: {e}")
31-
os.exit(1)
63+
exit(1)
3264

3365
# catch SIGTERM or SIGINT
3466
signal.signal(signal.SIGTERM, signal_handler)

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)