Skip to content

Make function std::ping::ping concurrency safe#7

Closed
michael-hartmann wants to merge 1 commit into
aisk:masterfrom
michael-hartmann:concurrency-safety
Closed

Make function std::ping::ping concurrency safe#7
michael-hartmann wants to merge 1 commit into
aisk:masterfrom
michael-hartmann:concurrency-safety

Conversation

@michael-hartmann

Copy link
Copy Markdown
Contributor

The function ping so far did not check the ident received. As a consequence the function ping might returned wrong results when used simultaneously from different threads, see #6 for more details.

This commit fixes the issue by checking the ident of the reply and comparing it with the expected ident.

Changes:

  • Changed type of timeout from Option<Duration> to Duration. This avoids an additional error check in new code.
  • Receive echo in a loop until either the correct ident was received or a timeout occured.

I have tested the changes and they seem to work for me. Let me know if something still needs to be changed.

The function `ping` so far did not check the ident received. As a consequence
the function `ping` might returned wrong results when used simultaneously from
different threads, see aisk#6 for more details.

This commit fixes the issue by checking the ident of the reply and comparing it
with the expected ident.

Changes:
* Changed type of `timeout` from `Option<Duration>` to `Duration`. This avoids
  an additional error check in new code.
* Receive echo in a loop until either the correct ident was received or a
  timeout occured.
@aisk

aisk commented Mar 9, 2023

Copy link
Copy Markdown
Owner

Great work! I have only a small issue which is pointed out up here. And it should be better to add a mutiple thread test case like what you posted on #6

Comment thread src/ping.rs

return Ok(());
// if ident is not correct check if timeout is over
time_elapsed = SystemTime::now().duration_since(time_start).expect("Clock may have gone backwards");

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Since this is a library, call expect is not good here, for it may cause panic for user's process. Return an error will be good.

@michael-hartmann

Copy link
Copy Markdown
Contributor Author

Thanks for the review, sounds fine. I am currently a bit occupied, give me a couple of days to update the PR.

aisk pushed a commit that referenced this pull request Feb 24, 2024
* Make function std::ping::ping concurrency safe

The function `ping` so far did not check the ident received. As a consequence
the function `ping` might returned wrong results when used simultaneously from
different threads, see #6 for more details.

This commit fixes the issue by checking the ident of the reply and comparing it
with the expected ident.

Changes:
* Changed type of `timeout` from `Option<Duration>` to `Duration`. This avoids
  an additional error check in new code.
* Receive echo in a loop until either the correct ident was received or a
  timeout occured.

* feat: improve error handling for duration_since

* feat: improve error handling for decode & reply error

* Update src/errors.rs

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

---------

Co-authored-by: Michael Hartmann <github@speicherleck.de>
Co-authored-by: Manuel Schmidt <ms@manuel-schmidt.at>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@aisk

aisk commented Feb 24, 2024

Copy link
Copy Markdown
Owner

These commits is already merged in #19.

@aisk aisk closed this Feb 24, 2024
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