Skip to content

mpsc::send returns error once, then success #32114

Closed
@jsgf

Description

@jsgf

If you close the rx side of a channel pair before sending to it, then the first send will fail with a SendError as expected, but a second send will return Ok(()).

use std::sync::mpsc::{channel, SendError};

#[test]
fn doublesend() {
    let (tx, _) = channel();

    assert_eq!(tx.send(123), Ok(()));
    assert_eq!(tx.send(123), Err(SendError(123)));
    assert_eq!(tx.send(123), Err(SendError(123)));
}

Fails with:

thread 'doublesend' panicked at 'assertion failed: (left == right) (left: Err("SendError(..)"), right: Ok(()))', :7

However, if something is successfully sent before the receiver is dropped (even if it never receives the value), then it works as expected:

use std::sync::mpsc::{channel, SendError};
use std::mem;

#[test]
fn doublesend() {
    let (tx, rx) = channel();

    assert_eq!(tx.send(123), Ok(()));
    mem::drop(rx);
    assert_eq!(tx.send(123), Err(SendError(123)));
    assert_eq!(tx.send(123), Err(SendError(123)));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions