Skip to content

An EventSource creates a session, but fails to close it if the coroutine ends too fast #9

Description

@McAwesome123

If an EventSource is created, but the coroutine it is in ends too early, it creates a session but then never closes it

Running the following script:

import asyncio
import contextlib

from aiohttp_sse_client2 import client as sse_client


async def main():
	listener_task = asyncio.create_task(notification_listener())
	delay_task = asyncio.create_task(delay())

	await asyncio.wait([listener_task, delay_task], return_when=asyncio.FIRST_COMPLETED)


async def delay():
	await asyncio.sleep(0.1)
	return


async def notification_listener():
	async with sse_client.EventSource(
		"https://stream.wikimedia.org/v2/stream/recentchange"
	) as event_source:
		try:
			async for event in event_source:
				print(event)
		except ConnectionError:
			pass


if __name__ == "__main__":
	with contextlib.suppress(KeyboardInterrupt):
		asyncio.run(main())

Consistently produces the following error:

Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x000001C63C1A8440>

However, results may vary depending on server, internet speed, etc. In one instance of testing this (with a different URL), having the delay set to 20 seconds was still too short and resulted in that error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions