Skip to content

Conversation

@JohnDoneth
Copy link
Contributor

@JohnDoneth JohnDoneth commented Aug 14, 2019

Duplicate of #1444 but for master instead of the v0.1.x branch.

Should (fix) #1443 on master.

UdpFramed now attempts to continue decoding past the first discrete item if there is more data in the read buffer. I added a test for this that sends a packet with 3 lines and tests for the successful decoding of each item in turn.

@JohnDoneth JohnDoneth changed the title udp: Fix UdpFramed with regards to Decode udp: Fix UdpFramed with regard to Decode Aug 14, 2019
@JohnDoneth JohnDoneth changed the title udp: Fix UdpFramed with regard to Decode udp: Fix UdpFramed with regards to Decode Aug 15, 2019
@LucioFranco
Copy link
Member

@JohnDoneth thanks for doing this, I'm gonna review #1444 then when that commit is approved we can port the fix into master.

@ipetkov
Copy link
Member

ipetkov commented Aug 31, 2019

@LucioFranco I think this is ready for review now that #1444 has been merged!

@ipetkov ipetkov requested a review from LucioFranco August 31, 2019 19:00
@LucioFranco
Copy link
Member

Yeah, we need to update this to the new method done in #1444 that uses decode_eof instead of just plain decode.

@JohnDoneth JohnDoneth closed this Sep 6, 2019
@JohnDoneth JohnDoneth force-pushed the fix-udp-codec-decode-0.2 branch from ca58f91 to 9b3f856 Compare September 6, 2019 14:25
@JohnDoneth JohnDoneth reopened this Sep 6, 2019
Ralith
Ralith previously requested changes Nov 27, 2019
Copy link
Contributor

@Ralith Ralith left a comment

Choose a reason for hiding this comment

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

Looks like there's still pending work on this.

@JohnDoneth
Copy link
Contributor Author

JohnDoneth commented Nov 28, 2019

I am working on this again. Currently stuck on this API change where BytesMut returns a slice of possibly uninitialized bytes.

error[E0308]: mismatched types
  --> tokio-util\src\udp\frame.rs:72:79
   |
72 |                 let res = ready!(Pin::new(&mut pin.socket).poll_recv_from(cx, pin.rd.bytes_mut()));
   |                                                                               ^^^^^^^^^^^^^^^^^^ expected u8, found union `std::mem::MaybeUninit`
   |
   = note: expected type `&mut [u8]`
              found type `&mut [std::mem::MaybeUninit<u8>]`

Edit: Ah, I think I got it. Just index it.

Edit 2: That didn't work either. In the end I used this bit of code I found elsewhere in the project.

let b = &mut *(pin.rd.bytes_mut() as *mut [MaybeUninit<u8>] as *mut [u8]);

@JohnDoneth JohnDoneth requested a review from Ralith November 28, 2019 01:31
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-net Module: tokio/net labels Apr 20, 2020
@Darksonn
Copy link
Contributor

I was wondering what the status on this is?

@JohnDoneth
Copy link
Contributor Author

@Darksonn It was ready to be reviewed. It now needs to be caught up to master again

@Darksonn
Copy link
Contributor

Sorry about that. If you merge master again I'll make sure someone actually reviews it :)

@LucioFranco
Copy link
Member

@JohnDoneth sorry about the delay, but anyway you could get this rebased and we can get it merged?

@JohnDoneth
Copy link
Contributor Author

I will put it on my todo list!

Copy link
Member

@LucioFranco LucioFranco left a comment

Choose a reason for hiding this comment

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

Thanks!

@LucioFranco
Copy link
Member

@JohnDoneth looks like some clippy and rustfmt failures?

@JohnDoneth
Copy link
Contributor Author

I think it might be CI? cargo clippy doesn't produce any output locally and cargo fmt leaves the project unchanged.

@Darksonn
Copy link
Contributor

The clippy error is the following. You probably forgot an --all-features on the clippy invocation.

error: transmute from a reference to a reference
  --> tokio-util/src/udp/frame.rs:71:36
   |
71 |                 let b: &mut [u8] = std::mem::transmute(pin.rd.bytes_mut());
   |                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(pin.rd.bytes_mut() as *mut [std::mem::MaybeUninit<u8>] as *mut [u8])`
   |
   = note: `-D clippy::transmute-ptr-to-ptr` implied by `-D warnings`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ptr

As for rustfmt, it's pretty confusing but you have to use the following command in Tokio's repositories. This has to do with our extensive use of features together with the use of macros to enable/disable them, which break rustfmt in a way that means it cannot find all the files.

rustfmt --edition 2018 $(find . -name '*.rs' -print)

Check out our contributing guide for more info.

@JohnDoneth JohnDoneth force-pushed the fix-udp-codec-decode-0.2 branch from 909d63d to 4c58d8a Compare July 22, 2020 23:28
@JohnDoneth
Copy link
Contributor Author

@Darksonn Thanks! That was the problem.

I believe this PR is ready now.

ping @LucioFranco

@LucioFranco LucioFranco merged commit 94b64cd into tokio-rs:master Jul 23, 2020
@LucioFranco
Copy link
Member

@JohnDoneth thanks a ton for following up!

@JohnDoneth JohnDoneth deleted the fix-udp-codec-decode-0.2 branch July 23, 2020 15:30
@Darksonn Darksonn added A-tokio-util Area: The tokio-util crate and removed A-tokio Area: The main tokio crate labels Nov 10, 2020
@kurnevsky
Copy link

Looks like this change makes decoding errors useless. If we return error from decode function then we will try to parse the same bytes from the same udp packet and will get the same error, and so on ad infinitum. @JohnDoneth was it expected? Should we put decoding errors to the Item type now?

@Darksonn
Copy link
Contributor

Darksonn commented Jan 2, 2021

Ah, that sounds like a mistake. Consider opening a PR to fix that.

@kurnevsky
Copy link

Looked through the implementation once again and realized that we have to clear the buffer when packet was parsed, and then have to handle empty input as None. So we could do the same when error happens actually.

@kurnevsky
Copy link

But then we won't be able to distinguish zero sized packets from completely consumed packets. Probably we should check for buffer size here https://github.com/tokio-rs/tokio/blob/master/tokio-util/src/udp/frame.rs#L63 ? And then do something similar for errors?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tokio-util Area: The tokio-util crate M-net Module: tokio/net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants