Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blocking::mpsc): add Receiver::recv(_ref)_timeout methods #75

Merged
merged 5 commits into from
Apr 26, 2023

Conversation

utkarshgupta137
Copy link
Contributor

@utkarshgupta137 utkarshgupta137 commented Mar 27, 2023

Added recv_timeout methods for blocking::mpsc::Receiver/StaticReceiver. It requires a simple change from thread::park to thread::park_timeout & an elapsed time check.
We need this functionality so that our threads can wake up at least once every few seconds to check for control events

@utkarshgupta137
Copy link
Contributor Author

@hawkw Any chance of getting this merged?

@hawkw
Copy link
Owner

hawkw commented Apr 26, 2023

@utkarshgupta137 whoops, sorry, this one slipped past me. Let me take a look!

Copy link
Owner

@hawkw hawkw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this looks good to me! I had some suggestions for potentially changing the documentation to be closer to the documentation for other methods in this module.

src/mpsc/blocking.rs Outdated Show resolved Hide resolved
src/mpsc/blocking.rs Outdated Show resolved Hide resolved
src/mpsc/blocking.rs Outdated Show resolved Hide resolved
src/mpsc/blocking.rs Outdated Show resolved Hide resolved
src/mpsc/blocking.rs Show resolved Hide resolved
Co-authored-by: Eliza Weisman <[email protected]>
Copy link
Owner

@hawkw hawkw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops i messed up some of the docs suggestions a little

src/mpsc/blocking.rs Outdated Show resolved Hide resolved
src/mpsc/blocking.rs Outdated Show resolved Hide resolved
@hawkw hawkw enabled auto-merge (squash) April 26, 2023 19:36
@hawkw
Copy link
Owner

hawkw commented Apr 26, 2023

@utkarshgupta137 thanks for the PR, happy to merge it once CI passes! are you interested in adding send_timeout methods as well?

Also, we should probably update the README to no longer state that send_timeout methods won't be implemented, if we add them :)

thingbuf/README.md

Lines 76 to 79 in a0e5740

- **You need a blocking channel with `send_timeout`** or **a blocking channel
with a `select` operation**. I'm probably not going to implement these things.
The blocking channel isn't particularly important to me compared to the async
channel, and I _probably_ won't add a bunch of additional APIs to it.

auto-merge was automatically disabled April 26, 2023 19:40

Head branch was pushed to by a user without write access

@hawkw
Copy link
Owner

hawkw commented Apr 26, 2023

It looks like the loom tests are currently failing because loom's simulated version of std::thread doesn't have a mocked park_timeout function: https://github.com/hawkw/thingbuf/actions/runs/4812666947/jobs/8568203063?pr=75

Let's add #[cfg(not(all(test, loom))] to the recv_timeout methods to fix the compiler errors for loom tests?

@utkarshgupta137
Copy link
Contributor Author

@utkarshgupta137 thanks for the PR, happy to merge it once CI passes! are you interested in adding send_timeout methods as well?

Also, we should probably update the README to no longer state that send_timeout methods won't be implemented, if we add them :)

thingbuf/README.md

Lines 76 to 79 in a0e5740

- **You need a blocking channel with `send_timeout`** or **a blocking channel
with a `select` operation**. I'm probably not going to implement these things.
The blocking channel isn't particularly important to me compared to the async
channel, and I _probably_ won't add a bunch of additional APIs to it.

Sure, I'll try to add them, but let's release this first since I actually need these :)

@hawkw hawkw enabled auto-merge (squash) April 26, 2023 20:02
@hawkw hawkw merged commit b57ce88 into hawkw:main Apr 26, 2023
@utkarshgupta137
Copy link
Contributor Author

Sweet! Looking forward to a new release.

hawkw added a commit that referenced this pull request Apr 26, 2023
<a name=""></a>
##  (2023-04-26)

#### Features

* **blocking::mpsc:**  add `Receiver::recv(_ref)_timeout` methods (#75)
  ([b57ce88](b57ce88))
hawkw added a commit that referenced this pull request Apr 26, 2023
<a name=""></a>
##  (2023-04-26)

#### Features

* **blocking::mpsc:**  add `Receiver::recv(_ref)_timeout` methods (#75)
  ([b57ce88](b57ce88))
@hawkw
Copy link
Owner

hawkw commented Apr 26, 2023

Hmm, it looks like the doctests that were added in this PR are not deterministic --- they sometimes fail spuriously (presumably based on OS scheduler behavior): https://github.com/hawkw/thingbuf/actions/runs/4813223393/jobs/8569477594#step:4:194

Can we fix this? I'd prefer to have examples that don't involve multiple threads being scheduled in the right order --- perhaps we can base our examples on std::sync::mpsc's recv_timeout examples?

@utkarshgupta137
Copy link
Contributor Author

utkarshgupta137 commented Apr 26, 2023

Hmm, it looks like the doctests that were added in this PR are not deterministic --- they sometimes fail spuriously (presumably based on OS scheduler behavior): https://github.com/hawkw/thingbuf/actions/runs/4813223393/jobs/8569477594#step:4:194

I think just increasing the durations would fix the issue?

Can we fix this? I'd prefer to have examples that don't involve multiple threads being scheduled in the right order --- perhaps we can base our examples on std::sync::mpsc's recv_timeout examples?

Do you mean creating 2 different examples?

@hawkw
Copy link
Owner

hawkw commented Apr 26, 2023

Can we fix this? I'd prefer to have examples that don't involve multiple threads being scheduled in the right order --- perhaps we can base our examples on std::sync::mpsc's recv_timeout examples?

Do you mean creating 2 different examples?

Yes, the other thing I would note is that the standard library's examples never actually spawn an additional thread. Instead, it demonstrates the timeout behavior by trying to receive from a channel that will never have a message sent to it. I would kind of prefer to write our examples this way, because it's more deterministic and the doctests should never spuriously fail depending on scheduler behavior.

@utkarshgupta137
Copy link
Contributor Author

Can we fix this? I'd prefer to have examples that don't involve multiple threads being scheduled in the right order --- perhaps we can base our examples on std::sync::mpsc's recv_timeout examples?

Do you mean creating 2 different examples?

Yes, the other thing I would note is that the standard library's examples never actually spawn an additional thread. Instead, it demonstrates the timeout behavior by trying to receive from a channel that will never have a message sent to it. I would kind of prefer to write our examples this way, because it's more deterministic and the doctests should never spuriously fail depending on scheduler behavior.

But they're spawning additional threads & depending on the scheduler behavior...
They just have longer durations.

Success

thread::spawn(move || {
    send.send('a').unwrap();
});

assert_eq!(
    recv.recv_timeout(Duration::from_millis(400)),
    Ok('a')
);

Failure:

thread::spawn(move || {
    thread::sleep(Duration::from_millis(800));
    send.send('a').unwrap();
});

assert_eq!(
    recv.recv_timeout(Duration::from_millis(400)),
    Err(mpsc::RecvTimeoutError::Timeout)
);

@hawkw
Copy link
Owner

hawkw commented Apr 26, 2023

But they're spawning additional threads & depending on the scheduler behavior...
They just have longer durations.

...yup, you're right, I can't read, apparently. I think increasing the sleep duration to at least twice the timeout is probably sufficient, then!

hawkw added a commit that referenced this pull request Apr 26, 2023
<a name=""></a>
##  (2023-04-26)

#### Features

* **blocking::mpsc:**  add `Receiver::recv(_ref)_timeout` methods (#75)
  ([b57ce88](b57ce88))
hawkw added a commit that referenced this pull request Apr 26, 2023
<a name=""></a>
##  (2023-04-26)

#### Features

* **blocking::mpsc:**  add `Receiver::recv(_ref)_timeout` methods (#75)
  ([b57ce88](b57ce88))
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.

2 participants