mirror of
https://github.com/logos-messaging/nim-ffi.git
synced 2026-06-22 01:10:03 +00:00
myLib lives in non-GC `createShared` memory, so under --mm:refc a GC-managed lib object stored there is invisible to the cycle collector and gets reclaimed mid-operation under sustained request load — a use-after-free that crashes deep in the lib (e.g. a held chronos AsyncLock). Take a GC_ref once a handler installs myLib (tracked by FFIContext.myLibRefd) and GC_unref in freeLib so a later recycle/create can re-pin. Guarded to refc + ref types; orc tracks it precisely. Also wrap freeLib's `=destroy` in try/except: it is conservatively typed as raising, and recycleContext (its async caller) is `raises: []`, so the library would not compile under orc/arc without this. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>