[erlang-questions] Re: how do I get escript to emit all output before exiting?

Ulf Wiger ulf.wiger@REDACTED
Thu Apr 7 19:24:21 CEST 2011


On 7 Apr 2011, at 14:49, Matthias Lang wrote:

>  - calling init:stop(), receive after infinity -> ok end, also loses
>      output, but even less often (Håkan's idea)


Init:stop() will in fact attempt an orderly shutdown first. Once all applications have been properly stopped, init will proceed to find all remaining processes and call exit(P, kill). This means your receive after infinity will only last you until that kill arrives.

This should mean that if you are in an application tree, you will be given the chance to complete (e.g. delaying the shutdown by trapping exits), However, if you are outside the application tree, you are given no such quarter.

Now, in an escript, the group_leader is the user server, which is a process in 'kernel', which actually does trap exits. This means that the problem isn't that init kills the group leader before it's finished, and the io:fwrite/2 request returns only after the group leader has pushed the output to the port. Rather, I suspect that the group leader pushes the output to the io driver and then happily terminates, in fact - looking at the code - since the user_drv port dies*.

What I suspect is actually happening is that your output is in the port buffer, which isn't explicitly flushed during halt or shutdown. With a sufficient timer:sleep(), it will be, but no other amount of trickery will suffice.

BR,
Ulf

* The code is quite intricate, so I may have missed something.

Ulf Wiger, CTO, Erlang Solutions, Ltd.
http://erlang-solutions.com






More information about the erlang-questions mailing list