CactusDev / CactusBot

An open source, community-written service-agnostic chat bot
MIT License
30 stars 5 forks source link

Clean shutdown #62

Closed 2Cubed closed 6 years ago

2Cubed commented 8 years ago

When sent a KeyboardInterrupt, the bot should gracefully close all websockets and display a shutdown message, rather than spewing out all sorts of asyncio error nonsense.

2Cubed commented 7 years ago

Much of the "spam" seems to be coming from not closing ClientResponses from the base API class. Need to look into this - if left running for long periods of time, it could become a serious memory hog.

pylang commented 7 years ago

This issue suggests session.close() must be explicitly called. I'm hoping async with will handle this in the future.

Note: For reference, I add the following:

As mentioned in the Discord, one option might be chaining close commands for all opened sessions. See a rough idea using simple files in this SO post. From this inspiration, it may be possible to add names of opened sessions (or instances) to a list of classes that inherit ClientSession, e.g. API and Websocket, then call session.closed() on each by iteration.

2Cubed commented 7 years ago

Current "State of the Explosions™":

^CTraceback (most recent call last):
  File "run.py", line 46, in <module>
    loop.run_until_complete(run())
  File "/usr/lib/python3.6/asyncio/base_events.py", line 454, in run_until_complete
    self.run_forever()
  File "/usr/lib/python3.6/asyncio/base_events.py", line 421, in run_forever
    self._run_once()
  File "/usr/lib/python3.6/asyncio/base_events.py", line 1389, in _run_once
    event_list = self._selector.select(timeout)
  File "/usr/lib/python3.6/selectors.py", line 445, in select
    fd_event_list = self._epoll.poll(timeout, max_ev)
KeyboardInterrupt
2017-03-28 02:04:24 ERROR asyncio default_exception_handler: Unclosed client session
client_session: <cactusbot.api.CactusAPI object at 0x7f523c9c7d30>
2017-03-28 02:04:24 ERROR asyncio default_exception_handler: Unclosed client session
client_session: <cactusbot.services.beam.api.BeamAPI object at 0x7f523b8c9588>
2017-03-28 02:04:24 ERROR asyncio default_exception_handler: Unclosed client session
client_session: <cactusbot.services.beam.constellation.BeamConstellation object at 0x7f523a84bf60>
2017-03-28 02:04:24 ERROR asyncio default_exception_handler: Unclosed client session
client_session: <cactusbot.services.beam.chat.BeamChat object at 0x7f523a877898>
2017-03-28 02:04:24 ERROR asyncio default_exception_handler: Unclosed response
client_response: <ClientResponse(wss://constellation.beam.pro) [101 Switching Protocols]>
<CIMultiDictProxy('Date': 'Tue, 28 Mar 2017 06:04:21 GMT', 'Connection': 'upgrade', 'Set-Cookie': '__cfduid=da921f6ac8042c15ddebab17be0b10fee1490681060; expires=Wed, 28-Mar-18 06:04:20 GMT; path=/; domain=.beam.pro; HttpOnly', 'Upgrade': 'websocket', 'Sec-Websocket-Accept': '5Yi/47qTD/aKRhK+Yil8eweKWyk=', 'Server': 'cloudflare-nginx', 'Cf-Ray': '34686436dbee23a8-IAD')>

2017-03-28 02:04:24 ERROR asyncio default_exception_handler: Unclosed response
client_response: <ClientResponse(wss://chat3-dal.beam.pro:443) [101 Switching Protocols]>
<CIMultiDictProxy('Date': 'Tue, 28 Mar 2017 06:04:21 GMT', 'Connection': 'upgrade', 'Set-Cookie': '__cfduid=d7adeb860485d9fb833dad6ab2e0a2d741490681061; expires=Wed, 28-Mar-18 06:04:21 GMT; path=/; domain=.beam.pro; HttpOnly', 'Upgrade': 'websocket', 'Sec-Websocket-Accept': 'XHnsvZWAmsOJ7KYB9A0Pd2W3csQ=', 'Server': 'cloudflare-nginx', 'Cf-Ray': '346864389c640862-IAD')>

2017-03-28 02:04:24 ERROR asyncio default_exception_handler: Task was destroyed but it is pending!
task: <Task pending coro=<WebSocket.read() running at /home/user/Documents/Cactus/CactusBot/cactusbot/services/websocket.py:69> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f523b97fa38>()]>>
2017-03-28 02:04:24 ERROR asyncio default_exception_handler: Unclosed response
client_response: <ClientResponse(wss://cactus.exoz.one/sepal) [101 Switching Protocols]>
<CIMultiDictProxy('Date': 'Tue, 28 Mar 2017 06:04:20 GMT', 'Connection': 'upgrade', 'Set-Cookie': '__cfduid=daf67f7da4e5c439932bae81c1253cba11490681060; expires=Wed, 28-Mar-18 06:04:20 GMT; path=/; domain=.exoz.one; HttpOnly', 'Upgrade': 'websocket', 'Sec-Websocket-Accept': 'JHb+d4wWSFSms4OngQ8ldcoLQio=', 'Server': 'cloudflare-nginx', 'Cf-Ray': '34686431496a0f2d-IAD')>

2017-03-28 02:04:24 ERROR asyncio default_exception_handler: Task was destroyed but it is pending!
task: <Task pending coro=<WebSocket.read() running at /home/user/Documents/Cactus/CactusBot/cactusbot/services/websocket.py:69> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f523fa17918>()]>>
2Cubed commented 6 years ago

No longer relevant.

pylang commented 6 years ago

How come?