|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Tests for greenlet behavior during interpreter shutdown (Py_FinalizeEx). |
| 4 | +
|
| 5 | +Prior to the safe finalization fix, active greenlets being deallocated |
| 6 | +during interpreter shutdown could trigger SIGSEGV or SIGABRT on Python |
| 7 | +< 3.11, because green_dealloc attempted to throw GreenletExit via |
| 8 | +g_switch() into a partially-torn-down interpreter. |
| 9 | +
|
| 10 | +The fix adds _Py_IsFinalizing() guards (on Python < 3.11 only) that |
| 11 | +call murder_in_place() instead of g_switch() when the interpreter is |
| 12 | +shutting down, avoiding the crash at the cost of not running cleanup |
| 13 | +code inside the greenlet. |
| 14 | +
|
| 15 | +These tests verify: |
| 16 | + 1. No crashes on ANY Python version (the core safety guarantee). |
| 17 | + 2. GreenletExit cleanup code runs correctly during normal thread exit |
| 18 | + (the standard production path, e.g. uWSGI worker threads). |
| 19 | +""" |
| 20 | +import sys |
| 21 | +import subprocess |
| 22 | +import unittest |
| 23 | +import textwrap |
| 24 | + |
| 25 | +from greenlet.tests import TestCase |
| 26 | + |
| 27 | + |
| 28 | +class TestInterpreterShutdown(TestCase): |
| 29 | + |
| 30 | + def _run_shutdown_script(self, script_body): |
| 31 | + """ |
| 32 | + Run a Python script in a subprocess that exercises greenlet |
| 33 | + during interpreter shutdown. Returns (returncode, stdout, stderr). |
| 34 | + """ |
| 35 | + full_script = textwrap.dedent(script_body) |
| 36 | + result = subprocess.run( |
| 37 | + [sys.executable, '-c', full_script], |
| 38 | + capture_output=True, |
| 39 | + text=True, |
| 40 | + timeout=30, |
| 41 | + check=False, |
| 42 | + ) |
| 43 | + return result.returncode, result.stdout, result.stderr |
| 44 | + |
| 45 | + # ----------------------------------------------------------------- |
| 46 | + # Core safety tests: no crashes on any Python version |
| 47 | + # ----------------------------------------------------------------- |
| 48 | + |
| 49 | + def test_active_greenlet_at_shutdown_no_crash(self): |
| 50 | + """ |
| 51 | + An active (suspended) greenlet that is deallocated during |
| 52 | + interpreter shutdown should not crash the process. |
| 53 | +
|
| 54 | + Before the fix, this would SIGSEGV on Python < 3.11 because |
| 55 | + _green_dealloc_kill_started_non_main_greenlet tried to call |
| 56 | + g_switch() during Py_FinalizeEx. |
| 57 | + """ |
| 58 | + rc, stdout, stderr = self._run_shutdown_script("""\ |
| 59 | + import greenlet |
| 60 | +
|
| 61 | + def worker(): |
| 62 | + greenlet.getcurrent().parent.switch("from worker") |
| 63 | + return "done" |
| 64 | +
|
| 65 | + g = greenlet.greenlet(worker) |
| 66 | + result = g.switch() |
| 67 | + assert result == "from worker", result |
| 68 | + print("OK: exiting with active greenlet") |
| 69 | + """) |
| 70 | + self.assertEqual(rc, 0, f"Process crashed (rc={rc}):\n{stdout}{stderr}") |
| 71 | + self.assertIn("OK: exiting with active greenlet", stdout) |
| 72 | + |
| 73 | + def test_multiple_active_greenlets_at_shutdown(self): |
| 74 | + """ |
| 75 | + Multiple suspended greenlets at shutdown should all be cleaned |
| 76 | + up without crashing. |
| 77 | + """ |
| 78 | + rc, stdout, stderr = self._run_shutdown_script("""\ |
| 79 | + import greenlet |
| 80 | +
|
| 81 | + def worker(name): |
| 82 | + greenlet.getcurrent().parent.switch(f"hello from {name}") |
| 83 | + return "done" |
| 84 | +
|
| 85 | + greenlets = [] |
| 86 | + for i in range(10): |
| 87 | + g = greenlet.greenlet(worker) |
| 88 | + result = g.switch(f"g{i}") |
| 89 | + greenlets.append(g) |
| 90 | +
|
| 91 | + print(f"OK: {len(greenlets)} active greenlets at shutdown") |
| 92 | + """) |
| 93 | + self.assertEqual(rc, 0, f"Process crashed (rc={rc}):\n{stdout}{stderr}") |
| 94 | + self.assertIn("OK: 10 active greenlets at shutdown", stdout) |
| 95 | + |
| 96 | + def test_nested_greenlets_at_shutdown(self): |
| 97 | + """ |
| 98 | + Nested (chained parent) greenlets at shutdown should not crash. |
| 99 | + """ |
| 100 | + rc, stdout, stderr = self._run_shutdown_script("""\ |
| 101 | + import greenlet |
| 102 | +
|
| 103 | + def inner(): |
| 104 | + greenlet.getcurrent().parent.switch("inner done") |
| 105 | +
|
| 106 | + def outer(): |
| 107 | + g_inner = greenlet.greenlet(inner) |
| 108 | + g_inner.switch() |
| 109 | + greenlet.getcurrent().parent.switch("outer done") |
| 110 | +
|
| 111 | + g = greenlet.greenlet(outer) |
| 112 | + result = g.switch() |
| 113 | + assert result == "outer done", result |
| 114 | + print("OK: nested greenlets at shutdown") |
| 115 | + """) |
| 116 | + self.assertEqual(rc, 0, f"Process crashed (rc={rc}):\n{stdout}{stderr}") |
| 117 | + self.assertIn("OK: nested greenlets at shutdown", stdout) |
| 118 | + |
| 119 | + def test_threaded_greenlets_at_shutdown(self): |
| 120 | + """ |
| 121 | + Greenlets in worker threads that are still referenced at |
| 122 | + shutdown should not crash. |
| 123 | + """ |
| 124 | + rc, stdout, stderr = self._run_shutdown_script("""\ |
| 125 | + import greenlet |
| 126 | + import threading |
| 127 | +
|
| 128 | + results = [] |
| 129 | +
|
| 130 | + def thread_worker(): |
| 131 | + def greenlet_func(): |
| 132 | + greenlet.getcurrent().parent.switch("from thread greenlet") |
| 133 | + return "done" |
| 134 | +
|
| 135 | + g = greenlet.greenlet(greenlet_func) |
| 136 | + val = g.switch() |
| 137 | + results.append((g, val)) |
| 138 | +
|
| 139 | + threads = [] |
| 140 | + for _ in range(3): |
| 141 | + t = threading.Thread(target=thread_worker) |
| 142 | + t.start() |
| 143 | + threads.append(t) |
| 144 | +
|
| 145 | + for t in threads: |
| 146 | + t.join() |
| 147 | +
|
| 148 | + print(f"OK: {len(results)} threaded greenlets at shutdown") |
| 149 | + """) |
| 150 | + self.assertEqual(rc, 0, f"Process crashed (rc={rc}):\n{stdout}{stderr}") |
| 151 | + self.assertIn("OK: 3 threaded greenlets at shutdown", stdout) |
| 152 | + |
| 153 | + # ----------------------------------------------------------------- |
| 154 | + # Cleanup semantics tests |
| 155 | + # ----------------------------------------------------------------- |
| 156 | + # |
| 157 | + # Note on behavioral testing during interpreter shutdown: |
| 158 | + # |
| 159 | + # During Py_FinalizeEx, sys.stdout is set to None early, making |
| 160 | + # print() a no-op. More importantly, an active greenlet in the |
| 161 | + # module-level scope interferes with module dict clearing — the |
| 162 | + # greenlet's dealloc path (which temporarily resurrects the object |
| 163 | + # and performs a stack switch via g_switch) prevents reliable |
| 164 | + # observation of cleanup behavior. |
| 165 | + # |
| 166 | + # The production crash (SIGSEGV/SIGABRT) occurs during thread-state |
| 167 | + # cleanup in Py_FinalizeEx, not during module dict clearing. Our |
| 168 | + # _Py_IsFinalizing() guard in _green_dealloc_kill_started_non_main_ |
| 169 | + # greenlet targets that path. The safety tests above verify that no |
| 170 | + # crashes occur; the tests below verify that greenlet cleanup works |
| 171 | + # correctly during normal thread exit (the most common code path). |
| 172 | + |
| 173 | + def test_greenlet_cleanup_during_thread_exit(self): |
| 174 | + """ |
| 175 | + When a thread exits normally while holding active greenlets, |
| 176 | + GreenletExit IS thrown and cleanup code runs. This is the |
| 177 | + standard cleanup path used in production (e.g. uWSGI worker |
| 178 | + threads finishing a request). |
| 179 | + """ |
| 180 | + rc, stdout, stderr = self._run_shutdown_script("""\ |
| 181 | + import os |
| 182 | + import threading |
| 183 | + import greenlet |
| 184 | +
|
| 185 | + _write = os.write |
| 186 | +
|
| 187 | + def thread_func(): |
| 188 | + def worker(_w=_write, |
| 189 | + _GreenletExit=greenlet.GreenletExit): |
| 190 | + try: |
| 191 | + greenlet.getcurrent().parent.switch("suspended") |
| 192 | + except _GreenletExit: |
| 193 | + _w(1, b"CLEANUP: GreenletExit caught\\n") |
| 194 | + raise |
| 195 | +
|
| 196 | + g = greenlet.greenlet(worker) |
| 197 | + g.switch() |
| 198 | + # Thread exits with active greenlet -> thread-state |
| 199 | + # cleanup triggers GreenletExit |
| 200 | +
|
| 201 | + t = threading.Thread(target=thread_func) |
| 202 | + t.start() |
| 203 | + t.join() |
| 204 | + print("OK: thread cleanup done") |
| 205 | + """) |
| 206 | + self.assertEqual(rc, 0, f"Process crashed (rc={rc}):\n{stdout}{stderr}") |
| 207 | + self.assertIn("OK: thread cleanup done", stdout) |
| 208 | + self.assertIn("CLEANUP: GreenletExit caught", stdout) |
| 209 | + |
| 210 | + def test_finally_block_during_thread_exit(self): |
| 211 | + """ |
| 212 | + try/finally blocks in active greenlets run correctly when the |
| 213 | + owning thread exits. |
| 214 | + """ |
| 215 | + rc, stdout, stderr = self._run_shutdown_script("""\ |
| 216 | + import os |
| 217 | + import threading |
| 218 | + import greenlet |
| 219 | +
|
| 220 | + _write = os.write |
| 221 | +
|
| 222 | + def thread_func(): |
| 223 | + def worker(_w=_write): |
| 224 | + try: |
| 225 | + greenlet.getcurrent().parent.switch("suspended") |
| 226 | + finally: |
| 227 | + _w(1, b"FINALLY: cleanup executed\\n") |
| 228 | +
|
| 229 | + g = greenlet.greenlet(worker) |
| 230 | + g.switch() |
| 231 | +
|
| 232 | + t = threading.Thread(target=thread_func) |
| 233 | + t.start() |
| 234 | + t.join() |
| 235 | + print("OK: thread cleanup done") |
| 236 | + """) |
| 237 | + self.assertEqual(rc, 0, f"Process crashed (rc={rc}):\n{stdout}{stderr}") |
| 238 | + self.assertIn("OK: thread cleanup done", stdout) |
| 239 | + self.assertIn("FINALLY: cleanup executed", stdout) |
| 240 | + |
| 241 | + def test_many_greenlets_with_cleanup_at_shutdown(self): |
| 242 | + """ |
| 243 | + Stress test: many active greenlets with cleanup code at shutdown. |
| 244 | + Ensures no crashes regardless of deallocation order. |
| 245 | + """ |
| 246 | + rc, stdout, stderr = self._run_shutdown_script("""\ |
| 247 | + import sys |
| 248 | + import greenlet |
| 249 | +
|
| 250 | + cleanup_count = 0 |
| 251 | +
|
| 252 | + def worker(idx): |
| 253 | + global cleanup_count |
| 254 | + try: |
| 255 | + greenlet.getcurrent().parent.switch(f"ready-{idx}") |
| 256 | + except greenlet.GreenletExit: |
| 257 | + cleanup_count += 1 |
| 258 | + raise |
| 259 | +
|
| 260 | + greenlets = [] |
| 261 | + for i in range(50): |
| 262 | + g = greenlet.greenlet(worker) |
| 263 | + result = g.switch(i) |
| 264 | + greenlets.append(g) |
| 265 | +
|
| 266 | + print(f"OK: {len(greenlets)} greenlets about to shut down") |
| 267 | + # Note: we can't easily print cleanup_count during shutdown |
| 268 | + # since it happens after the main module's code runs. |
| 269 | + """) |
| 270 | + self.assertEqual(rc, 0, f"Process crashed (rc={rc}):\n{stdout}{stderr}") |
| 271 | + self.assertIn("OK: 50 greenlets about to shut down", stdout) |
| 272 | + |
| 273 | + def test_deeply_nested_greenlets_at_shutdown(self): |
| 274 | + """ |
| 275 | + Deeply nested greenlet parent chains at shutdown. |
| 276 | + Tests that the deallocation order doesn't cause issues. |
| 277 | + """ |
| 278 | + rc, stdout, stderr = self._run_shutdown_script("""\ |
| 279 | + import greenlet |
| 280 | +
|
| 281 | + def level(depth, max_depth): |
| 282 | + if depth < max_depth: |
| 283 | + g = greenlet.greenlet(level) |
| 284 | + g.switch(depth + 1, max_depth) |
| 285 | + greenlet.getcurrent().parent.switch(f"depth-{depth}") |
| 286 | +
|
| 287 | + g = greenlet.greenlet(level) |
| 288 | + result = g.switch(0, 10) |
| 289 | + print(f"OK: nested to depth 10, got {result}") |
| 290 | + """) |
| 291 | + self.assertEqual(rc, 0, f"Process crashed (rc={rc}):\n{stdout}{stderr}") |
| 292 | + self.assertIn("OK: nested to depth 10", stdout) |
| 293 | + |
| 294 | + def test_greenlet_with_traceback_at_shutdown(self): |
| 295 | + """ |
| 296 | + A greenlet that has an active exception context when it's |
| 297 | + suspended should not crash during shutdown cleanup. |
| 298 | + """ |
| 299 | + rc, stdout, stderr = self._run_shutdown_script("""\ |
| 300 | + import greenlet |
| 301 | +
|
| 302 | + def worker(): |
| 303 | + try: |
| 304 | + raise ValueError("test error") |
| 305 | + except ValueError: |
| 306 | + # Suspend while an exception is active on the stack |
| 307 | + greenlet.getcurrent().parent.switch("suspended with exc") |
| 308 | + return "done" |
| 309 | +
|
| 310 | + g = greenlet.greenlet(worker) |
| 311 | + result = g.switch() |
| 312 | + assert result == "suspended with exc" |
| 313 | + print("OK: greenlet with active exception at shutdown") |
| 314 | + """) |
| 315 | + self.assertEqual(rc, 0, f"Process crashed (rc={rc}):\n{stdout}{stderr}") |
| 316 | + self.assertIn("OK: greenlet with active exception at shutdown", stdout) |
| 317 | + |
| 318 | + |
| 319 | +if __name__ == '__main__': |
| 320 | + unittest.main() |
0 commit comments