Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(src): Close connections to API clients when shutting down #219

Merged
merged 2 commits into from
Dec 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/wsdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,8 @@ def perform_metadata_exchange(self, endpoint, xaddr: str):
self.handle_metadata(stream.read(), endpoint, xaddr)
except urllib.error.URLError as e:
logger.warning('could not fetch metadata from: {} {}'.format(url, e))
except TimeoutError:
logger.warning('metadata exchange with {} timed out'.format(url))

def build_getmetadata_message(self, endpoint) -> str:
tree, _ = self.build_message_tree(endpoint, WSD_GET, None, None)
Expand Down Expand Up @@ -1123,10 +1125,12 @@ def do_POST(self) -> None:
class ApiServer:

address_monitor: 'NetworkAddressMonitor'
clients: List[asyncio.StreamWriter]

def __init__(self, aio_loop: asyncio.AbstractEventLoop, listen_address: bytes,
address_monitor: 'NetworkAddressMonitor') -> None:
self.server = None
self.clients = []
self.address_monitor = address_monitor

# defer server creation
Expand All @@ -1146,6 +1150,7 @@ async def create_server(self, aio_loop: asyncio.AbstractEventLoop, listen_addres
self.on_connect, path=listen_address))

async def on_connect(self, read_stream: asyncio.StreamReader, write_stream: asyncio.StreamWriter) -> None:
self.clients.append(write_stream)
while True:
try:
line = await read_stream.readline()
Expand All @@ -1154,12 +1159,14 @@ async def on_connect(self, read_stream: asyncio.StreamReader, write_stream: asyn
if not write_stream.is_closing():
await write_stream.drain()
else:
self.clients.remove(write_stream)
write_stream.close()
return
except UnicodeDecodeError as e:
logger.debug('invalid input utf8', e)
except Exception as e:
logger.warning('exception in API client', e)
self.clients.remove(write_stream)
write_stream.close()
return

Expand Down Expand Up @@ -1219,6 +1226,8 @@ async def cleanup(self) -> None:
await self.create_task
if self.server:
self.server.close()
for client in self.clients:
client.close()
await self.server.wait_closed()


Expand Down
Loading