I am writing a C# application that runs user code over the internet. The user code is sent to the application over an HTTP post request, and it is handled on the server (either compiled, then executed, or executed directly for languages such as python, lua, php). I am using the following code to read the output of the process that runs the user's code (either the compiled binary, or the interpreter):
Thread readThread = new Thread(() =>
{
char[] buffer = new char[Console.BufferWidth];
int read = 1;
while (read > 0)
{
read = p.StandardOutput.Read(buffer, 0, buffer.Length);
string data = read > 0 ? new string(buffer, 0, read) : null;
if (data != null)
env.ClientSock.Send(data);
Thread.Sleep(10);
}
});
readThread.Start();
Where p
is the process running the user's code, and env.ClientSock
is the web socket over which the output is sent to the user. The user can write to the process' standard input stream over this same web socket.
This works absolutely fine for most languages the application supports (C#, VB, C++, Python, PHP, Java). However, for C and Lua, if the user's code involves reading from the standard input stream, this read operations blocks before any output is given.
For example, the following C program:
#include <stdio.h>
int main(){
printf("Hello, World!");
int i;
scanf("%d", &i);
return 0;
}
Will not print anything until the user gives some input. For C, I am using the MSVC compiler that comes with Visual Studio. The same issue occurs when running Lua code:
io.write("Hello World!")
io.read()
Nothing is printed until the user enters some input. I cannot figure out why this is happening, and I would like my application to properly support C and Lua. This issue was also present using Python 2.7, although I overlooked this as Python 3 works as expected.
Why is this happening? How can I fix it?