Skip to content

Fix expect() timeout never firing while the stream keeps producing output - #3

Merged
vzakaznikov merged 2 commits into
testflows:masterfrom
CarlosFelipeOR:fix/expect-timeout
Aug 19, 2026
Merged

Fix expect() timeout never firing while the stream keeps producing output#3
vzakaznikov merged 2 commits into
testflows:masterfrom
CarlosFelipeOR:fix/expect-timeout

Conversation

@CarlosFelipeOR

Copy link
Copy Markdown
Contributor

Close #2

Contributor License Agreement

For more information see https://github.com/testflows/TestFlows/blob/master/CONTRIBUTING.md.

You can electronically sign the CLA inside your first pull request using CLA Assistant by reading the CLA
and, when requested, sign it by adding a comment containing the following text.

I have read the CLA Document and I hereby sign the CLA

I, the Contributor, have agreed to and signed the CLA.

Description of Changes

expect() only evaluated its deadline inside the except TimeoutError: branch, which is reached only when read() sees no data for 0.1s. With a stream that never goes quiet that branch is never taken, so timeleft reaches 0 and the loop spins indefinitely.

The deadline is now checked on every iteration: search first, so a match that just arrived still wins, then check the deadline, then read. The timeout block is moved unchanged, so expect_timeout=True still returns instead of raising.

Verified with the reproducer from #2:

case before after
timeout, quiet stream 3.0s 3.0s
timeout, continuous stream never fires 3.8s
match, quiet stream ok ok
match, continuous stream ok ok

The continuous-stream case overshoots slightly (3.8s for timeout=3) because the regex search over the accumulated buffer runs before the deadline check. That ordering is deliberate — checking first would discard a match that had just arrived.

…eeps producing output

Signed-off-by: CarlosFelipeOR <carlosfelipeor@gmail.com>
@CarlosFelipeOR CarlosFelipeOR changed the title "Fix expect() timeout never firing while the stream keeps producing output Fix expect() timeout never firing while the stream keeps producing output Aug 11, 2026
@CarlosFelipeOR

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@vzakaznikov

Copy link
Copy Markdown
Collaborator

The solution does not seem to be correct. Try changing

while timeleft >= 0:
to just ">" so that the 0 value will cause an exception.

@vzakaznikov

Copy link
Copy Markdown
Collaborator

Actually, the read(timeout=0) is treated as non-blocking so if data is there it will read. So expect with timeout=0 should do the same, try only once if data is read and matched then no exception otherwise raise.

…ries once

Signed-off-by: CarlosFelipeOR <carlosfelipeor@gmail.com>
@CarlosFelipeOR

CarlosFelipeOR commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

Done. expect() now attempts exactly one read before the deadline can fire, so expect(timeout=0) matches when the data is already there and raises otherwise. read() is untouched, so read(timeout=0) stays non-blocking.

Behaviour matrix used to verify it, generated by Claude Opus 5: test_matrix.py

python3 test_matrix.py <path-to-checkout> — 8/8 on this branch. On b7ddbd3 two cases hang; on the previous commit the two timeout=0 cases fail.

@vzakaznikov could you review it again?

@vzakaznikov
vzakaznikov merged commit f0e83c5 into testflows:master Aug 19, 2026
1 check failed
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

expect() timeout never fires while the stream keeps producing output

2 participants