Mochiweb immediate SHUTDOWN {error, closed} from Windows Command Line

52 views
Skip to first unread message

Lynton Grice

unread,
Aug 22, 2009, 2:54:11 AM8/22/09
to MochiWeb, lynton...@logosworld.com
Hi there,

I wonder if someone can help me on this...I have tried for a couple
days now and am about to pull out my hair ;-)

First off, if I run my Mochiweb server from the normal Erlang console
it works perfectly....

So if I go:

karoona_http:start(8001).

It will load up and I can access it from the browser like follows:
http://localhost:8001/ping

So that is fine....but if I try start the mochiweb server like follows
from the windows command line:

erl -pa ./ -boot start_sasl -sasl errlog_type error -run karoona_http
start 8001

I ALWAYS get this {error, closed} error:

=CRASH REPORT==== 22-Aug-2009::08:44:33 ===
crasher:
initial call: mochiweb_socket_server:acceptor_loop/1
pid: <0.52.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: 233
stack_size: 24
reductions: 85
neighbours:

I can I can see that it is dying in the following code in
“mochiweb_server_socket.erl”

acceptor_loop({Server, Listen, Loop}) ->
case catch gen_tcp:accept(Listen) of
{ok, Socket} ->
gen_server:cast(Server, {accepted, self()}),
call_loop(Loop, Socket);
{error, closed} ->
exit({error, closed});

NOTE: You may ask why am I doing this command above? Well I want to
install this Mochiweb HTTP server as a windows service using ERLSRV
and need the command above to work first. I can get the service
installed...and it seems to start up BUT then dies IMMEDIATELY.

To prove that Mochiweb does in fact start up I used LOG4ERL to check
it out....

Here is a code snippet:

start(Port) ->
application:start(log4erl),
log4erl:add_file_appender(file, {"./", "log", {size, 100000}, 4,
"txt", debug}),
log4erl:debug("Karoona starting...ready to Rock n Roll!!"),

case mochiweb_http:start([{loop, fun handle_requests/1}, {port,
8001}]) of
{ok, MochiPid} ->
log4erl:debug("Mochiweb started...");
{error, Reason} ->
log4erl:debug("Mochiweb died, reason:~s...", [Reason])
end.

And sure enough, when I start the server I can see the following in
the log file:

[debug] Karoona starting...ready to Rock n Roll!!
[debug] Mochiweb started...

BUT then is closes immediately...

Do you have any idea why it would shut down the connection immediately
once it starts up?

Any help would be HUGELY appreciated..;-)

BTW: When I install the code as a Windows Service I get pretty much
EXACTLY the same error.....and not sure if the log I get for the
Windows Service will help at all?

{"init terminating in do_boot",{undef,[{karoona_http,start,[['8001']]},
{init,start_it,1},{init,start_em,1}]}}

Thanks again ;-)

Lynton



Geoff Cant

unread,
Aug 22, 2009, 7:30:26 AM8/22/09
to moch...@googlegroups.com
Lynton Grice <lynto...@gmail.com> writes:

> Hi there,
>
> I wonder if someone can help me on this...I have tried for a couple
> days now and am about to pull out my hair ;-)
>

Save your hair, this one's fairly easy to fix :)

>
> erl -pa ./ -boot start_sasl -sasl errlog_type error -run karoona_http
> start 8001

The -run and -s arguments take a module and a function and collect the
following arguments into a single list, so "-run karoona_http start
8001" is not karoona_http:start(8001) but actually
karoona_http:start([8001]).

The fix is pretty simple, modify
start(Port) ->
...
to

start([Port]) -> start(Port);
start(Port) when is_integer(Port) ->


> application:start(log4erl),
> log4erl:add_file_appender(file, {"./", "log", {size, 100000}, 4,
> "txt", debug}),
> log4erl:debug("Karoona starting...ready to Rock n Roll!!"),
>
> case mochiweb_http:start([{loop, fun handle_requests/1}, {port,
> 8001}]) of
> {ok, MochiPid} ->
> log4erl:debug("Mochiweb started...");
> {error, Reason} ->
> log4erl:debug("Mochiweb died, reason:~s...", [Reason])
> end.

OTP has printed the error message for you here - you can see the nested
argument list:


> {"init terminating in do_boot",{undef,[{karoona_http,start,[['8001']]},

Cheers,
--
Geoff Cant

Lynton Grice

unread,
Aug 22, 2009, 10:42:56 AM8/22/09
to MochiWeb
Hi Geoff,

I see the Port was actually seen as an ATOM.....so I changed the code
to the following:

NOTE: the added "Port2 = erlang:list_to_integer(erlang:atom_to_list
(Port)),"


-export([start/1, stop/0, ping/1]).

start([Port]) ->
application:start(log4erl),
log4erl:add_file_appender(file, {"./", "log", {size, 100000}, 4,
"txt", debug}),
Port2 = erlang:list_to_integer(erlang:atom_to_list(Port)),
start(Port2);

start(Port) when is_integer(Port) ->
log4erl:debug("Karoona starting...ready to Rock n Roll!!"),

case mochiweb_http:start([{loop, fun handle_requests/1}, {port,
Port}]) of
{ok, MochiPid} ->
log4erl:debug("Mochiweb started...");
{error, Reason} ->
log4erl:debug("Mochiweb died, reason:~s...", [Reason])
end.

I ran the above with the following command:

erl -pa ./ -s karoona_http start 8001

But I still cannot access it even though the my custom log file says:

[debug] Karoona starting...ready to Rock n Roll!!
[debug] Mochiweb started...

If I change the command to:

erl -pa ./ -boot start_sasl errlog_type error -s karoona_http start
8001

I still get the same error ;-(

=CRASH REPORT==== 22-Aug-2009::16:40:57 ===
crasher:
initial call: mochiweb_socket_server:acceptor_loop/1
pid: <0.52.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: 233
stack_size: 24
reductions: 85
neighbours:


Any other ideas on my mochiweb cannot stay up when started in this
fashion from the windows command line?

Thanks again

Lynton


On Aug 22, 1:30 pm, Geoff Cant <n...@erlang.geek.nz> wrote:

Lynton Grice

unread,
Aug 22, 2009, 9:51:47 AM8/22/09
to MochiWeb
Hi Geoff,

Thanks for the response, much appreciated ;-)

I have changed my code to the following:

-export([start/1, stop/0, ping/1]).

start([Port]) ->
start(Port);

start(Port) when is_integer(Port) ->
application:start(log4erl),
log4erl:add_file_appender(file, {"./", "log", {size, 100000}, 4,
"txt", debug}),
log4erl:debug("Karoona starting...ready to Rock n Roll!!"),

case mochiweb_http:start([{loop, fun handle_requests/1}, {port,
Port}]) of
{ok, MochiPid} ->
log4erl:debug("Mochiweb started...");
{error, Reason} ->
log4erl:debug("Mochiweb died, reason:~s...", [Reason])
end.

But when I run this from the Windows command line like follows:

erl -pa ./ -s karoona_http start 8001

I get the following error:

{"init terminating in do_boot",{function_clause,[{karoona_http,start,
['8001']},{init,start_it,1},{init,start_em,1}]}}

Is it something to do with 8001 being seen as a string or something?

Any ideas?

Thanks for the help thus far, much appreciated ;-)

Lynton



On Aug 22, 1:30 pm, Geoff Cant <n...@erlang.geek.nz> wrote:

Geoff Cant

unread,
Aug 23, 2009, 7:43:36 AM8/23/09
to moch...@googlegroups.com
Lynton Grice <lynto...@gmail.com> writes:

> Hi Geoff,
>
> I see the Port was actually seen as an ATOM.....so I changed the code
> to the following:

That's really quite odd - I don't know why that's happening, but you are
correct from the error I saw.

> NOTE: the added "Port2 = erlang:list_to_integer(erlang:atom_to_list
> (Port)),"

The error leading to {error, closed} is a bit subtle.

> -export([start/1, stop/0, ping/1]).

To process -run or -s arguments, erlang spawns a process to evaluate the
M:F(A) you pass on the command line.

> start([Port]) ->
> application:start(log4erl),
> log4erl:add_file_appender(file, {"./", "log", {size, 100000}, 4,
> "txt", debug}),
> Port2 = erlang:list_to_integer(erlang:atom_to_list(Port)),
> start(Port2);
>
> start(Port) when is_integer(Port) ->
> log4erl:debug("Karoona starting...ready to Rock n Roll!!"),
>
> case mochiweb_http:start([{loop, fun handle_requests/1}, {port,
> Port}]) of
> {ok, MochiPid} ->
> log4erl:debug("Mochiweb started...");
> {error, Reason} ->
> log4erl:debug("Mochiweb died, reason:~s...", [Reason])
> end.

> =CRASH REPORT==== 22-Aug-2009::16:40:57 ===


> crasher:
> initial call: mochiweb_socket_server:acceptor_loop/1
> pid: <0.52.0>
> registered_name: []
> exception exit: {error,closed}
> in function mochiweb_socket_server:acceptor_loop/1
> ancestors: [mochiweb_http,<0.1.0>]

The error {error, closed} must come from a mochiweb socket acceptor
process calling gen_tcp:accept on the listen socket opened by the
mochiweb_socket_server process. The socket being closed when accept is
called would produce this error.

Maybe the process evalutating -run/-s (i.e. executing
karoona_http:start) exits which causes the socket_server and the listen
socket to exit, the acceptor processes survive, but the socket dies with
the socket_server and so you get {error, closed}

I'm not sure about this theory as the code for the
mochiweb_socket_server process traps exits, which should isolate the
listen socket from the socket server's parent exiting.

Does your code behave differently if you spawn a supervisor with one
child - use mochiweb_http:start(YourOptions) as the start function? In
this case the parent of the mochiweb_socket_server will be the
supervisor, which will survive past the -run/-s option processing phase
of startup.

If the above does work, then there's a bug somewhere or an API
assumption we're missing.

Cheers,
--
Geoff Cant
ps: resent due to mx weirdness.

Lynton Grice

unread,
Aug 23, 2009, 10:20:51 AM8/23/09
to MochiWeb
Hi Geoff,

Thanks again for the reply ;-) I am sensing there is a bug somewhere
in Mochiweb as in my mind this behaviour should not happen.

I will try the supervisor approach but don't think it will help too
much....

I will let you know what happens, I just can't believe a simple
statement like:

erl -pa ./ -s karoona_http start 8001

Does not work and closes the socket etc immediately....;-(

I will keep trying....

Thanks again

Lynton

On Aug 23, 1:43 pm, Geoff Cant <n...@erlang.geek.nz> wrote:
> ps: resent due to mx weirdness.- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages