Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close websocket connection when pong not received #1814

Closed
wpbloger opened this issue Apr 12, 2017 · 3 comments
Closed

Close websocket connection when pong not received #1814

wpbloger opened this issue Apr 12, 2017 · 3 comments
Labels

Comments

@wpbloger
Copy link

Hello.
I need to disconnect inactive connections to save resources.
The ping/pong perfectly suited, I think it's fine for this.
I installed heartbeat=60 in WebSocketResponse.
But after 60 seconds the connection is not closed and instead throws an exception:
Exception in callback WebSocketResponse._pong_not_received() handle: <TimerHandle when=785637 WebSocketResponse._pong_not_received()> Traceback (most recent call last): File "/usr/local/lib/python3.6/asyncio/events.py", line 127, in _run self._callback(*self._args) File "/lib/python3.6/site-packages/aiohttp/web_ws.py", line 88, in _pong_not_received self._req.transport.close() AttributeError: 'NoneType' object has no attribute 'close'

And I also saw that autoping=True by default, and heartbeat=None through what interval in this case will ping for the connections?

from aiohttp import web

async def index(request):
	ws = web.WebSocketResponse(autoping=True, heartbeat=60)
	await ws.prepare(request)

	request.app['websockets'].append(ws)
	
	print('connection count: '+str(len(request.app['websockets'])))

	async for msg in ws:
		if msg.type == web.WSMsgType.TEXT:
			if msg.data == 'close':
				await ws.close()
			else:
				ws.send_str(msg.data + '/answer')
		elif msg.type == web.WSMsgType.ERROR:
			print('ws connection closed with exception %s' %
				  ws.exception())

	request.app['websockets'].remove(ws)
	print('websocket connection closed')
	print('connection count: '+str(len(request.app['websockets'])))

	return ws

async def on_shutdown(app):
    for ws in app['websockets']:
        await ws.close(code=1012, message='5000')

app = web.Application()
app['websockets'] = []
app.on_shutdown.append(on_shutdown)
app.router.add_get('/', index)
web.run_app(app, host='***.***.***.***', port=8080)

Environment:
Python 3.6.1
aiohttp 2.0.6
Ubuntu 12.04

I did something wrong, or is it a bug in the code?

@fafhrd91
Copy link
Member

it should be fixed in master

@fafhrd91
Copy link
Member

should be fixed in 2.0.7

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants