Fix mpsc bounded channel capacity to match documented (buffer + num_senders)#2998
Open
Emilie-Thome wants to merge 1 commit intorust-lang:masterfrom
Open
Fix mpsc bounded channel capacity to match documented (buffer + num_senders)#2998Emilie-Thome wants to merge 1 commit intorust-lang:masterfrom
Emilie-Thome wants to merge 1 commit intorust-lang:masterfrom
Conversation
The channel(buffer) documentation states capacity is buffer + num_senders, but the implementation used a fixed capacity of buffer, ignoring the sender count. This also caused a behavioral gap between async send and try_send. Fix the park condition in do_send_b to account for num_senders, simplify poll_flush which incorrectly blocked on capacity instead of being a no-op, and update tests to match the documented semantics. Signed-off-by: Emilie Thome <emilie.thome@ampere.cars>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The documentation for
channel(buffer)states:However, the implementation only used buffer as the capacity threshold, ignoring num_senders.
This PR tries to fix two issues:
sendandtry_send: asyncsendwould park after buffer messages, whiletry_sendeffectively allowed one extra message.Changes in PR:
mpsc/mod.rs: Changed the park condition in do_send_b fromnum_messages > self.inner.buffertonum_messages >= self.inner.buffer + self.inner.num_senders.load(SeqCst), so capacity correctly accounts for the number of active senders.mpsc/sink_impl.rs: SimplifiedSender::poll_flushto always returnPoll::Ready(Ok(())). The previous implementation forwardedpoll_ready's result, which incorrectly blocked flush on channel capacity. Sincestart_sendimmediately enqueues messages into the shared queue, there is nothing to flush.tests/mpsc.rs: Adjusted buffer sizes in existing tests to match the corrected semantics, removed a test that becomes obsolete with this patch, and added two new tests (send_recv_buffer_zeroandsend_recv_buffer_zero_async) that verify the dynamicbuffer + num_senderscapacity through bothtry_sendand asyncSinkpaths, including when senders are cloned at runtime.