[erlang-questions] otp genserver and port program

Geoff Cant nem@REDACTED
Fri Oct 23 12:46:52 CEST 2009

Vinubalaji Gopal <vinubalaji@REDACTED> writes:

> Hi all,
> I wrote a port program based on the design principles of
> http://www.trapexit.org/Writing_an_Erlang_Port_using_OTP_Principles
> The only difference is my program is a linked in driver and the linked
> in driver connects to a C program. The C program returns a tuple
> response. It works fine if only one client connects to the server (I
> get a proper response). But in case multiple client connects to the
> server I get a bad match error and the gen_server terminates :(. The
> debug message is not that useful and googling didn't give me anything
> useful. I am using port_command to send the message to the linked in
> driver (which is part of the handle_call). Is there any way to debug a
> gen_server crashing (I don't get anything other than the badmatch
> which I don't understand) and would gen_server handle_call not handle
> multiple calls that are received simultaneously? It works fine if I
> remove the port_command and just return random tuples.
> The error I get looks something like this:
> {{badmatch,^M
>                {'$gen_call',^M
>                    {<0.189.0>,#Ref<>},^M
> {my_function,^M
>                        Msg}}}
> I would really appreciate any kind of help or pointers - have been
> trying to debug this for a while and can't get much information :(.

Hi there, I'm going to attempt a little psychic debugging, so don't be
surprised if I get it all wrong. :)

I'm guessing you're receiving a message somewhere in your code that you
expect to come from the port or the driver. I'm also guessing that
your receive statement has a pattern that will match any message
(certainly enough to match {'$gen_call', _, _}).

You're probably sending a message to the driver and then waiting for the
response, only the first message in the gen_server message queue is
another call request and your code mis-interprets this as the reply from
the driver. Matching the first message from the queue against something
else would give you this badmatch error.

If I've got this right, you probably just need to change your receive
statement to match only the right messages to fix this problem. 

Geoff Cant
ps: if the above was nonsense, then we probably need to see a bit more
of your code - particularly the handle_call function from your
gen_server and the bit that does a send/receive to the linked in driver.

More information about the erlang-questions mailing list