<div dir="ltr">Hi,<br><br>I got a series of troubles with gen_tcp all eventually resulting in crashes. I tested this under Windows XP and with R12B-3 as well as R12B-4. Under Linux it seems to work but I am not perfectly sure since the crash happens sporadically and seems to be timing related.<br>
<br>The two problems below lead me to a couple of questions:<br>a) What is the real cause? Is it the socket error enfile? Do both problems have the same root cause?<br>b) Is there a bug in Erlang? I guess this should not lead to a crash.<br>
c) How do you avoid this problem on systems you do not control yourself?<br><br><br>Problem #1:<br>###########<br><br>Just compile the following code and run it with sasl enabled and the following command:<br> tcp_test:test(1000).<br>
and - yes - without anything listening on port 2222. And sometimes you have to try two times!<br><br>-------------------------- start: tcp_test.erl --------------------------<br>-module(tcp_test).<br><br>-export([test/1, test_con/0]).<br>
<br>-define(DEF_PORT, 2222).<br>-define(DEF_IP, {127,0,0,1}).<br><br>test(0) -> ok;<br>test(HowManyProcs) -><br> spawn(?MODULE, test_con, []),<br> test(HowManyProcs-1).<br><br>test_con() -><br> {ok,S} = gen_tcp:connect(?DEF_IP, ?DEF_PORT,[]),<br>
gen_tcp:send(S,<<0,5,65,66,67,68,69>>),<br> receive<br> {tcp_closed, _Socket} -> ok;<br> _Msg -> gen_tcp:close(S)<br> after 500 -><br> gen_tcp:close(S)<br> end.<br>-------------------------- end: tcp_test.erl --------------------------<br>
<br>It just spawns a bunch of processes all trying to connect to a currently closed port and sending some garbage there. This is what happens:<br><br>-------------------------- start: log tcp_test.erl --------------------------<br>
=ERROR REPORT==== 12-Sep-2008::15:28:47 ===<br>Error in process <0.41.0> with exit value: {{badmatch,{error,econnrefused}},[{tcp_test,test_con,0}]}<br><br>[... a couple of them but usually between 1 and 20.]<br><br>
=ERROR REPORT==== 12-Sep-2008::15:28:47 ===<br>Error in process <0.103.0> with exit value: {{badmatch,{error,econnrefused}},[{tcp_test,test_con,0}]}<br><br><br>Crash dump was written to: erl_crash.dump<br>Inconsistent, why isnt io reported?<br>
<br>Abnormal termination<br>-------------------------- end: log tcp_test.erl --------------------------<br><br>It might have something to do with the socket error enfile 'file table overflow' but I guess it should not simply crash the emulator!?<br>
Searching google for 'Inconsistent, why isnt io reported?' just gives one hit to Erlang's source code.<br>I can provide the crash dump if needed. Just did not want to spam the whole list with big attachments.<br>
Spawning only 500 processes (tcp_test:test(500).) usually leads to a crash, spawning only 200 seems to work.<br><br><br>Problem #2:<br>###########<br><br>Now let's try the same with a server answering to port 2222: Just take the code from the trapexit tutorial 'Building a Non-blocking TCP server using OTP principles' <a href="http://trapexit.org/Building_a_Non-blocking_TCP_server_using_OTP_principles">http://trapexit.org/Building_a_Non-blocking_TCP_server_using_OTP_principles</a><br>
Start it first and then our test module in a different erlang node as described above. Now, usually the client survives (have seen crashes as well!) and the server crashes in a similar way. Sometimes it survives and in very rare cases you will see the following logs in the erlang server instance:<br>
<br>-------------------------- start: log server --------------------------<br>=ERROR REPORT==== 12-Sep-2008::12:58:56 ===<br>File operation error: system_limit. Function: get_cwd. Process: code_server.<br><br>=ERROR REPORT==== 12-Sep-2008::12:58:56 ===<br>
Error in async accept: {async_accept,"file table overflow"}.<br><br>=ERROR REPORT==== 12-Sep-2008::12:58:56 ===<br>** Generic server tcp_listener terminating <br>** Last message in was {inet_async,#Port<0.109>,1019,{ok,#Port<0.2141>}}<br>
** When Server state == {state,#Port<0.109>,1019,tcp_echo_fsm}<br>** Reason for termination == <br>** {async_accept,"file table overflow"}<br><br>[...]<br>-------------------------- end: log server --------------------------<br>
<br>Can anyone help? Thank you!<br><br>Regards,<br>Michael<br></div>