Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/libs/broadcast_channel/redis/_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ def receive(self, timeout: float | None = 0.1) -> bytes | None:
except queue.Empty:
return None

if item == SIG_CLOSE:
raise SubscriptionClosedError(f"The Redis {self._get_subscription_type()} subscription is closed")
return item

@override
Expand Down
3 changes: 3 additions & 0 deletions api/services/workflow_event_snapshot_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from graphon.runtime import GraphRuntimeState
from graphon.runtime.graph_runtime_state_protocol import ReadOnlyVariablePool
from graphon.workflow_type_encoder import WorkflowRuntimeTypeConverter
from libs.broadcast_channel.exc import SubscriptionClosedError
from libs.datetime_utils import to_utc_timestamp
from models.human_input import HumanInputForm
from models.model import AppMode, Message
Expand Down Expand Up @@ -669,6 +670,8 @@ def _worker() -> None:
except queue.Full:
continue
logger.warning("Dropped buffered workflow event, total_dropped=%s", dropped_count)
except SubscriptionClosedError:
pass
except Exception:
logger.exception("Failed while buffering workflow events")
finally:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ def producer_thread(producer_idx: int) -> set[bytes]:
def consumer_thread() -> set[bytes]:
received_msgs: set[bytes] = set()
with subscription:
subscription.receive(timeout=0.1)
consumer_ready.set()
while True:
try:
Expand All @@ -260,7 +261,10 @@ def consumer_thread() -> set[bytes]:
for future in as_completed(producer_futures, timeout=30.0):
sent_msgs.update(future.result())

consumer_received_msgs = consumer_future.result(timeout=60.0)
try:
consumer_received_msgs = consumer_future.result(timeout=60.0)
finally:
subscription.close()

assert sent_msgs == consumer_received_msgs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,12 @@ def test_receive_on_closed_subscription(self, subscription, subscription_params)
with pytest.raises(SubscriptionClosedError):
subscription.receive()

def test_receive_does_not_leak_close_signal(self, subscription, subscription_params):
subscription._queue.put_nowait(SIG_CLOSE)

with pytest.raises(SubscriptionClosedError):
subscription.receive()

# ==================== Table-driven Tests ====================

@pytest.mark.parametrize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from core.workflow.nodes.human_input.pause_reason import HumanInputRequired
from graphon.enums import WorkflowExecutionStatus, WorkflowNodeExecutionStatus
from graphon.runtime import GraphRuntimeState, VariablePool
from libs.broadcast_channel.exc import SubscriptionClosedError
from libs.datetime_utils import to_utc_timestamp
from models.enums import ConversationFromSource, CreatorUserRole
from models.human_input import HumanInputForm, HumanInputFormRecipient, RecipientType
Expand Down Expand Up @@ -718,6 +719,17 @@ def receive(self, timeout: int = 1) -> bytes | None:
assert finished is True


def test_start_buffering_should_treat_closed_subscription_as_done(caplog: pytest.LogCaptureFixture) -> None:
class Subscription:
def receive(self, timeout: int = 1) -> bytes | None:
raise SubscriptionClosedError("closed")

buffer_state = service_module._start_buffering(Subscription())

assert buffer_state.done_event.wait(timeout=1) is True
assert "Failed while buffering workflow events" not in caplog.text


def test_build_workflow_event_stream_should_emit_ping_and_terminal_snapshot_event(
monkeypatch: pytest.MonkeyPatch,
unbound_session_factory: sessionmaker[Session],
Expand Down
Loading