Gut icon indicating copy to clipboard operation
Gut copied to clipboard

wait_physics_frames(n) waits one extra frame when called right after another wait

Open M-boxx opened this issue 2 months ago • 1 comments

Hi! Thanks for GUT, we use it for all the tests in our game.

I noticed something odd while measuring a speed curve frame by frame. My loop did await wait_physics_frames(1) and then read a value, and the curve came out twice as fast as it really was. It looks like after the first wait, every following wait lets one extra _physics_process through.

Here's a small test that shows it:

extends GutTest

class Counter:
	extends Node
	var n := 0
	func _physics_process(_d): n += 1

func test_chained_waits():
	await wait_process_frames(3)
	var c = add_child_autofree(Counter.new())
	var per := []
	for i in 6:
		var before: int = c.n
		await wait_physics_frames(1)
		per.append(c.n - before)
	print("wait_physics_frames(1) x6: ", per)

	var c2 = add_child_autofree(Counter.new())
	var raw := []
	for i in 6:
		var before: int = c2.n
		await get_tree().physics_frame
		raw.append(c2.n - before)
	print("await physics_frame x6:    ", raw)
	pass_test("")

which prints

wait_physics_frames(1) x6: [1, 2, 2, 2, 2, 2]
await physics_frame x6:    [1, 1, 1, 1, 1, 1]

Chained waits of 2, 5 and 10 gave me 3, 6 and 11.

From what I can tell, the awaiter ends a wait from inside its physics_frame handler, so the test resumes before that frame's _physics_process calls have run. The next wait then starts counting at the next physics_frame, so the current frame runs but is never counted.

I tried this in awaiter.gd and it seems to fix it:

 func wait_physics_frames(x, msg=''):
 	await_logger.waiting_on = str(x, " physics frames ", msg)
 	_did_last_wait_timeout = false
 	_wait_physics_frames = x
+	if(Engine.is_in_physics_frame()):
+		_elapsed_frames = 1
 	wait_started.emit()

With it, chained waits of 2, 5, 10 and 1 give 2, 5, 10 and 1, and test_awaiter.gd still passes. I also added a chained case there.

I didn't want to go straight to a PR, though, because TestElapsedTimeAndFrames.test_get_elapsed_physics_frames in test_test.gd expects 11 and then 22 after two wait_physics_frames(10) in a row. So maybe this is how it's meant to work? With my change that test gets 21, and it's the only failure in the suite. If it isn't intended, I'm happy to open a PR with the fix, the new test and that assertion updated.

wait_process_frames does the same thing (chained wait_process_frames(1) gave me [2, 2, 2, 2, 2, 2]). I didn't find a process-frame equivalent of Engine.is_in_physics_frame(), so I'm not sure what the clean fix is there.

GUT 9.7.1 (godot_4_7 branch), Godot 4.7.stable.official.5b4e0cb0f, Linux, running headless.

M-boxx avatar Sep 22 '26 14:09 M-boxx

I can reproduce this with GUT 9.7.1 on Godot 4.7.2 (headless, Linux). Same-type waits in a loop, 5 iterations each, counting _physics_process / _process calls of a node that is already in the tree:

wait_physics_frames(1) [1, 2, 2, 2, 2]
wait_physics_frames(2) [3, 3, 3, 3, 3]
wait_physics_frames(5) [6, 6, 6, 6, 6]
wait_process_frames(1) [1, 2, 2, 2, 2]
wait_process_frames(2) [3, 3, 3, 3, 3]
wait_process_frames(5) [6, 6, 6, 6, 6]

One extra data point: alternating the two kinds (wait_physics_frames(1), then wait_process_frames(2), then wait_physics_frames(2)) counts exactly 2 each time. Only a wait started from the end of a wait of the same kind overshoots, which fits your explanation.

About a process-frame version of your fix: the engine counters can stand in for Engine.is_in_physics_frame(). Within one iteration, physics_frame/process_frame and the _physics_process/_process calls after them all see the same Engine.get_physics_frames() / Engine.get_process_frames() value, and physics runs before process. So the awaiter can store the first frame that hasn't run yet and end the wait when Engine.get_*_frames() - start >= x, instead of counting signals. For process frames I used the awaiter's own _process to tell whether the current frame was already processed:

var _start_frame := 0
var _last_processed_frame := -1

func _process(_delta):
	_last_processed_frame = Engine.get_process_frames()

func _next_process_frame():
	var f = Engine.get_process_frames()
	if(_last_processed_frame == f):
		f += 1
	return f

# wait_process_frames (and _signal_callback, which sets _wait_process_frames = 1):
#	_start_frame = _next_process_frame()
# wait_physics_frames:
#	_start_frame = Engine.get_physics_frames()
#	if(!Engine.is_in_physics_frame()):
#		_start_frame += 1
# _on_tree_process_frame / _on_tree_physics_frame:
#	_elapsed_frames = Engine.get_process_frames() - _start_frame   # or get_physics_frames()
#	if(_elapsed_frames >= _wait_process_frames):                   # or _wait_physics_frames
#		_end_wait()

With that, every row above comes out exact: 1/1/1/1/1, 2/2/2/2/2, 5/5/5/5/5 for both kinds.

But the full GUT suite (1762 tests) then has 3 failures, not just the test_test.gd one you found:

  • test_awaiter.gd test_wait_process_frames_counts_frames_in_process gets 9 instead of 10. It adds the Counter right before the wait, and a node added at that point doesn't get _process in the current frame. Today the extra frame hides that.
  • test_test.gd test_get_elapsed_process_frames gets 10/20 instead of 11/22, and test_get_elapsed_physics_frames gets 21 instead of 22. These assertions encode the current +1.

So for process frames, the extra frame that's wrong for nodes already in the tree is exactly what makes the count right for a node added just before the wait. I don't see a way to get both from the awaiter alone. That probably needs a call from @bitwes on which behaviour wait_process_frames(x) should promise.

vladosskins178 avatar Nov 01 '26 10:11 vladosskins178