Skip to content

fix: avoid ZMQ slow-joiner loss on pipeline connect - #297

Open
chrisk314 wants to merge 1 commit into
mainfrom
fix/zmq-slow-joiner
Open

chrisk314 wants to merge 1 commit into
mainfrom
fix/zmq-slow-joiner

Conversation

@chrisk314

Copy link
Copy Markdown
Contributor

Problem

_ZMQPipelineConnectorProxy.connect_recv returned as soon as the SUB socket was connected, but a subscription needs a round trip through the proxy before the XPUB side starts forwarding. A sender that connected and published immediately could therefore have its first messages dropped (the classic ZMQ slow-joiner problem). This surfaced as a flake in the Ray + ZMQ proxy integration test on GitHub Actions.

Change

  • Extend the settle delay in connect_recv from 0.1s to 0.5s, which covered the observed propagation window in local reproduction.
  • Mark the affected integration test @pytest.mark.flaky(reruns=3) so a residual race is retried rather than failing the run.

Notes

This is split out of #284, where it was originally found and diagnosed - it is unrelated to the message data classes and belongs in its own review.

The delay is a mitigation rather than a fix. A proper solution would confirm subscription propagation (e.g. a probe message with acknowledgement, or a subscribe/forward handshake) instead of guessing a wait; happy to follow up that way if preferred.

No issue is filed for this yet.

Fixes #None

`_ZMQPipelineConnectorProxy.connect_recv` returned as soon as the SUB socket was
connected, but a subscription takes a round trip through the proxy before the XPUB side
starts forwarding. A sender that connected and published immediately could therefore
have its first messages dropped, which surfaced as a flake in the Ray + ZMQ proxy
integration test on GitHub Actions.

- Extend the settle delay from 0.1s to 0.5s, which covered the observed propagation
  window in local reproduction.
- Mark the affected integration test as flaky with 3 reruns, so a residual race is
  retried rather than failing the run.

Split out of #284, where it was originally found: it is unrelated to the message data
classes and deserves its own review. The delay is a mitigation, not a fix; a proper
solution would confirm subscription propagation instead of waiting.
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown

Benchmark comparison for 894671bb (base) vs 3757b2b8 (PR)


------------------------------------------------------------------------------------------------------------------ benchmark: 2 tests -----------------------------------------------------------------------------------------------------------------
Name (time in ms)                                                                         Min                 Max                Mean            StdDev              Median               IQR            Outliers     OPS            Rounds  Iterations
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_benchmark_process_run (main/.benchmarks/Linux-CPython-3.14-64bit/0001_base)     534.1964 (1.0)      552.7469 (1.01)     541.3358 (1.00)     6.8481 (1.92)     539.6903 (1.00)     5.1630 (1.01)          2;1  1.8473 (1.00)          5           1
test_benchmark_process_run (pr/.benchmarks/Linux-CPython-3.14-64bit/0001_pr)         535.5735 (1.00)     544.7210 (1.0)      539.4150 (1.0)      3.5658 (1.0)      539.2918 (1.0)      5.1000 (1.0)           2;0  1.8539 (1.0)           5           1
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Legend:
  Outliers: 1 Standard Deviation from Mean; 1.5 IQR (InterQuartile Range) from 1st Quartile and 3rd Quartile.
  OPS: Operations Per Second, computed as 1 / Mean

@codecov

codecov Bot commented Oct 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

chrisk314 added a commit that referenced this pull request Dec 5, 2026
# Summary

Implements #102: feat: Base component for external communication
(#102).

Adds `MessageDataReader` / `MessageDataWriter` for reading and writing
**records** through a pub/sub broker - the broker equivalent of
`FileReader`/`FileWriter`, where each message carries one record and
`field_names` become the component's outputs (reader) or inputs
(writer). Both extend the existing `DataReader`/`DataWriter` bases,
adding what a broker needs and a finite source does not: a long-lived
connection, reconnection with backoff, and acknowledgment.

Two implementations are included: **Google Cloud PubSub** and **AWS
SQS/SNS**. Apache Kafka is split out into draft PR #298 - see the note
at the bottom for why.

**Integration tests against real infrastructure are deliberately
deferred** - no AWS/GCP accounts or broker resources exist yet.
Terraform for the required PubSub topic/subscription and SNS topic/SQS
queue lives in `plugboard-infra` (`terraform/ci-data-message-testing`),
and `plugboard-infra/docs/running-message-data-tests.md` describes
wiring its outputs into this suite. All 104 tests here are mock-based,
so no credentials are needed.

# Changes

**Base classes**
- `MessageDataReader` / `MessageDataWriter` extend `DataReader` /
`DataWriter` (buffer, input binding and batching are inherited, not
copied), adding `_connect`, `_disconnect`, `_receive`/`_send`,
`_convert`, and `_ack` (reader).
- Retry, exponential backoff and reconnect live in one shared place:
`plugboard/utils/retry.py`, with a frozen `RetryPolicy` value object
used by both bases.
- Broker exceptions (`MessageBrokerConnectionError` / `TransientError` /
`PermanentError`) are raised and honoured: each implementation maps its
SDK's failures onto them, and permanent errors skip retries that cannot
help.
- Settings for GCP/AWS are read as constructor fallbacks
(`GCP_PUBSUB_PROJECT_ID`, `AWS_REGION`); explicit arguments win.
- All four concrete classes are exported from `plugboard.library`.

**Implementations**
- `GCPPubSubDataReader` / `GCPPubSubDataWriter` (`google-cloud-pubsub`)
- every synchronous PubSub call is dispatched to a worker thread, so a
pull or publish never stalls the shared event loop.
- `AWSSQSDataReader` / `AWSSNSDataWriter` (`aiobotocore`) - long
polling, `delete_message_batch` for acks, and the 10-message SQS cap is
logged rather than applied silently.

**Packaging**: optional extras `gcp-pubsub` and `aws-messaging`; the
broker SDKs are also declared in the `test` dependency group, which is
what lets the tests import the real client and error types.

**Docs**: new [Message
Data](https://docs.plugboard.dev/usage/message-data/) usage page
(extras, worked example, delivery semantics), broker settings in the
configuration page, new extras in the README.

# Behaviour worth knowing

- **An empty poll is not the end of the stream.** A reader started
before its producer keeps waiting and picks up messages as they arrive,
like `WebsocketReader`. A subclass signals a genuinely exhausted source
by raising `NoMoreDataException`, which closes the IO as other data
readers do.
- **Messages are acknowledged after they are processed.** A batch
fetched from the broker is acked only once all of its records have been
published downstream, so a crash mid-batch leaves the unread messages
for redelivery (at-least-once). Unconsumed pre-fetched messages are
deliberately left unacked.
- **Sends are not cancelled at teardown**: an in-flight batch is awaited
(and any failure logged) rather than dropped.

# Testing

- 104 unit tests, all against teardown-aware `monkeypatch` fixtures
rather than global `sys.modules` mocks, asserting broker calls and
arguments rather than private attribute identity.
- Coverage for the paths the review flagged as untested: retry
exhaustion and the delay cap, empty-batch handling, SDK error mapping,
reconnect/disconnect failures, pending-send failure at teardown, and ack
timing.
- Branch coverage of the new modules: `utils/retry.py` 100%,
`message_reader.py` 95%, `message_writer.py` 94%, `gcp_pubsub_io.py`
94%, `aws_messaging_io.py` 95% - the remaining misses are
abstract-method bodies.
- `ruff check`, `ruff format --check`, `ty` over `plugboard/`,
`plugboard-schemas/` and `tests/` pass; `mkdocs build` is clean.
- Full unit suite: 387 passed, 6 skipped. Integration suite against the
compose services: 141 passed, 1 pre-existing ZMQ flake
(`test_process_stop_event[LocalProcess-ZMQConnector-...]`), which passes
on repeat runs in isolation and is addressed in #297.

# Related

- #298 (draft) - the Apache Kafka implementation and its tests, split
out of this PR.
- #297 - the ZMQ slow-joiner delay and flaky marker found while
debugging this branch; split out as unrelated scope.
- `plugboard-infra/terraform/ci-data-message-testing` - infrastructure
for the deferred real-broker integration tests.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant