tags:

views:

166

answers:

4

When I start up a function within the erl shell, it works fine. When I try to invoke the same function with erl ... -s module function, it fails.

The line of code that eventually fails is:

start(Port) ->
    mochiweb_http:start([{port, Port}, {loop, fun dispatch_requests/1}]).

I'm positive that Port is set correctly. My error message is:

=CRASH REPORT==== 17-Jan-2010::00:21:09 ===
  crasher:
    initial call: mochiweb_socket_server:acceptor_loop/1
    pid: <0.65.0>
    registered_name: []
    exception exit: {error,closed}
      in function  mochiweb_socket_server:acceptor_loop/1
    ancestors: [mochiweb_http,<0.1.0>]
    messages: []
    links: []
    dictionary: []
    trap_exit: false
    status: running
    heap_size: 377
    stack_size: 24
    reductions: 93
  neighbours:

I tried the debugger and it lets me step through right up until the line of code above is given. After I pass that, it gives me this crash report.

Any help is greatly appreciated.

A: 

when using -s, the arguments are collected into a list, so the port would actually be enclosed in a list. you can check both cases (list or int) with a wrapper function (like start([Port])).

jspcal
I actually had a wrapper around that. I'm actually calling start_all/0 which calls start. My command is: erl -boot start_sasl -s server_util start_all
Eli
+1  A: 

Hm, I think that should work. Are all modules compiled with the same compiler version? IIRC there might be weird errors on the socket level if not. BTW, you might call your entry point function start which is the default for -s.

andi5: how do I check the compiler version on each?
Eli
proplists:get_value(version, Mod:module_info(compile)).should show it for the module Mod. Using lists:foreach/2 and code:all_loaded() you can iterate over the list of all loaded modules.
Everything appears to be either undefined or 4.6.3. Check out http://pastie.org/782757. Also, this fails too, which uses start as the entry point: http://pastie.org/782752
Eli
There is nothing about mochiweb_http. Are you sure you tried to start your server before checking the compiler versions?
Ah yeah, that'd help. I reran it after loading that up and it's still all 4.6.3 or undefined.
Eli
Eli
Eli: You are probably right about the source code location throwing the error. If it is not the compiler version, does adding an infinite receive loop at the end of start/0 change anything?
andi5: for some reason, this works: http://pastie.org/791300 ! Any idea why this works but what I did before didn't?
Eli
Ok, I am not an expert with mochiweb, but this sounds like your loop function is not correct then. http://www.codeweblog.com/mochiweb-design/ might have better information. Please note that x is an atom, whereas X is either a bound or unbound variable.
A: 

When you use -s to run Erlang functions, arguments are put into a list of atoms. When you use -run to run Erlang functions, arguments are put into a list of strings.

If you need an integer value to pass on, you will need to do the proper conversions. If you want to cover all cases, something like this could help:

start([Port]) when is_atom(Port) ->
    start([atom_to_list(Port)]);
start([Port]) when is_list(Port) ->
    start(list_to_integer(Port));
start(Port) when is_integer(Port) ->
    mochiweb_http:start([{port, Port}, {loop, fun dispatch_requests/1}]).

Consult the man page for erl ("erl -man erl") for details.

ndim
Thanks for the details. However, I should have edited my question, because I'm actually calling start/0 which calls start/1 with my default port. I started removing things and eventually simplified my problem to this: http://pastie.org/782752 -- does this work for you? It fails for me on OS X. Any ideas? Much appreciated.
Eli
Well, the 'loop' fun is not a loop. I am not familiar with mochiweb, so I cannot tell you that this is your issue. Anyway, that paste looks like a completely separate issue to me, like one of proper mochiweb API usage, not of "erl -s" startup. Probably better open a new question with that new problem.
ndim
ndim: but it works fine when I invoke it from within the erl console. Additionally, fixing it to a real loop doesn't help: http://pastie.org/788592. The problem is that gen_tcp:listen is returning {error, closed} for some reason.
Eli
A: 

Alternatively you can try eval option

erl -eval 'module:start(9090).'

Yogish Baliga