Skip to content

Make OwnedEnv Sync-compatible#724

Open
hauleth wants to merge 1 commit intorusterlium:masterfrom
hauleth:push-szoytyxywytl
Open

Make OwnedEnv Sync-compatible#724
hauleth wants to merge 1 commit intorusterlium:masterfrom
hauleth:push-szoytyxywytl

Conversation

@hauleth
Copy link
Copy Markdown

@hauleth hauleth commented Apr 12, 2026

It should be perfectly fine to do, in the same way #469 makes
OwnedBinary Sync-compatible.

I require it to make OwnedEnv part of the structure that is storing
Terms within its body, just like it happens with ETS.

It should be perfectly fine to do, in the same way rusterlium#469 makes
`OwnedBinary` `Sync`-compatible.

I require it to make `OwnedEnv` part of the structure that is storing
`Term`s within its body, just like it happens with ETS.
@filmor
Copy link
Copy Markdown
Member

filmor commented Apr 12, 2026

Quoting from the docs (https://www.erlang.org/doc/apps/erts/erl_nif.html):

A NIF is thread-safe without any explicit synchronization as long as it acts as a pure function and only reads the supplied arguments. When you write to a shared state either through static variables or enif_priv_data, you need to supply your own explicit synchronization. This includes terms in process independent environments that are shared between threads. Resource objects also require synchronization if you treat them as mutable.

I don't think process-independent environments as such are thread-safe "enough" for Sync.

@hauleth
Copy link
Copy Markdown
Author

hauleth commented Apr 12, 2026

@filmor quoting docs:

Created by calling enif_alloc_env. This environment can be used to store terms between NIF calls and to send terms with enif_send. A process independent environment with all its terms is valid until you explicitly invalidate it with enif_free_env or enif_send.

Emphasis mine. Rustler requires that the value returned from the NIF call must be Sync, which mean that OwnedEnv must be Sync to be used in such situation.

@filmor
Copy link
Copy Markdown
Member

filmor commented Apr 18, 2026

Sorry for the high latency :)

I am not quite following what you are trying to do that would require OwnedEnv to be Sync. Erlang terms are Sync, so all terms living in an OwnedEnv can be passed back from the NIF (though that will in any case first copy the term back into the process environment).

My point was that ErlNifEnv as such might not be thread-safe enough. It does say that this is only relevant for writing, so it could be that the same logic applies as in #469 (Rust's rules around &mut T).

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.

2 participants