Skip to content

Commit

Permalink
Finally fix socket hanging on reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
mrvillage committed Jan 13, 2023
1 parent 3f3275b commit 9526482
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pnwkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from .legacy.keys import set_bot_key, set_key # type: ignore
from .new import *

__version__ = "2.6.12"
__version__ = "2.6.13"

# Set default logging handler to avoid "No handler found" warnings.
logging.getLogger(__name__).addHandler(NullHandler())
Expand Down
7 changes: 4 additions & 3 deletions pnwkit/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -1492,13 +1492,14 @@ async def reconnect(self) -> None:
async def actual_run(self) -> None:
while True:
try:
logger.debug("Waiting for connected WS %s", self.ws)
await self.connected.wait()
logger.debug("Listening for messages WS %s", self.ws)
async for message in self.ws:
try:
# message.type is Unknown
if message.type in {aiohttp.WSMsgType.CLOSED, aiohttp.WSMsgType.CLOSING, aiohttp.WSMsgType.CLOSE}: # type: ignore
await self.handle_socket_close()
asyncio.create_task(self.handle_socket_close())
elif message.type not in {aiohttp.WSMsgType.TEXT}: # type: ignore
continue
# message.data is Unknown
Expand Down Expand Up @@ -1538,7 +1539,7 @@ async def actual_run(self) -> None:
logging.warning(
"Encountered ConnectionResetError in socket", exc_info=e
)
await self.close_and_reconnect()
asyncio.create_task(self.close_and_reconnect())
break
except Exception as e:
utils.print_exception_with_header(
Expand All @@ -1551,7 +1552,7 @@ async def actual_run(self) -> None:
if self.ws.closed:
self.connected.clear()
logger.debug("actual_run -> Socket closed WS %s", self.ws)
await self.handle_socket_close()
asyncio.create_task(self.handle_socket_close())
except asyncio.TimeoutError as e:
utils.print_exception_with_header("Encountered exception in socket", e)
logging.warning("Encountered exception in socket", exc_info=e)
Expand Down

0 comments on commit 9526482

Please sign in to comment.