从 vibe.d 同步执行 std.process 有时静静地挂起服务器

Executing std.process synchronously from vibe.d sometimes silently hangs the server

我为 clang-format 写了一个 vibe.d web-UI,在使用 LLVM 风格时出现 this input 时,服务器挂起。

处理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);
}

这可能不应该以阻塞方式完成,但我不知道如何异步完成。我曾尝试将函数的内容包装在 runTask 中,但我无法找到正确调用它的方法。

如何让它可靠?

您可能在没有读取程序 stdout 的情况下向程序 stdin 写入过多数据。由于管道缓冲区大小有限,这会导致执行的程序在写入其 stdout 时阻塞,进而导致您的程序在写入其 stdin.

时阻塞

解决方案是边写边读数据。一个简单的方法是创建第二个线程读取数据,而主线程写入数据。