File: issue152.py

package info (click to toggle)
python-sse-starlette 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,572 kB
  • sloc: python: 3,856; makefile: 134; sh: 57
file content (34 lines) | stat: -rw-r--r-- 831 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import asyncio

"""
https://github.com/sysid/sse-starlette/issues/152

python examples/issues/issue152.py
"""

async def test_leak():
    from sse_starlette.sse import _ensure_watcher_started_on_this_loop, AppStatus

    def count_watchers():
        return sum(
            1 for t in asyncio.all_tasks()
            if t.get_coro() and '_shutdown_watcher' in t.get_coro().__qualname__
        )

    print(f"Before: {count_watchers()} watchers")

    # Simulate 10 SSE connections
    async def trigger():
        _ensure_watcher_started_on_this_loop()

    await asyncio.gather(*[asyncio.create_task(trigger()) for _ in range(10)])
    await asyncio.sleep(0.5)

    print(f"After:  {count_watchers()} watchers (expected: 1)")

    # Cleanup
    AppStatus.should_exit = True
    await asyncio.sleep(1)


asyncio.run(test_leak())