AtomVM icon indicating copy to clipboard operation
AtomVM copied to clipboard

sending message to a registered process via it's name does not work.

Open elsbiet opened this issue 6 years ago • 4 comments

running the pgm

-module(pr). -export([start/0, ping/1, pong/0]). ping(0) -> pong ! finished, erlang:display("ping finished");

ping(N) -> erlang:display("ping started"), pong ! {ping, self()}, erlang:display("ping sent"), receive pong -> erlang:display("Ping received pong") end, ping(N - 1).

pong() -> erlang:display("pong started"), receive finished -> erlang:display("Pong finished"); {ping, Ping_PID} -> erlang:display("Pong received ping"), Ping_PID ! pong, pong() end.

start() -> register(pong, spawn(pr, pong, [])), spawn(pr, ping, [3]).

results in: "pong started" "ping started" term is not a pid: a4b "ping sent" Hang detected

after replacing

pong ! Msg,

by

P = whereis(pong), P ! Msg,

the expected output "pong started" "ping started" "ping sent" "Pong received ping" "pong started" "Ping received pong" "ping started" "ping sent" "Pong received ping" "pong started" "Ping received pong" "ping started" "ping sent" "Pong received ping" "pong started" "Ping received pong" "ping finished" "Pong finished" Return value: <0.3.0>

is displayed.

elsbiet avatar May 16 '19 09:05 elsbiet

Update: this bug is still here (tested on 1939abfaad3faf64f982eee39d5db5bd146f279e)

bettio avatar Sep 04 '19 12:09 bettio

still unresolved

elsbiet avatar Apr 28 '21 07:04 elsbiet

This issue is still open, if you consider the send operator (!), which the original poster used. #631 addresses the Erlang:send nif, not the OP_SEND opcode.

Here is a simpler example to reproduce the error:

-module(test).

-export([start/0]).

start() ->
    Pid = spawn_opt(fun loop/0, []),
    register(foo, Pid),
    foo ! {ping, self()},
    receive
        pong ->
            ok
    after 1000 ->
        timeout
    end.

loop() ->
    receive
        {ping, Pid} ->
            Pid ! pong,
            loop()
    end.

On OTP:

Eshell V12.3.2.15  (abort with ^G)
1> test:start().
ok

On AtomVM:

> atomvm test.beam
Unsupported line_ref tag: 0
Return value: timeout

I believe the issue may relate to the current implementation of the send opcode:

            case OP_SEND: {
                #ifdef IMPL_CODE_LOADER
                    TRACE("send/0\n");
                #endif

                #ifdef IMPL_EXECUTE_LOOP
                    int local_process_id = term_to_local_process_id(x_regs[0]);
                    TRACE("send/0 target_pid=%i\n", local_process_id);
                    TRACE_SEND(ctx, x_regs[0], x_regs[1]);
                    globalcontext_send_message(ctx->global, local_process_id, x_regs[1]);

                    x_regs[0] = x_regs[1];
                #endif
                break;
            }

As you can see, we are assuming the term in the 0th x-register is a Pid, when in fact it could be an atom.

fadushin avatar Feb 18 '24 03:02 fadushin

The oversight of missing OP_SEND opcode is finally addressed in #1047.

UncleGrumpy avatar Feb 18 '24 03:02 UncleGrumpy