Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## Unreleased

### Fixed

- Native `Channel.async_receive()` no longer drops a queued `None` or a
delivery whose receiver is cancelled after notification.

## 5.0.1 (2026-09-22)

### Fixed
Expand Down
11 changes: 5 additions & 6 deletions c_src/py_callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -2995,12 +2995,6 @@ static PyObject *erlang_channel_wait_impl(PyObject *self, PyObject *args) {

pthread_mutex_lock(&channel->mutex);

/* Check if closed */
if (channel->closed) {
pthread_mutex_unlock(&channel->mutex);
return Py_BuildValue("(ss)", "error", "closed");
}

/* Check if waiter already exists */
if (channel->has_waiter || channel->has_sync_waiter) {
pthread_mutex_unlock(&channel->mutex);
Expand Down Expand Up @@ -3058,6 +3052,11 @@ static PyObject *erlang_channel_wait_impl(PyObject *self, PyObject *args) {
}
}

if (channel->closed) {
pthread_mutex_unlock(&channel->mutex);
return Py_BuildValue("(ss)", "error", "closed");
}

/* No data - register waiter */
enif_keep_resource(loop);
channel->waiter_loop = loop;
Expand Down
2 changes: 1 addition & 1 deletion docs/code-map.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ exercised by suites). Guides are in `docs/`, suites in `test/`. Start with
| `py_event_worker` | One process per running loop receiving `enif_select` readiness and timers | live | event_loop_architecture | `py_event_loop_SUITE`, `py_fd_ops_SUITE` |
| `py_event_worker_sup`, `py_event_worker_registry` | Supervisor and name registry of the event workers | live | event_loop_architecture | `py_event_loop_SUITE` |
| `py_reactor_context` | FD-owning context for the protocol-based reactor | live | reactor | `py_reactor_SUITE` |
| `py_channel`, `py_byte_channel` | Term and byte queues between Erlang and Python coroutines (NIF resources) | live | channel | `py_channel_SUITE`, `py_byte_channel_SUITE` |
| `py_channel`, `py_byte_channel` | Term and byte queues between Erlang and Python coroutines (NIF resources); native term receives dequeue in the receiving task, not the readiness callback | live | channel | `py_channel_SUITE`, `py_byte_channel_SUITE` |
| `py_buffer` | Native streaming input buffer; shared variant delegates to `py_shm` | live | buffer, isolated | `py_buffer_SUITE`, `py_isolated_buffer_SUITE` |
| `py_shm` | Shared memory regions over iommap and the ring behind shared buffers | live | isolated | `py_isolated_shm_SUITE` |
| `py_import` | Registry of imports and `sys.path` entries applied to every interpreter | live | imports | `py_import_SUITE` |
Expand Down
74 changes: 19 additions & 55 deletions priv/_erlang_impl/_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ async def async_receive(self):
"""
import asyncio

loop = asyncio.get_running_loop()
if hasattr(loop, '_loop_capsule') and hasattr(loop, '_timers'):
return await self._async_receive_event_driven(loop)

# Try non-blocking first (direct NIF - fast)
result = self.try_receive()
if result is not None:
Expand All @@ -173,78 +177,38 @@ async def async_receive(self):
if self._is_closed():
raise ChannelClosed("Channel has been closed")

# Get the running event loop
loop = asyncio.get_running_loop()

# Check if this is an ErlangEventLoop with native dispatch support
if hasattr(loop, '_loop_capsule') and hasattr(loop, '_timers'):
return await self._async_receive_event_driven(loop)
else:
# Fallback for non-Erlang event loops: use polling
return await self._async_receive_polling()
return await self._async_receive_polling()

async def _async_receive_event_driven(self, loop):
"""Event-driven async receive using channel waiter mechanism.

Registers with the channel and waits for EVENT_TYPE_TIMER dispatch
when data arrives. No polling required.
"""
import asyncio
from asyncio import events
import erlang

future = loop.create_future()
callback_id = id(future)
while True:
future = loop.create_future()
callback_id = id(future)

# Callback that fires when channel has data
def on_channel_ready():
if future.done():
return
try:
data = self.try_receive()
if data is not None:
future.set_result(data)
elif self._is_closed():
future.set_exception(ChannelClosed("Channel closed"))
else:
# Data consumed by race - set None to signal retry
def on_channel_ready(future=future):
if not future.done():
future.set_result(None)
except Exception as e:
future.set_exception(e)

# Create handle and register in timer dispatch system
# Only use _timers dict - don't touch _handle_to_callback_id (for timer cancellation only)
handle = events.Handle(on_channel_ready, (), loop)
loop._timers[callback_id] = handle

try:
# Register waiter with channel (direct C call, no Erlang overhead)
result = erlang._channel_wait(self._ref, callback_id, loop._loop_capsule)

if isinstance(result, tuple):
if result[0] == 'ok':
# Data already available - clean up and return
loop._timers.pop(callback_id, None)
return result[1]
elif result[0] == 'error':
loop._timers.pop(callback_id, None)
loop._timers[callback_id] = events.Handle(on_channel_ready, (), loop)
try:
result = erlang._channel_wait(self._ref, callback_id, loop._loop_capsule)
if isinstance(result, tuple):
if result[0] == 'ok':
return result[1]
if result[1] == 'closed':
raise ChannelClosed("Channel closed")
raise RuntimeError(f"Channel wait failed: {result[1]}")

# Waiter registered - await notification
try:
data = await future
# Handle race condition (data was None)
if data is None:
# Retry with polling fallback for this edge case
return await self._async_receive_polling()
return data
except asyncio.CancelledError:
await future
finally:
loop._timers.pop(callback_id, None)
erlang._channel_cancel_wait(self._ref, callback_id)
raise
finally:
loop._timers.pop(callback_id, None)

async def _async_receive_polling(self):
"""Fallback async receive for non-Erlang event loops.
Expand Down
2 changes: 1 addition & 1 deletion test/coverage_audit.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ suite is visible. `scripts/check_code_map.sh` requires a row per module.
| `erlang.spawn_task()` | asyncio.md | `py_async_task_SUITE` |
| `erlang.sleep()` | asyncio.md | `py_erlang_sleep_SUITE` |
| `erlang.SharedDict` | shared-dict.md | `py_SUITE` (`test_shared_dict_*`) |
| `erlang.Channel`, `erlang.ByteChannel` | channel.md, buffer.md | `py_channel_SUITE`, `py_byte_channel_SUITE` |
| `erlang.Channel`, `erlang.ByteChannel` | channel.md, buffer.md | `py_channel_SUITE` (including native None payloads, readiness cancellation and isolated rejection), `py_byte_channel_SUITE` |
| `erlang.reactor.Protocol` and friends | reactor.md | `py_reactor_SUITE` |
| `erlang.state_set / state_get / state_delete / state_keys / state_incr / state_decr` (registered callbacks) | README, getting-started.md | `py_SUITE` (`test_shared_state`) |

Expand Down
81 changes: 81 additions & 0 deletions test/py_channel_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
python_try_receive_test/1,
python_iterate_test/1,
async_receive_immediate_test/1,
native_none_payload_test/1,
native_cancel_ready_test/1,
isolated_channel_rejected_test/1,
async_receive_wait_test/1,
async_iteration_test/1,
async_closed_channel_test/1,
Expand Down Expand Up @@ -63,6 +66,9 @@ all() -> [
python_try_receive_test,
python_iterate_test,
async_receive_immediate_test,
native_none_payload_test,
native_cancel_ready_test,
isolated_channel_rejected_test,
async_receive_wait_test,
async_iteration_test,
async_closed_channel_test,
Expand Down Expand Up @@ -99,6 +105,81 @@ end_per_suite(_Config) ->
init_per_testcase(_TestCase, Config) ->
Config.

native_none_payload_test(_Config) ->
{ok, Ch} = py_channel:new(),
ok = py_channel:send(Ch, none),
ok = py:register_function(channel_regression_none, fun([]) ->
py_channel:send(Ch, none)
end),
Ctx = py:context(1),
ok = py:exec(Ctx, <<"
import asyncio
import erlang
async def receive_none(ref):
value = await asyncio.wait_for(erlang.Channel(ref).async_receive(), 0.2)
assert value is None
receiver = asyncio.create_task(erlang.Channel(ref).async_receive())
await asyncio.sleep(0)
erlang.call('channel_regression_none')
assert await asyncio.wait_for(receiver, 0.2) is None
return True
def run_receive_none(ref):
return erlang.run(receive_none(ref))
">>),
try
{ok, true} = py:call(Ctx, '__main__', run_receive_none, [Ch])
after
py:unregister_function(channel_regression_none),
py_channel:close(Ch)
end.

native_cancel_ready_test(_Config) ->
{ok, Ch} = py_channel:new(),
ok = py:register_function(channel_regression_send, fun([]) ->
py_channel:send(Ch, 42)
end),
Ctx = py:context(1),
ok = py:exec(Ctx, <<"
import asyncio
import erlang
async def cancel_ready(ref):
loop = asyncio.get_running_loop()
channel = erlang.Channel(ref)
before = set(loop._timers)
receiver = asyncio.create_task(channel.async_receive())
await asyncio.sleep(0)
callbacks = set(loop._timers) - before
assert len(callbacks) == 1
ready = loop._timers[callbacks.pop()]
erlang.call('channel_regression_send')
ready._run()
receiver.cancel()
try:
await receiver
except asyncio.CancelledError:
pass
assert channel.try_receive() == 42
return True
def run_cancel_ready(ref):
return erlang.run(cancel_ready(ref))
">>),
try
{ok, true} = py:call(Ctx, '__main__', run_cancel_ready, [Ch])
after
py:unregister_function(channel_regression_send),
py_channel:close(Ch)
end.

isolated_channel_rejected_test(_Config) ->
{ok, Ctx} = py_context:new(#{mode => isolated}),
try
{ok, true} = py_context:eval(Ctx, <<"
exec('import erlang\\ntry:\\n erlang.Channel(None)\\nexcept RuntimeError:\\n rejected = True\\nelse:\\n rejected = False') or rejected
">>)
after
py_context:destroy(Ctx)
end.

end_per_testcase(_TestCase, _Config) ->
ok.

Expand Down
Loading