Skip to content

Commit

Permalink
use exceptiongroup.catch
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-anthropic committed May 9, 2024
1 parent 36cf97a commit 56d7dbf
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/hypercorn/trio/tcp_server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

import sys
from math import inf
from typing import Any, Generator, Optional

import exceptiongroup
import trio

from .task_group import TaskGroup
Expand Down Expand Up @@ -51,28 +53,31 @@ async def run(self) -> None:
socket = self.stream.socket
ssl = False

def log_handler(e: Exception):
if self.config.log.error_logger is not None:
self.config.log.error_logger.exception("Internal hypercorn error")

try:
client = parse_socket_addr(socket.family, socket.getpeername())
server = parse_socket_addr(socket.family, socket.getsockname())

async with TaskGroup() as task_group:
self._task_group = task_group
self.protocol = ProtocolWrapper(
self.app,
self.config,
self.context,
task_group,
ssl,
client,
server,
self.protocol_send,
alpn_protocol,
)
await self.protocol.initiate()
await self._start_idle()
await self._read_data()
except (OSError, BaseExceptionGroup):
await self.config.log.exception("Internal hypercorn error")
with exceptiongroup.catch({Exception: log_handler}):
client = parse_socket_addr(socket.family, socket.getpeername())
server = parse_socket_addr(socket.family, socket.getsockname())

async with TaskGroup() as task_group:
self._task_group = task_group
self.protocol = ProtocolWrapper(
self.app,
self.config,
self.context,
task_group,
ssl,
client,
server,
self.protocol_send,
alpn_protocol,
)
await self.protocol.initiate()
await self._start_idle()
await self._read_data()
finally:
await self._close()

Expand Down

0 comments on commit 56d7dbf

Please sign in to comment.