Skip to content

Commit

Permalink
Fixed error processing in StreamingTransport
Browse files Browse the repository at this point in the history
  • Loading branch information
Cykooz committed Jul 7, 2023
1 parent 8516f02 commit 7b3af51
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions sockjs/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def add_endpoint(
cors_config: Optional[CorsConfig] = None,
heartbeat_delay=25,
disconnect_delay=5,
debug=False,
) -> List[web.AbstractRoute]:
registered_routes = []

Expand All @@ -73,6 +74,7 @@ async def handler(m, s, msg):
handler,
heartbeat_delay,
disconnect_delay,
debug=debug,
)

if manager.name != name:
Expand Down
18 changes: 12 additions & 6 deletions sockjs/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def acquire(self, request: web.Request) -> bool:
self._hits += 1

if self.state == SessionState.NEW:
log.debug("open session: %s", self.id)
if self._debug:
log.debug("open session: %s", self.id)
self.state = SessionState.OPEN
self.feed(Frame.OPEN, Frame.OPEN.value)
return True
Expand Down Expand Up @@ -280,7 +281,8 @@ async def stop(self, _app=None):

async def _check_expiration(self, session: Session):
if session.expired:
log.debug("session expired: %s", session.id)
if self.debug:
log.debug("session expired: %s", session.id)
# Session is to be GC'd immediately
if session.id in self.acquired:
await self.release(session)
Expand Down Expand Up @@ -408,7 +410,8 @@ def __del__(self):

async def remote_message(self, session: Session, msg):
"""Call handler with message received from client."""
log.debug("incoming message: %s, %s", session.id, msg[:200])
if self.debug:
log.debug("incoming message: %s, %s", session.id, msg[:200])
session.tick()

try:
Expand All @@ -421,7 +424,8 @@ async def remote_messages(self, session: Session, messages):
session.tick()

for msg in messages:
log.debug("incoming message: %s, %s", session.id, msg[:200])
if self.debug:
log.debug("incoming message: %s, %s", session.id, msg[:200])
try:
await self.handler(self, session, SockjsMessage(MsgType.MESSAGE, msg))
except Exception:
Expand All @@ -432,7 +436,8 @@ async def remote_close(self, session: Session, exc=None):
if session.state in (SessionState.CLOSING, SessionState.CLOSED):
return

log.info("close session: %s", session.id)
if self.debug:
log.info("close session: %s", session.id)
session.tick()
session.state = SessionState.CLOSING
if exc is not None:
Expand All @@ -452,7 +457,8 @@ async def remote_closed(self, session: Session):
session.expire()
return

log.info("session closed: %s", session.id)
if self.debug:
log.info("session closed: %s", session.id)
session.state = SessionState.CLOSED
session.expire()
try:
Expand Down
4 changes: 2 additions & 2 deletions sockjs/transports/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import asyncio

from aiohttp import web
from aiohttp.web_exceptions import HTTPClientError
from aiohttp.web_exceptions import HTTPClientError, HTTPError

from ..exceptions import SessionIsAcquired, SessionIsClosed
from ..protocol import (
Expand Down Expand Up @@ -101,7 +101,7 @@ async def handle_session(self):
stop = await self._send(text)
if stop:
break
except (asyncio.CancelledError, ConnectionError) as e:
except (asyncio.CancelledError, ConnectionError, HTTPError) as e:
await self.manager.remote_close(self.session, exc=e)
await self.manager.remote_closed(self.session)
raise
Expand Down

0 comments on commit 7b3af51

Please sign in to comment.