I wrote a vibe.d
web-UI for clang-format
, when presented with this input while using LLVM style, the server hangs.
The code for handling POST:
void post(string style, string code)
{
import std.algorithm;
import std.file;
import std.conv;
import std.process;
auto pipes = pipeProcess(["clang-format", "-style="~style], Redirect.stdout | Redirect.stdin);
scope(exit) wait(pipes.pid);
pipes.stdin.write(code);
pipes.stdin.close;
pipes.pid.wait;
code = pipes.stdout.byLine.joiner.to!string;
string selectedStyle = style;
render!("index.dt", styles, code, selectedStyle);
}
This probably shouldn't be done in a blocking way, but I am at a loss how to do it asynchronously. I have tried wrapping the contents of the function in runTask
, but I couldn't figure out a way to call it correctly.
How can I make it reliable?
You are probably writing too much data to the program's stdin
without reading its stdout
. Since the pipe buffer size is limited, this causes the executed program to block on writing to its stdout
, which in turn causes your program to block on writing to its stdin
.
The solution is to read data as you are writing it. A simple way to do it is to create a second thread which reads the data, while the main thread writes it.