Stdio blocks main thread despite running in another thread.

I’m using Godot 4.4 and try to read the console output from a process I run, but for whatever reason it blocks my main thread no matter what I do. What’s going on?

func _running(exec):
	while OS.is_process_running(exec.pid): 
		var content = exec.stdio.get_as_text().strip_edges()
		if content:
			print(content)
	
func start() -> void:
	if _thread == null:
		var exec = OS.execute_with_pipe(OS.get_executable_path(), ["--instance", "--identifier", _id], false)
		_thread = Thread.new()
		_thread.start(_running.bind(exec))

If I don’t start the thread everything runs fine, but as soon as I start reading stdio everything freezes.

There is an odd snippet in the docs

If blocking is false, created pipes work in non-blocking mode, i.e. read and write operations will return immediately. Use FileAccess.get_error() to check if the last read/write operation was successful.

That print call in the thread could be thrashing on the main threads stream buffer. I would probably slow the thread down with a delay, or wait till you have actual string data. Or call deferred on print but again dont do it every cycle. Delay the thread.

This lead me to the real solution, it was calling exec.stdio on every loop, changing it to _thread.start(_running.bind(exec.pid, exec.stdio)) fixed the issue.

But I did change the _running too

func _running(pid, stdio: FileAccess):
	while OS.is_process_running(pid): 
		if stdio.get_length() > 0:
			print(stdio.get_line().strip_edges())