Skip to content

Add process-bound Python environments#27

Merged
benoitc merged 5 commits intomainfrom
feature/process-bound-envs
Mar 13, 2026
Merged

Add process-bound Python environments#27
benoitc merged 5 commits intomainfrom
feature/process-bound-envs

Conversation

@benoitc
Copy link
Owner

@benoitc benoitc commented Mar 13, 2026

Summary

  • Each Erlang process gets its own Python globals/locals dict
  • Stored as NIF resource in process dictionary, auto-freed on exit
  • Worker mode only; subinterpreters keep full isolation

Changes

  • py_env_resource_t struct and create_local_env NIF
  • _with_env variants of context NIFs
  • py:get_local_env/0 using process dictionary
  • 4 new tests for isolation, main access, state persistence, cleanup

Each Erlang process gets its own Python globals/locals dict stored as a
NIF resource in the process dictionary. When the process exits, the
resource destructor frees the Python dicts.

Works in both worker and subinterpreter modes.

- Add py_env_resource_t and create_local_env NIF
- Add _with_env variants of context_exec/eval/call
- Add get_local_env/0 to py.erl using process dictionary
- Update py_context to pass EnvRef to NIFs
- Add tests for isolation, __main__ access, persistence, cleanup
@benoitc benoitc force-pushed the feature/process-bound-envs branch from 01e578c to 4ed57f8 Compare March 13, 2026 11:17
benoitc added 4 commits March 13, 2026 13:17
Subinterpreters have separate memory allocators. Env dicts must
be created inside the target interpreter to avoid free() errors.
- Fix get_interp_id to return {ok, InterpId} tuple (was returning raw int)
- Add handler for create_local_env during callback wait
- Add handlers for EnvRef variants of call, eval, exec messages
- Add schedule marker handling to nif_context_eval_with_env

These messages are sent during reentrant callbacks when Python calls
back to Erlang which then calls py:call/eval/exec.
@benoitc benoitc merged commit 0663009 into main Mar 13, 2026
11 checks passed
@benoitc benoitc deleted the feature/process-bound-envs branch March 13, 2026 14:15
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