[erlang-questions] open_port() slow on Windows

Daniel Goertzen daniel.goertzen@REDACTED
Wed Nov 9 21:00:59 CET 2011

I've figured the Windows open_port() delay problem and will document the
issue here in case anyone else trips on it.

The function create_child_process() in erts/ermulator/sys/win32/sys.c makes
the following call after the port process has been created:

WaitForInputIdle(piProcInfo.hProcess, 5000);

What this does is wait for the Windows message loop in the port process to
become active.  If the port process doesn't start a message loop right away
(or ever),  WaitForInputIdle(), and hence open_port() will just hang for up
to 5 sec.

A partial work-around for this problem is to have the port program do a
single iteration through the Windows message loop early in its life.  In my
Python/PyQt port program this was achieved with:

app = QtGui.QApplication(sys.argv)

Apparently the purpose of the WaitForInputIdle() call was to address a
memory leak problem in Windows NT 3.5 (
http://support.microsoft.com/kb/124121) .  This has been fixed in NT 3.5
and everything following, so it is probably time to do away with the
WaitForInputIdle() call so nobody else gets burnt by this issue.


On Tue, Nov 8, 2011 at 4:19 PM, Daniel Goertzen

> I am observing slow behavior for open_port() on Windows.  The port program
> itself starts up quickly, but open_port() itself hangs for several seconds
> before returning... long after the port program is up and running.
> Here is my instrumented Erlang code....
>   io:format("~p opening port~n",[now()]),
>   Port=open_port( {spawn_executable, Exe},
>     [
>       {cd, Dir},
>      {args, Args},
>      stream,
>      binary,
>      exit_status
>     ]),
>   io:format("~p done port~n",[now()]),
> My port program is a python PyQt program.  One of the first things I have
> it do is...
>   logfile = open("c:\\logfile.txt","w")
>   print(time.time(), "starting", file=logfile)
> The output of my Erlang code interspersed with the above starting message
> is....
> {1320,789477,830000} opening port    [werl.exe]
>   1320789478.968 starting                 [my_port_program.exe]
> {1320,789483,337000} done port        [werl.exe]
> Any idea why open_port() waits for so long before returning?
> Thanks,
> Dan.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111109/7cbd8ebc/attachment.htm>

More information about the erlang-questions mailing list