Skip to content

Commit 34b9476

Browse files
committed
fix: save asyncio task refs to prevent GC destruction, add pydantic dep
1 parent 0710faf commit 34b9476

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

ingest/stream_listener.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ def __init__(
4545
self._servers: list[Any] = []
4646
self._transports: list[Any] = []
4747

48+
# Background tasks — must keep strong references to prevent GC
49+
self._tasks: list[asyncio.Task] = []
50+
4851
# Hex stream state
4952
self._hex_header_lines: list[str] = []
5053
self._hex_scan_lines: list[str] = []
@@ -66,7 +69,7 @@ async def start(self) -> None:
6669
await self._start_udp(host, port)
6770

6871
# Background flush timer
69-
asyncio.ensure_future(self._flush_timer())
72+
self._tasks.append(asyncio.get_running_loop().create_task(self._flush_timer()))
7073

7174
logger.info(
7275
"Stream listener started: protocol=%s, %s:%d, format=%s",
@@ -76,12 +79,15 @@ async def start(self) -> None:
7679
async def stop(self) -> None:
7780
"""Stop all listeners and flush remaining records."""
7881
self._running = False
82+
for task in self._tasks:
83+
task.cancel()
7984
for server in self._servers:
8085
server.close()
8186
for transport in self._transports:
8287
transport.close()
8388
await self._flush()
8489
await self._flush_hex()
90+
self._tasks.clear()
8591
logger.info("Stream listener stopped")
8692

8793
# ------------------------------------------------------------------
@@ -90,7 +96,8 @@ async def stop(self) -> None:
9096

9197
async def _start_tcp(self, host: str, port: int) -> None:
9298
if self.config.stream_connect_mode == "client":
93-
asyncio.ensure_future(self._tcp_client_loop(host, port))
99+
task = asyncio.get_running_loop().create_task(self._tcp_client_loop(host, port))
100+
self._tasks.append(task)
94101
else:
95102
server = await asyncio.start_server(self._handle_tcp_connection, host, port)
96103
self._servers.append(server)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ watchdog
1414
# Utilities
1515
python-dotenv
1616
psutil
17+
pydantic
1718

1819
# Oceanstream library (provides all parsing: NMEA, CSV, CTD hex, ADCP)
1920
# For local development, install with: pip install -e ../oceanstream-cli[geotrack,adcp]

0 commit comments

Comments
 (0)