joshbarrass / TerrariaServerWrapper

Alternative Terraria Server launcher providing auto- and exit saving.
MIT License
3 stars 1 forks source link

High CPU utilization on idle server process #2

Open duffyjp opened 10 months ago

duffyjp commented 10 months ago

Hi, thanks for making this wrapper. I have a docker-compose setup with 5 worlds and everything is working well except I'm seeing really high CPU usage without anyone connected. The worlds are fully generated and loaded in from disk. Users can log in and play fine.

There are no messages in the docker logs other than the autosaves that your wrapper provides.

Screenshot 2023-10-31 at 2 27 25 PM Screenshot 2023-10-31 at 2 32 56 PM

I'm hosting on Windows 10 with the WSL 2 backend. I'm using docker.io/ubuntu:latest as my base image.

Running a server the basic way with start-server.bat in Windows uses basically no CPU at all while idle.

Screenshot 2023-10-31 at 2 30 58 PM

Any thoughts?

joshbarrass commented 10 months ago

I suspect the problem is related to this loop, which is constantly running in the background to forward the contents of stdin to the server. I will need to look into how I can make this loop more lazy. I have a few ideas, but will need to test them.

In the meantime, I will see about adding an option to disable forwarding stdin. I should then be able to confirm whether this is indeed the problem, and you could also disable this to alleviate the problem if you don't use the console to control the server.

joshbarrass commented 10 months ago

I've just tried running it locally and the problem only seems to occur for me when running the container without either -i or -t. When running with -i, -t or -it, the CPU usage is normal, so as a temporary workaround, try adding one of these!

I'll look into how I can test for this within the program and avoid spiking the CPU.

joshbarrass commented 10 months ago

@duffyjp give the latest release a try, and add the environment variable INTERACTIVE=false if you aren't using -i. Since I can only reproduce the issue when running a non-interactive docker container, I'm assuming you aren't using the console anyway, and setting this environment variable should solve your problem.

I'll look into how I can automatically detect whether the session is interactive, but this should work for now. Let me know if this solves your problem, and if it turns out you are using an interactive container, please let me know so I can try reproducing the bug in more detail.

duffyjp commented 10 months ago

@joshbarrass Thanks, I'll try the update tonight. As an FYI, I forked your repo and commented out the input and save loops and that did drop the CPU use right down.

If you'd like to checkout my docker config I pushed that up as a repo here: https://github.com/duffyjp/terraria-server-farm

I'm still getting around 10% CPU load for each map without users connected but it's manageable.

trnubo commented 8 months ago

I was facing the same high CPU utilisation issue when running in docker without -it. My workaround was to exit the input loop if os.Stdin.Read(buf) errors with EOF. You can see my code at https://github.com/trnubo/terraria/blob/b572e363a586f231f56512b2cac05d0fcff201be/TerrariaServerWrapper/internal/loop.go#L26-L32 (I have modified the wrapper quite a bit to run tshock which didn't require the auto save).

joshbarrass commented 6 months ago

Hi @trnubo, thanks for the suggestion. I've added that to the code now.

I'm trying to think if there might be any side effects from this (e.g. if EOF is accidentally sent via CTRL+D, we lose interactivity?)... if I get some time, I might take a look into this and see if I can come up with anything more sophisticated (maybe testing somehow for a TTY). Either way, the latest release adds that solution and should hopefully alleviate the CPU load issue without having to manually set the env var.