[0.17.68] Server started from cmd has broken input stream (Windows)

Things that we don't consider worth fixing at this moment.
Post Reply
Hornwitser
Fast Inserter
Fast Inserter
Posts: 205
Joined: Fri Oct 05, 2018 4:34 pm
Contact:

[0.17.68] Server started from cmd has broken input stream (Windows)

Post by Hornwitser »

When I start a Factorio server through cmd with a command like "bin\x64\factorio.exe --start-server save.zip" on Windows 7 it imminently signals completion and returns to the shell, but keeps the stdin/out streams open. This results in input written to the terminal alternating between being sent to cmd and to the Factorio process (see attached image.)
Attachments
bad-input.png
bad-input.png (60.18 KiB) Viewed 3518 times

posila
Factorio Staff
Factorio Staff
Posts: 5201
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

Re: [0.17.68] Server started from cmd has broken input stream (Windows)

Post by posila »

you need to start it with

Code: Select all

start /wait bin\x64\factorio.exe --start-server save.zip

Hornwitser
Fast Inserter
Fast Inserter
Posts: 205
Joined: Fri Oct 05, 2018 4:34 pm
Contact:

Re: [0.17.68] Server started from cmd has broken input stream (Windows)

Post by Hornwitser »

How am I supposed to send input to a factorio server on Windown when it ignores stdin and instead opt hijack the input stream of the console belonging to the process group it's associated with?

Edit: Programatically speaking that is. Like from a process manager for factorio servers.

DaleStan
Filter Inserter
Filter Inserter
Posts: 368
Joined: Mon Jul 09, 2018 2:40 am
Contact:

Re: [0.17.68] Server started from cmd has broken input stream (Windows)

Post by DaleStan »

The Windows command prompt has a special feature that all windows applications (as opposed to console applications) are started as if you'd ended the command with & in bash. Use something that doesn't have this feature, or use "start /wait".

User avatar
zero318
Burner Inserter
Burner Inserter
Posts: 16
Joined: Fri Apr 13, 2018 2:57 pm
Contact:

Re: [0.17.68] Server started from cmd has broken input stream (Windows)

Post by zero318 »

Hornwitser wrote: ↑
Thu Sep 12, 2019 5:47 pm
How am I supposed to send input to a factorio server on Windown when it ignores stdin and instead opt hijack the input stream of the console belonging to the process group it's associated with?

Edit: Programatically speaking that is. Like from a process manager for factorio servers.
I'd like to know the same thing. I had no idea why my server manager refused to work with Factorio until I found this thread. All I'm trying to do is pass I/O to a server without using RCON, but nothing I've tried has worked.
I make mods! Feedback is appreciated, particularly if I broke something.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13202
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.17.68] Server started from cmd has broken input stream (Windows)

Post by Rseding91 »

You can just launch the game without using a command prompt. Just "run" it directly from what ever program you're going to use to send input/output to the game.
If you want to get ahold of me I'm almost always on Discord.

Hornwitser
Fast Inserter
Fast Inserter
Posts: 205
Joined: Fri Oct 05, 2018 4:34 pm
Contact:

Re: [0.17.68] Server started from cmd has broken input stream (Windows)

Post by Hornwitser »

Rseding91 wrote: ↑
Mon Mar 02, 2020 1:24 am
You can just launch the game without using a command prompt. Just "run" it directly from what ever program you're going to use to send input/output to the game.
No. This doesn't work. The Windows Release build does not read from stdin when spawned from a process that owns a console. Here's a few ways you can tell this is the case:
  • Input is being read from the console after the command prompt has returned. The console input buffer is no longer connected to the program's stdin at that point, in fact it was never connected to it in the first place as factorio is not a console process, so what Factorio is reading is not stdin.
  • Input redirection doesn't work. Appending < input-file.txt to the command executed in cmd does not cause Factorio to read anything in input-file.txt, instead it'll still read input from the console input buffer.
  • Spawning factorio from a console application, for example in python with code like

    Code: Select all

    import subprocess, sys
    subprocess.run('factorio.exe --start-server-load-scenario base/freeplay', stdout=sys.stdout, input=b'this is stdin which is completely ignored\n')
    does not let you send input to the game from that application. Instead Factorio opens the console input buffer of the parent process' console and reads that.
I was able to get the Alpha build to read stdin when launching it from a process that does not own a console, but it's rare for server managers to not be a console application (oddly enough 0.17.79 version would not write the usual log to stdout, only the chat log, while 0.18.9 did.)

User avatar
zero318
Burner Inserter
Burner Inserter
Posts: 16
Joined: Fri Apr 13, 2018 2:57 pm
Contact:

Re: [0.17.68] Server started from cmd has broken input stream (Windows)

Post by zero318 »

Can confirm that redirection of stdin doesn't work in that scenario.

I've been writing a wrapper in C# that uses the Process.Start() method to launch the server, which is the standard way of starting a child process. However, attempting to programmatically write to stdin results in the exact behavior described in the original post.

Image

Without going into too much detail about my own code, any input lines that start with "wrapper" are supposed to be processed separately as a means of interacting with the wrapper program itself. In the screenshot above I'm attempting to send a "testecho" command to the wrapper program, which should just echo back the following text argument in a cyan "wrapper" line. However, every other input completely bypasses all of the wrapper code and is sent directly to the Factorio server.

Image

I initially assumed it was a bug in my own code, but even commenting out the entire block of code responsible for sending input to Factorio doesn't change the behavior. The expected behavior is that the server would only respond to input specifically written to its redirected stdin by the wrapper program, but instead it directly receives every other user input and ignores all attempts at programmatic input.
I make mods! Feedback is appreciated, particularly if I broke something.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13202
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.17.68] Server started from cmd has broken input stream (Windows)

Post by Rseding91 »

Is your program compiled as a console application? So, Factorio sees it as a console and attaches to it?
If you want to get ahold of me I'm almost always on Discord.

User avatar
zero318
Burner Inserter
Burner Inserter
Posts: 16
Joined: Fri Apr 13, 2018 2:57 pm
Contact:

Re: [0.17.68] Server started from cmd has broken input stream (Windows)

Post by zero318 »

Rseding91 wrote: ↑
Tue Mar 03, 2020 7:46 pm
Is your program compiled as a console application? So, Factorio sees it as a console and attaches to it?
Yes, my program is compiled as a console app. I'm not familiar enough with the inner workings of Windows to know whether or not that's exactly the right description of the behavior, but it certainly sounds correct.
I make mods! Feedback is appreciated, particularly if I broke something.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13202
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.17.68] Server started from cmd has broken input stream (Windows)

Post by Rseding91 »

zero318 wrote: ↑
Thu Mar 05, 2020 2:24 am
Rseding91 wrote: ↑
Tue Mar 03, 2020 7:46 pm
Is your program compiled as a console application? So, Factorio sees it as a console and attaches to it?
Yes, my program is compiled as a console app. I'm not familiar enough with the inner workings of Windows to know whether or not that's exactly the right description of the behavior, but it certainly sounds correct.
If you compile as not-a-console-application Factorio won't attach itself to your program when you launch it.
If you want to get ahold of me I'm almost always on Discord.

Hornwitser
Fast Inserter
Fast Inserter
Posts: 205
Joined: Fri Oct 05, 2018 4:34 pm
Contact:

Re: [0.17.68] Server started from cmd has broken input stream (Windows)

Post by Hornwitser »

Rseding91 wrote: ↑
Thu Mar 05, 2020 10:15 am
If you compile as not-a-console-application Factorio won't attach itself to your program when you launch it.
This is not an option for Clusterio which runs on Node.js which in turn does not provide a windows subsystem build of their runtime. Not having a console is also kind of inconvenient for this kind of application.

User avatar
zero318
Burner Inserter
Burner Inserter
Posts: 16
Joined: Fri Apr 13, 2018 2:57 pm
Contact:

Re: [0.17.68] Server started from cmd has broken input stream (Windows)

Post by zero318 »

Rseding91 wrote: ↑
Thu Mar 05, 2020 10:15 am
If you compile as not-a-console-application Factorio won't attach itself to your program when you launch it.
For now I'll look into making my program work as a GUI program. Not looking forward to refactoring my code, but also excited to make it finally work.

However, it still seems like this is not desirable behavior since dedicated servers are usually run via console to save resources. Is there any possibility this could be looked into further?

Edit:

After further investigation, it seems that everything works as expected so long as there's no console for Factorio to attach to shortly after launching. Using a block of code like this fixes the issue without needing to compile as a GUI program. There's probably a simpler way of implementing this, but it's the first thing I found that reliably works.

Image
I make mods! Feedback is appreciated, particularly if I broke something.

Post Reply

Return to β€œWon't fix.”