fal_client's SyncRequestHandle.get() drains iter_events() at its default interval=0.1, i.e. a status GET every ~0.3-0.5 s including RTT. Through the Nous tool gateway (200 requests per fixed one-minute window) a single 12 s image cost 42 requests (~208 req/min), so a second concurrent job — or the same user's video job — trips RATE_LIMIT_EXCEEDED on submit, status and result calls. Add tools.fal_common.wait_fal_queue_result(): drain iter_events() at FAL_QUEUE_POLL_SECONDS (2 s), then call get(), whose first status check now sees Completed and goes straight to the result GET. Public SDK API only, no monkeypatching. The image tool's interrupt-aware _wait_fal_result and both FAL video waits (generation + SeedVR upscale) use it. Live, one gpt-image-2.5 flare image via the gateway: 42 -> 9 requests, 12.1 s -> 12.5 s wall clock. Test fakes that modelled the handle with only get() gain an empty iter_events(); two touched test files also get encoding="utf-8" on their config write_text (windows-footguns).
73 lines
2.0 KiB
Python
73 lines
2.0 KiB
Python
"""_wait_fal_result must notice a user interrupt while the FAL job runs."""
|
|
|
|
import threading
|
|
import time
|
|
|
|
import pytest
|
|
|
|
import tools.image_generation_tool as image_tool
|
|
from tools.interrupt import set_interrupt
|
|
|
|
|
|
class _SlowHandler:
|
|
"""Fake FAL handler whose get() blocks like the real SDK."""
|
|
|
|
def __init__(self, delay=30.0, result=None):
|
|
self.delay = delay
|
|
self._result = result if result is not None else {"images": []}
|
|
|
|
def iter_events(self, **_):
|
|
return iter(())
|
|
|
|
def get(self):
|
|
time.sleep(self.delay)
|
|
return self._result
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _clean_interrupt():
|
|
set_interrupt(False)
|
|
yield
|
|
set_interrupt(False)
|
|
|
|
|
|
def test_wait_fal_result_raises_on_interrupt():
|
|
def _interrupt_soon(tid):
|
|
time.sleep(0.2)
|
|
set_interrupt(True, tid)
|
|
|
|
tid = threading.current_thread().ident
|
|
threading.Thread(target=_interrupt_soon, args=(tid,), daemon=True).start()
|
|
|
|
t0 = time.monotonic()
|
|
with pytest.raises(image_tool.ImageGenerationInterrupted):
|
|
image_tool._wait_fal_result(_SlowHandler(delay=30.0), poll_seconds=0.05)
|
|
assert time.monotonic() - t0 < 5.0
|
|
|
|
|
|
def test_wait_fal_result_propagates_handler_error():
|
|
class _ErrHandler(_SlowHandler):
|
|
def get(self):
|
|
raise ValueError("boom")
|
|
|
|
with pytest.raises(ValueError, match="boom"):
|
|
image_tool._wait_fal_result(_ErrHandler())
|
|
|
|
|
|
def test_upscale_interrupt_propagates(monkeypatch):
|
|
"""_upscale_image must NOT swallow the interrupt into a None fallback."""
|
|
|
|
monkeypatch.setattr(
|
|
image_tool, "_submit_fal_request", lambda *a, **k: _SlowHandler(30.0)
|
|
)
|
|
|
|
def _interrupt_soon(tid):
|
|
time.sleep(0.2)
|
|
set_interrupt(True, tid)
|
|
|
|
tid = threading.current_thread().ident
|
|
threading.Thread(target=_interrupt_soon, args=(tid,), daemon=True).start()
|
|
|
|
with pytest.raises(image_tool.ImageGenerationInterrupted):
|
|
image_tool._upscale_image("https://example.com/x.png", "prompt")
|