Search code examples
linuxdartbufferstdoutstdin

Dart don't buffer process stdout (tell process that it is running from a terminal)


I'm trying to run a binary file width Process.start in dart and redirect its output the stdout and pipe stdin to the process.

Here is my code:

import 'dart:io';
import 'dart:convert';

main() async {
  Process.start('./test.bin', []).then((p) {
    p.stdout.listen((bytes) {
      stdout.add(bytes);
      stdout.flush();
    });
    stdin.pipe(p.stdin);
  });
}

The problem is that it only flushes the process's stdout after the process terminates.

After digging around on the internet I found this: "This is caused by libc which by default puts stdout int buffered mode when it is not connected to a terminal."

How could I tell the process that it is running from a terminal?

My goal with this project is to have a terminal in a webapp that interacts with this process running on the backend, so it will be running on a terminal but the process is obviously unaware of that.


Solution

  • I'm answering my own question in case anyone finds this post.

    I ended up writing the backend in nodejs with the help of this library (https://www.npmjs.com/package/node-pty)

    But I found a similar library for dart as well (https://pub.dev/packages/pty)