How to know where a message is being sent from?

Recently I encountered a problem that a gen_server process would receive a message that should not have been received. The message has no information to tell where it was sent from, and the project code is complicated. I need to know a way to know where the message was sent from, so does anyone know how?

2 Likes

Maybe by tracing send and receive ?

Edbg examples look promising …

4 Likes

If you can narrow down the situation where the event happens, you could try to
turn on tracing for a short time
with erlang:trace/3 with the flag send, maybe also ‘receive’.
If you do this within a gen_server, you can filter the callbacks from tracer for the interesting messages.

2 Likes

thank for the link!

2 Likes

That’s a good idea, This method can find out msg from which process. I’ve based on GitHub - rabbitmq/looking_glass: An Erlang/Elixir/BEAM profiler tool write a wheel. It’s time to use it.

1 Like

Technically a message could be sent from anywhere (including ERTS itself, not even Erlang code).
So there is no reliable way to know the sender, unless sender adds that information into the message itself.

In addition to function call tracing (erlang/trace), there is also Erlang -- seq_trace feature, which may prove useful to understand the entire flow caused by a message. Just be careful with it in production, as it may eventually add the trace marker to all messages in your system.

4 Likes

Even if you turn on tracing you will see when a message arrives at a process but you will only see the message itself and not from where it came. For a simple experiment try starting observer and use it to trace messages to/from the process in which you are interested.

2 Likes

Thank you for your advice.

1 Like

I trace send events that contain the process that sent and received the message

2 Likes

To debug message handling in an Elixir/Erlang project, you can:

1) add metadata to the message, 2) enable process tracing, 3) add logging, or 4) use the debugger. Instrument your code and gather information to understand the issue.
2 Likes

That is basically the only way to do it.

When you trace messages being sent by a process then you can see to where it is sent but that is about it.

2 Likes

Very good advice, thank you!

1 Like