Commit 5f89866
Fix: Raise RuntimeError when ClientSession is used without context manager
This change ensures ClientSession is always used within an 'async with' block
by checking the context manager state before allowing operations that depend
on the background receive loop.
Without this check, calling initialize() outside of a context manager causes
the program to hang indefinitely because the _receive_loop task is only started
in __aenter__.
Changes:
- Add _entered flag to track context manager state
- Override __aenter__ and __aexit__ to manage the flag
- Add _check_is_active() method to enforce proper usage
- Call _check_is_active() in initialize() to prevent hangs
- Add test case for invalid usage
Fixes #1564
Co-authored-by: Cursor <cursoragent@cursor.com>1 parent 8f669a7 commit 5f89866
2 files changed
+40
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
107 | 108 | | |
108 | 109 | | |
109 | 110 | | |
| 111 | + | |
| 112 | + | |
110 | 113 | | |
111 | 114 | | |
112 | 115 | | |
| |||
133 | 136 | | |
134 | 137 | | |
135 | 138 | | |
| 139 | + | |
136 | 140 | | |
137 | 141 | | |
138 | 142 | | |
139 | 143 | | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
140 | 162 | | |
141 | 163 | | |
142 | 164 | | |
| |||
146 | 168 | | |
147 | 169 | | |
148 | 170 | | |
| 171 | + | |
149 | 172 | | |
150 | 173 | | |
151 | 174 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
| |||
706 | 708 | | |
707 | 709 | | |
708 | 710 | | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
0 commit comments