Commit 2f3f1ba
committed
fix: serialize cross-extension Go calls via a process-wide pthread mutex
Loading two gopy extensions in the same Python process embeds two
independent Go runtimes. On macOS x86_64 / Go ≥1.24 this causes
"fatal error: bad sweepgen in refill" (issue #370) when both runtimes
run Go code concurrently.
Add a process-wide pthread_mutex_t stored as a Python capsule in
builtins._gopy_global_mu so every gopy extension in the same interpreter
shares the same lock. The generated CGo wrappers:
1. Call gopy_ensure_mu() (lazy init, Python GIL must be held) before
releasing the GIL.
2. Release the GIL via PyEval_SaveThread.
3. Acquire the mutex via gopy_lock() — blocking until any other
extension's Go call finishes.
4. Release the mutex (gopy_unlock()) before restoring the GIL
(PyEval_RestoreThread), avoiding the GIL/mutex deadlock.
On Windows the lock/unlock are compiled as no-ops.
Fixes #370 / #385.1 parent 0299311 commit 2f3f1ba
2 files changed
Lines changed: 48 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
90 | 126 | | |
91 | 127 | | |
92 | 128 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
305 | 305 | | |
306 | 306 | | |
307 | 307 | | |
308 | | - | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
309 | 314 | | |
| 315 | + | |
310 | 316 | | |
311 | | - | |
| 317 | + | |
312 | 318 | | |
| 319 | + | |
| 320 | + | |
313 | 321 | | |
314 | 322 | | |
315 | 323 | | |
| |||
462 | 470 | | |
463 | 471 | | |
464 | 472 | | |
465 | | - | |
| 473 | + | |
| 474 | + | |
466 | 475 | | |
467 | 476 | | |
468 | 477 | | |
| |||
0 commit comments