Skip to content

Commit

Permalink
Merge pull request #101 from gamultong/hotfix/handle-send-disconnected
Browse files Browse the repository at this point in the history
커넥션 끊겼을 때 send 처리 (아마 진짜 최종)
  • Loading branch information
onee-only authored Dec 30, 2024
2 parents b5bd526 + 08d8d55 commit 1b7cf8a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 3 additions & 3 deletions conn/internal/conn.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi.websockets import WebSocket, WebSocketState
from fastapi.websockets import WebSocket, WebSocketState, WebSocketDisconnect
from websockets.exceptions import ConnectionClosed
from message import Message
from dataclasses import dataclass
Expand All @@ -23,11 +23,11 @@ async def receive(self):
return Message.from_str(await self.conn.receive_text())

async def send(self, msg: Message):
if self.conn.client_state == WebSocketState.DISCONNECTED:
if self.conn.application_state == WebSocketState.DISCONNECTED:
return

try:
await self.conn.send_text(msg.to_str())
except ConnectionClosed:
except (ConnectionClosed, WebSocketDisconnect):
# 커넥션이 종료되었는데도 타이밍 문제로 인해 커넥션을 가져왔을 수 있음.
return
3 changes: 2 additions & 1 deletion conn/test/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest.mock import MagicMock, AsyncMock

from fastapi.websockets import WebSocket
from fastapi.websockets import WebSocket, WebSocketState


class ConnMock():
Expand All @@ -9,6 +9,7 @@ def __init__(self):
self.receive_text = AsyncMock()
self.send_text = AsyncMock()
self.close = AsyncMock()
self.application_state = WebSocketState.CONNECTED


def create_connection_mock() -> ConnMock:
Expand Down

0 comments on commit 1b7cf8a

Please sign in to comment.