-
Notifications
You must be signed in to change notification settings - Fork 0
fix: read-idle timeout on SSE so a stalled stream can't hang events() #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """events() must recover from a silently-stalled ("zombie") SSE connection | ||
| instead of blocking forever — a read-idle timeout trips, and the poll fallback | ||
| takes over. Regression for the long-job SSE hang.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import time | ||
|
|
||
| import httpx | ||
|
|
||
| from comfy_low import transport | ||
| from comfy_sdk import AsyncComfy, Comfy, Progress, StatusChange | ||
|
|
||
|
|
||
| def _wf(client): | ||
| return client.workflows.from_json({"3": {"class_type": "KSampler", "inputs": {}}}) | ||
|
|
||
|
|
||
| def test_events_recovers_from_zombie_stream(server, monkeypatch) -> None: | ||
| # Short read-idle timeout so the stalled stream trips it quickly. | ||
| monkeypatch.setattr(transport, "_SSE_IDLE_TIMEOUT", httpx.Timeout(5.0, read=0.3)) | ||
| server.state.sse_mode = "stall" | ||
| # Socket held open + silent, well past the 0.3s read timeout. | ||
| server.state.stall_seconds = 3.0 | ||
| server.state.polls_to_succeed = 1 # poll fallback sees terminal immediately | ||
|
|
||
| with Comfy(server.base_url) as client: | ||
| job = client.submit(_wf(client)) | ||
| t0 = time.monotonic() | ||
| seen = list(job.events()) # MUST NOT hang | ||
| elapsed = time.monotonic() - t0 | ||
|
|
||
| kinds = [type(e).__name__ for e in seen] | ||
| # Delivered the pre-stall frames, then recovered to a terminal StatusChange via poll. | ||
| assert any(isinstance(e, Progress) for e in seen), kinds | ||
| assert isinstance(seen[-1], StatusChange) and seen[-1].status == "succeeded", kinds | ||
| # Recovery must happen around the ~0.3s read timeout, NOT after the full 3s | ||
| # stall (which would mean it waited for the socket to close instead of timing out). | ||
| assert elapsed < 2.0, f"events() did not time out on the idle stream ({elapsed:.2f}s)" | ||
|
|
||
|
|
||
| async def test_async_events_recovers_from_zombie_stream(server, monkeypatch) -> None: | ||
| # The async path uses the same idle timeout + poll fallback — must also recover. | ||
| monkeypatch.setattr(transport, "_SSE_IDLE_TIMEOUT", httpx.Timeout(5.0, read=0.3)) | ||
| server.state.sse_mode = "stall" | ||
| server.state.stall_seconds = 3.0 | ||
| server.state.polls_to_succeed = 1 | ||
|
|
||
| async with AsyncComfy(server.base_url) as client: | ||
| job = await client.submit(_wf(client)) | ||
| t0 = time.monotonic() | ||
| seen = [e async for e in job.events()] # MUST NOT hang | ||
| elapsed = time.monotonic() - t0 | ||
|
|
||
| kinds = [type(e).__name__ for e in seen] | ||
| assert any(isinstance(e, Progress) for e in seen), kinds | ||
| assert isinstance(seen[-1], StatusChange) and seen[-1].status == "succeeded", kinds | ||
| assert elapsed < 2.0, f"async events() did not time out on the idle stream ({elapsed:.2f}s)" | ||
|
|
||
|
|
||
| def test_get_job_events_timeout_none_opts_out_of_idle_timeout(server, monkeypatch) -> None: | ||
| # The documented escape hatch: timeout=None disables the idle timeout, so the | ||
| # raw iterator rides out the stall until the socket closes rather than raising. | ||
| monkeypatch.setattr(transport, "_SSE_IDLE_TIMEOUT", httpx.Timeout(5.0, read=0.05)) | ||
| server.state.sse_mode = "stall" | ||
| server.state.stall_seconds = 0.8 # held open, then the stub closes | ||
|
|
||
| with Comfy(server.base_url) as client: | ||
| job = client.submit(_wf(client)) | ||
| events_url = job._model.urls.events | ||
| t0 = time.monotonic() | ||
| raws = list(client._low.get_job_events(events_url, timeout=None)) | ||
| elapsed = time.monotonic() - t0 | ||
|
|
||
| # Rode out the full stall (until close) instead of tripping the 0.05s default — | ||
| # proves timeout=None disabled the idle timeout. And it delivered both frames. | ||
| assert elapsed >= 0.7, f"timeout=None should not have timed out early ({elapsed:.2f}s)" | ||
| assert len(raws) == 2, raws | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.