From 25f44828d7d5d0382f8d498239c5a7a93e9df943 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 21:13:26 +0000 Subject: [PATCH] docs(shmem): document that sender and receiver must use the same kind of type - shmem uses one metadata format for a single message and another for a multipart message; Receive(Parts) of a single send, or Receive(MessagePtr) of a multipart send, gives wrong metadata or a crash - Add a sender/receiver table to the messages page of the documentation and a note to Channel::Receive() - Update the source line links into Channel.h - Keep PartsTransfer.SingleSendMultipartReceive disabled: a fix changes the shmem wire format Co-Authored-By: Claude Opus 5.5 --- docs/src/content/docs/concepts/messages.mdx | 18 +++++++++-- docs/src/content/docs/concepts/threads.mdx | 2 +- .../docs/examples/multiple-transports.mdx | 2 +- docs/src/content/docs/examples/readout.mdx | 2 +- docs/src/content/docs/guides/end-of-run.mdx | 2 +- .../docs/guides/several-transports.mdx | 2 +- .../docs/reference/channel-properties.mdx | 32 +++++++++---------- .../content/docs/reference/transfer-codes.mdx | 2 +- fairmq/Channel.h | 4 +++ test/parts/_access.cxx | 9 +++--- 10 files changed, 47 insertions(+), 28 deletions(-) diff --git a/docs/src/content/docs/concepts/messages.mdx b/docs/src/content/docs/concepts/messages.mdx index e861b9e6..224c4225 100644 --- a/docs/src/content/docs/concepts/messages.mdx +++ b/docs/src/content/docs/concepts/messages.mdx @@ -168,7 +168,7 @@ message owns its buffer. When you destroy the message, the transport releases th ### A message of another transport [`Channel::Send()`](/api/fair/mq/Channel/#Send) compares the transport of each message with the -transport of the channel ([Channel.h](/source/fairmq/Channel.h/#L421-L480)). If they are different, +transport of the channel ([Channel.h](/source/fairmq/Channel.h/#L425-L484)). If they are different, the channel makes a new message of its own transport, and your `MessagePtr` points to the new message after the call: @@ -228,12 +228,26 @@ apply to each part. This test sends three parts, one of them empty, and receives :::caution[Rules for multipart messages] - Do not send an empty `Parts` object. Both transports return `TransferCode::error`. - Receive with the same form that the sender uses: `Parts` for `Parts`, `MessagePtr` for - `MessagePtr`. The shmem transport cannot read a single message into a `Parts` object. + `MessagePtr`. See the table below. - `Receive()` adds the received parts at the end of the `Parts` object. Give it an empty one. - A receiver with [`OnData()`](/api/fair/mq/Device/#OnData-3) must register a callback that takes `fair::mq::Parts&`. ::: +The two transports do not accept the same mix of forms: + +| Sender | Receiver | zeromq | shmem | +| --- | --- | --- | --- | +| `Send(MessagePtr&)` | `Receive(MessagePtr&)` | yes | yes | +| `Send(Parts&)` or `vector` | `Receive(Parts&)` or `vector` | yes | yes | +| `Send(MessagePtr&)` | `Receive(Parts&)` or `vector` | yes | **no** | +| `Send(Parts&)` or `vector` | `Receive(MessagePtr&)` | one part per call | **no** | + +The shmem transport uses one metadata format for a single message and another for a multipart +message. A mix gives a message with wrong metadata or a crash. The test +`PartsTransfer.SingleSendMultipartReceive` in `test/parts/_access.cxx` shows the problem. It is +disabled, because a fix changes the shmem wire format. + ## Related pages - [Channels and sockets](/concepts/channels/): the channels that send and receive the messages. diff --git a/docs/src/content/docs/concepts/threads.mdx b/docs/src/content/docs/concepts/threads.mdx index 31d8ba59..8b4d49f8 100644 --- a/docs/src/content/docs/concepts/threads.mdx +++ b/docs/src/content/docs/concepts/threads.mdx @@ -65,7 +65,7 @@ Some rules follow from this table: thread that requests the transition. The next state resumes the transports in the thread of the state machine ([Device.cxx](/source/fairmq/Device.cxx/#L96-L104)). - A zeromq channel that sends a shmem message wraps the message. The free function of the wrapper - destroys the shmem message ([Channel.h](/source/fairmq/Channel.h/#L421-L438)). Thus the shmem + destroys the shmem message ([Channel.h](/source/fairmq/Channel.h/#L425-L442)). Thus the shmem message can be destroyed in a thread of ZeroMQ. - Both transports call the region event callback while they hold the lock of the subscription ([shmem/Manager.h](/source/fairmq/shmem/Manager.h/#L582-L616), diff --git a/docs/src/content/docs/examples/multiple-transports.mdx b/docs/src/content/docs/examples/multiple-transports.mdx index 5e19993c..aa0d7611 100644 --- a/docs/src/content/docs/examples/multiple-transports.mdx +++ b/docs/src/content/docs/examples/multiple-transports.mdx @@ -132,7 +132,7 @@ leaves `RUNNING` after it got this number of messages on both data channels. If the transport of a message is not the transport of the channel, `Send()` converts the message. It creates a new message of the channel transport for the same data -([Channel.h](/source/fairmq/Channel.h/#L421-L438)). If the channel uses `shmem`, this step copies the data +([Channel.h](/source/fairmq/Channel.h/#L425-L442)). If the channel uses `shmem`, this step copies the data into shared memory. `NewMessageFor()` creates the message with the transport of the channel, so `Send()` does not convert it. diff --git a/docs/src/content/docs/examples/readout.mdx b/docs/src/content/docs/examples/readout.mdx index 482aaa81..f58c6d15 100644 --- a/docs/src/content/docs/examples/readout.mdx +++ b/docs/src/content/docs/examples/readout.mdx @@ -95,7 +95,7 @@ The builder and the sender register a data callback with [`OnData()`](/api/fair/ -The sender forwards from the channel of `--input-name` (default `bs`) to `sr`. The message is a shmem message, but `sr` is a zeromq channel. [`Channel::Send()`](/api/fair/mq/Channel/#Send) compares the transports. If they differ, the channel puts the data of the message into a new message of its own transport. It does not copy the data. The new message deletes the original message when the zeromq transport releases the data. See [`Channel.h`](/source/fairmq/Channel.h/#L421-L438). +The sender forwards from the channel of `--input-name` (default `bs`) to `sr`. The message is a shmem message, but `sr` is a zeromq channel. [`Channel::Send()`](/api/fair/mq/Channel/#Send) compares the transports. If they differ, the channel puts the data of the message into a new message of its own transport. It does not copy the data. The new message deletes the original message when the zeromq transport releases the data. See [`Channel.h`](/source/fairmq/Channel.h/#L425-L442). Thus the readout device gets the acknowledgement for a block after the zeromq transport releases the data. diff --git a/docs/src/content/docs/guides/end-of-run.mdx b/docs/src/content/docs/guides/end-of-run.mdx index 1829161f..da89fb23 100644 --- a/docs/src/content/docs/guides/end-of-run.mdx +++ b/docs/src/content/docs/guides/end-of-run.mdx @@ -37,7 +37,7 @@ Thus a device must have all its data before it goes to `RESETTING DEVICE`. The t ## Linger The channel property `linger` is a time in ms. The default is 500 -([Channel.h](/source/fairmq/Channel.h/#L378)). [`Channel::Init()`](/api/fair/mq/Channel/#Init) sets +([Channel.h](/source/fairmq/Channel.h/#L382)). [`Channel::Init()`](/api/fair/mq/Channel/#Init) sets it as the ZeroMQ option `ZMQ_LINGER` on the socket ([Channel.cxx](/source/fairmq/Channel.cxx/#L325-L326)). The shmem transport sets it on the ZeroMQ socket that carries the metadata of its messages. diff --git a/docs/src/content/docs/guides/several-transports.mdx b/docs/src/content/docs/guides/several-transports.mdx index e73c17f5..62763116 100644 --- a/docs/src/content/docs/guides/several-transports.mdx +++ b/docs/src/content/docs/guides/several-transports.mdx @@ -94,7 +94,7 @@ transport, and makes it if the device has none. ## Send a message of another transport You can send a message on a channel of another transport. `Channel::Send()` then converts the -message ([Channel.h](/source/fairmq/Channel.h/#L421-L480)): +message ([Channel.h](/source/fairmq/Channel.h/#L425-L484)): | Message | Channel | What the channel does | | --- | --- | --- | diff --git a/docs/src/content/docs/reference/channel-properties.mdx b/docs/src/content/docs/reference/channel-properties.mdx index 5eeecf48..63d91c54 100644 --- a/docs/src/content/docs/reference/channel-properties.mdx +++ b/docs/src/content/docs/reference/channel-properties.mdx @@ -29,21 +29,21 @@ The type is the C++ type of the property. The parsers of `--channel-config` and | Key | Type | Default | Allowed values | Effect | | --- | --- | --- | --- | --- | | [`name`](#name) | string | none | `a`–`z`, `A`–`Z`, `0`–`9`, `-`, `_`, `[`, `]`, `#` | Name of the channel. | -| [`type`](#type) | string | [`unspecified`](/source/fairmq/Channel.h/#L369) | `push`, `pull`, `pub`, `sub`, `req`, `rep`, `dealer`, `router`, `xpub`, `xsub`, `pair` | Socket type. | -| [`method`](#method) | string | [`unspecified`](/source/fairmq/Channel.h/#L370) | `bind`, `connect` | Bind or connect the endpoints. | -| [`address`](#address) | string | [`unspecified`](/source/fairmq/Channel.h/#L371) | One or more endpoints, separated by `,` | Endpoints of the socket. | -| [`transport`](#transport) | string | [`default`](/source/fairmq/Channel.h/#L367) | `default`, `zeromq`, `shmem` | Transport of the sub-channel. | -| [`sndBufSize`](#sndbufsize-and-rcvbufsize) | int | [`1000`](/source/fairmq/Channel.h/#L372) | 0 or more | Maximum number of messages in the send queue. | -| [`rcvBufSize`](#sndbufsize-and-rcvbufsize) | int | [`1000`](/source/fairmq/Channel.h/#L373) | 0 or more | Maximum number of messages in the receive queue. | -| [`sndKernelSize`](#sndkernelsize-and-rcvkernelsize) | int | [`0`](/source/fairmq/Channel.h/#L374) | 0 or more | Kernel send buffer in bytes. 0: system default. | -| [`rcvKernelSize`](#sndkernelsize-and-rcvkernelsize) | int | [`0`](/source/fairmq/Channel.h/#L375) | 0 or more | Kernel receive buffer in bytes. 0: system default. | -| [`sndTimeoutMs`](#sndtimeoutms-and-rcvtimeoutms) | int | [`-1`](/source/fairmq/Channel.h/#L376) | -1, 0 or more | Default timeout of a send call in ms. | -| [`rcvTimeoutMs`](#sndtimeoutms-and-rcvtimeoutms) | int | [`-1`](/source/fairmq/Channel.h/#L377) | -1, 0 or more | Default timeout of a receive call in ms. | -| [`linger`](#linger) | int | [`500`](/source/fairmq/Channel.h/#L378) | -1, 0 or more | Time in ms that the socket keeps unsent messages after it closes. | -| [`rateLogging`](#ratelogging) | int | [`1`](/source/fairmq/Channel.h/#L379) | 0 or more | Interval of the rate log in seconds. 0: no rate log. | -| [`portRangeMin`](#portrangemin-portrangemax-and-autobind) | int | [`22000`](/source/fairmq/Channel.h/#L380) | A TCP port | Lowest port for autobind. | -| [`portRangeMax`](#portrangemin-portrangemax-and-autobind) | int | [`23000`](/source/fairmq/Channel.h/#L381) | A TCP port | Highest port for autobind. | -| [`autoBind`](#portrangemin-portrangemax-and-autobind) | bool | [`true`](/source/fairmq/Channel.h/#L382-L386) | `true`, `false`, `1`, `0` | Bind to a random port of the range when the bind fails. | +| [`type`](#type) | string | [`unspecified`](/source/fairmq/Channel.h/#L373) | `push`, `pull`, `pub`, `sub`, `req`, `rep`, `dealer`, `router`, `xpub`, `xsub`, `pair` | Socket type. | +| [`method`](#method) | string | [`unspecified`](/source/fairmq/Channel.h/#L374) | `bind`, `connect` | Bind or connect the endpoints. | +| [`address`](#address) | string | [`unspecified`](/source/fairmq/Channel.h/#L375) | One or more endpoints, separated by `,` | Endpoints of the socket. | +| [`transport`](#transport) | string | [`default`](/source/fairmq/Channel.h/#L371) | `default`, `zeromq`, `shmem` | Transport of the sub-channel. | +| [`sndBufSize`](#sndbufsize-and-rcvbufsize) | int | [`1000`](/source/fairmq/Channel.h/#L376) | 0 or more | Maximum number of messages in the send queue. | +| [`rcvBufSize`](#sndbufsize-and-rcvbufsize) | int | [`1000`](/source/fairmq/Channel.h/#L377) | 0 or more | Maximum number of messages in the receive queue. | +| [`sndKernelSize`](#sndkernelsize-and-rcvkernelsize) | int | [`0`](/source/fairmq/Channel.h/#L378) | 0 or more | Kernel send buffer in bytes. 0: system default. | +| [`rcvKernelSize`](#sndkernelsize-and-rcvkernelsize) | int | [`0`](/source/fairmq/Channel.h/#L379) | 0 or more | Kernel receive buffer in bytes. 0: system default. | +| [`sndTimeoutMs`](#sndtimeoutms-and-rcvtimeoutms) | int | [`-1`](/source/fairmq/Channel.h/#L380) | -1, 0 or more | Default timeout of a send call in ms. | +| [`rcvTimeoutMs`](#sndtimeoutms-and-rcvtimeoutms) | int | [`-1`](/source/fairmq/Channel.h/#L381) | -1, 0 or more | Default timeout of a receive call in ms. | +| [`linger`](#linger) | int | [`500`](/source/fairmq/Channel.h/#L382) | -1, 0 or more | Time in ms that the socket keeps unsent messages after it closes. | +| [`rateLogging`](#ratelogging) | int | [`1`](/source/fairmq/Channel.h/#L383) | 0 or more | Interval of the rate log in seconds. 0: no rate log. | +| [`portRangeMin`](#portrangemin-portrangemax-and-autobind) | int | [`22000`](/source/fairmq/Channel.h/#L384) | A TCP port | Lowest port for autobind. | +| [`portRangeMax`](#portrangemin-portrangemax-and-autobind) | int | [`23000`](/source/fairmq/Channel.h/#L385) | A TCP port | Highest port for autobind. | +| [`autoBind`](#portrangemin-portrangemax-and-autobind) | bool | [`true`](/source/fairmq/Channel.h/#L386-L390) | `true`, `false`, `1`, `0` | Bind to a random port of the range when the bind fails. | | [`numSockets`](#numsockets) | int | [`0`](/source/fairmq/JSONParser.cxx/#L129) | 0 or more | Number of sub-channels with the same properties. | The JSON parser uses the same defaults ([JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L111-L126)). The device also uses them for a property that is not set ([Channel.cxx](/source/fairmq/Channel.cxx/#L92-L112)). @@ -130,7 +130,7 @@ The start script of the [region example](/examples/region/) sets `sndKernelSize= ### `sndTimeoutMs` and `rcvTimeoutMs` -The timeout of a send or receive call that does not give its own timeout ([Channel.h](/source/fairmq/Channel.h/#L297-L333)). This applies to [`Channel::Send()`](/api/fair/mq/Channel/#Send) and [`Channel::Receive()`](/api/fair/mq/Channel/#Receive), to [`Device::Send()`](/api/fair/mq/Device/#Send) and [`Device::Receive()`](/api/fair/mq/Device/#Receive) without the timeout argument, and to the receive calls of [`OnData()`](/api/fair/mq/Device/#OnData). +The timeout of a send or receive call that does not give its own timeout ([Channel.h](/source/fairmq/Channel.h/#L297-L337)). This applies to [`Channel::Send()`](/api/fair/mq/Channel/#Send) and [`Channel::Receive()`](/api/fair/mq/Channel/#Receive), to [`Device::Send()`](/api/fair/mq/Device/#Send) and [`Device::Receive()`](/api/fair/mq/Device/#Receive) without the timeout argument, and to the receive calls of [`OnData()`](/api/fair/mq/Device/#OnData). - `-1`: wait until the transfer is complete. - `0`: do not wait. If the transfer is not possible at once, the call returns `TransferCode::timeout` (-2). diff --git a/docs/src/content/docs/reference/transfer-codes.mdx b/docs/src/content/docs/reference/transfer-codes.mdx index 97db36bf..409a983f 100644 --- a/docs/src/content/docs/reference/transfer-codes.mdx +++ b/docs/src/content/docs/reference/transfer-codes.mdx @@ -43,7 +43,7 @@ message, the number of bytes is the sum of all parts. | Positive | Wait up to this time in ms, rounded up to a multiple of 100 ms. | The channel properties `sndTimeoutMs` and `rcvTimeoutMs` give the timeout of a channel. Their -default is `-1` ([`Channel.h`](/source/fairmq/Channel.h/#L376-L377)). The steps of 100 ms come from +default is `-1` ([`Channel.h`](/source/fairmq/Channel.h/#L380-L381)). The steps of 100 ms come from the internal socket timeout of the zeromq and shmem transports ([`zeromq/Common.h`](/source/fairmq/zeromq/Common.h/#L64-L77)). diff --git a/fairmq/Channel.h b/fairmq/Channel.h index 407d7bf5..bba4eb73 100644 --- a/fairmq/Channel.h +++ b/fairmq/Channel.h @@ -318,6 +318,10 @@ class Channel /// TransferCode::timeout if timed out, /// TransferCode::error if there was an error, /// TransferCode::interrupted if interrupted (e.g. by requested state change) + /// @note Receive with the same kind of type that the sender used: a MessagePtr for a + /// MessagePtr, a Parts/vector for a Parts/vector. The zeromq + /// transport also accepts a mix. The shmem transport uses a different metadata format for + /// each kind: a mix gives a message with wrong metadata or a crash. template std::enable_if_t::value, int64_t> Receive(M& m, Timeout&&... rcvTimeoutMs) diff --git a/test/parts/_access.cxx b/test/parts/_access.cxx index 95cff5a3..36b79f21 100644 --- a/test/parts/_access.cxx +++ b/test/parts/_access.cxx @@ -137,10 +137,11 @@ TEST_P(PartsTransfer, EmptyPartsIsRefused) EXPECT_EQ(pull.Receive(in, 100), static_cast(TransferCode::timeout)); } -// Known defect: shmem uses different metadata formats for a single message -// and for Parts. Receive(Parts) of a message sent with Send(MessagePtr) reads -// the size of the message as the number of parts, and reads metadata past the -// received data. zeromq receives it as one part. +// Known limitation (documented in docs/Transport.md and Channel::Receive()): +// shmem uses different metadata formats for a single message and for Parts. +// Receive(Parts) of a message sent with Send(MessagePtr) reads the size of the +// message as the number of parts, and reads metadata past the received data. +// zeromq receives it as one part. A fix changes the shmem wire format. TEST_P(PartsTransfer, DISABLED_SingleSendMultipartReceive) { ProgOptions config;