otp genserver and port program

View: New views
3 Messages — Rating Filter:   Alert me  

otp genserver and port program

by Vinubalaji Gopal-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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<0.0.0.1057>},^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 :(.

--
Vinu

In a world without fences who needs Gates?

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org


Re: otp genserver and port program

by Geoff Cant-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Vinubalaji Gopal <vinubalaji@...> 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<0.0.0.1057>},^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.

Cheers,
--
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.


________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org


Re: otp genserver and port program

by Vinubalaji Gopal-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Geoff,

> 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', _, _}).
>

Great that was the problem - I had a
handle_call.....
->
port_command(....)
Receive
  Data ->
%do a match on the data here

in the handle_call and this was what was matching the
{'$gen_server',... . I had no idea that the gen_server messages will
be matched by this receive. Thanks a lot for clarifying that.

--
Vinu

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org