Skip to content

Harden NIF resource ownership - #94

Open
benoitc wants to merge 4 commits into
mainfrom
fix/nif-resource-safety
Open

benoitc wants to merge 4 commits into
mainfrom
fix/nif-resource-safety

Conversation

@benoitc

@benoitc benoitc commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Shared dictionaries could deadlock the Erlang node when Python pickling re-entered the same dictionary or when Erlang and Python acquired the dictionary lock and GIL in opposite orders. Their interpreter-owned storage was also unsafe to access from owngil contexts. Several partially constructed NIF resources could clean up uninitialized state or release the same state twice, and some native size calculations could wrap.

Shared dictionaries now store serialized bytes in a mutex-protected native table, with Python serialization performed outside the table lock. Resource constructors track initialized fields so their destructors safely handle every failure point. Native allocation, frame-length and growth calculations now reject values that cannot be represented safely. Regression coverage exercises reentrant pickling and cross-interpreter SharedDict access.

There are no public API changes.

Prevent SharedDict lock inversion and cross-interpreter object access by storing serialized values in native memory. Guard partial resource construction, allocation failures and size arithmetic so cleanup and oversized inputs cannot corrupt or stall the VM.
Keep the timed operation well beyond its deadline so sanitizer scheduling overhead cannot let it complete before Erlang processes the timeout. The following call continues to verify that the reimport session recovers.
Use a one-second timed operation so sanitizer overhead cannot beat the deadline while Python versions that finish the sleep before recovery still keep the soak suite within its timetrap.
Queue Python namespace cleanup behind in-flight work instead of waiting during stop, so a timed-out call cannot make close kill the linked session owner. Gate timeout tests on an explicit release signal so scheduler delays cannot invert their result.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant