[erlang-questions] Using select in a Port

jm <>
Fri Oct 26 07:28:50 CEST 2007


Per Hedeland wrote:

> I can however be wrong from your process' point of view - e.g. if it
> expects to receive binaries in Erlang external term format, and
> optimistically does a binary_to_term/1 on whatever it gets, it will die
> with a badarg if your port program sends garbage - and the port will be
> closed *as a result of your process dying*. At least during debugging
> you may want to wrap catch and/or try around such things. And at least
> when running from the shell, you should get a message about the
> immediate cause of any "unnatural" death:
> 
> =ERROR REPORT==== 25-Oct-2007::22:30:11 ===
> Error in process <0.52.0> with exit value: {badarg,[{erlang,binary_to_term,[<<3 bytes>>]},{ll,loop,1}]}
> 
> 
> --Per

I put a simple try/catch around the loop/1 as you suggested

                 try loop(Port)
                   catch
                       T:Err -> io:format("~n**** catch : ~p:~p~n",
                                          [T, Err])
                   end

This did it. It spat out the error. A very dumb error, which when 
corrected rectified the problem. Given the amount of code I would have 
thought the error lay else where. I'd looked at that particular line of 
code a number of times and didn't see anything. Just goes to show. 
Anyway, I've added abit more code, fix a couple of other bugs, and 
removed all those fprintf() statements. The next chance I'll get to work 
on this after today won't be for another week or two at which time I'm 
hoping to be able to make an initial release.

Jeff.




More information about the erlang-questions mailing list