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

Implemented shutdown for UdpSocket (fixes #23194). #23214

Closed
wants to merge 1 commit into from
Closed

Implemented shutdown for UdpSocket (fixes #23194). #23214

wants to merge 1 commit into from

Conversation

aatxe
Copy link
Member

@aatxe aatxe commented Mar 9, 2015

See title. This should probably have unit tests. So, I'll add those soon (a bit busy right now, but wanted to get this out) and then squash.

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @huonw (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see CONTRIBUTING.md for more information.

@aatxe
Copy link
Member Author

aatxe commented Mar 9, 2015

r? @alexcrichton

@rust-highfive rust-highfive assigned alexcrichton and unassigned huonw Mar 9, 2015
@ghost
Copy link

ghost commented Mar 9, 2015

@aatxe Could we concoct a test in the vein of

fn shutdown_smoke() {
?

Edited: Oh, just noticed your comment in the PR description.

@alexcrichton
Copy link
Member

In addition to tests, could you also refactor the implementations of TcpStream::shutdown and UdpSocket::shutdown to share code?

@aatxe
Copy link
Member Author

aatxe commented Mar 10, 2015

It seems as though I cannot write any test for this that doesn't just yield Socket is not connected (os error 57). I'm not entirely sure this is possible. @alexcrichton asked me to ping him here on IRC to look into this more.

@alexcrichton
Copy link
Member

Hm I just wrote a small test locally that passes, it looks like shutdown should definitely be able to unblock the read side at least?

use std::net::UdpSocket;
use std::thread;
use std::os::unix::prelude::*;
fn main() {
    let b = UdpSocket::bind("0.0.0.0:3944").unwrap();

    let _t = thread::scoped(|| {
        let mut b = &b;
        println!("a");
        assert!(b.recv_from(&mut []).is_err());
        println!("here");
    });

    let fd = b.as_raw_fd();
    extern {
        fn shutdown(socket: i32, how: i32) -> i32;
        fn sleep(seconds: u32) -> u32;
    }

    unsafe {
        sleep(1);
        println!("shutting down");
        println!("{}", shutdown(fd as i32, 2));
        println!("shut down");
    }
}

@aatxe
Copy link
Member Author

aatxe commented Mar 10, 2015

I have what appears to me to be an almost identical test that's still failing with the same issue. I must be missing something here. Hopefully, you can pick it out. I tried adding sleep just as you did, but it didn't help.

    #[test]
    fn udp_shutdown_smoke() {
        each_ip(&mut |addr, _| {
            let sock = t!(UdpSocket::bind(&addr));

            let _t = thread::scoped(|| {
                let mut sock = &sock;
                assert!(sock.recv_from(&mut []).is_err());
            });

            t!(sock.shutdown(Shutdown::Read));
        })
    }

@alexcrichton
Copy link
Member

Hm, you may need to fiddle with the synchronization there to guarantee that the shutdown happens after the socket is already blocked in recv. I would expect, however, that a socket which has been shut down prevents all future reads as well, so I'm a little curious as to why you're getting an error. Could you paste the logs of the test?

@aatxe
Copy link
Member Author

aatxe commented Mar 10, 2015

I had tried adding the sleep, too. It didn't make a difference.
Here's a log of the tests: http://lpaste.net/124415

@alexcrichton
Copy link
Member

Hm interestingly enough it appears I wasn't even reading the output of my program! It also appears to return an error (of the same kind). Oddly enough it looks like it also unblocks any pending read (but not future reads).

In light of this we may not want to expose this at this time. One can indeed connect a UDP socket to another address, but we don't currently expose support for that. I think it may be best to actually hold off on this for now until a need for it arises, how does that sound?

@aatxe
Copy link
Member Author

aatxe commented Mar 10, 2015

Seems fair to me. I was wary of this to begin with because most information online seemed to indicate that shutting down a UDP socket wasn't particularly useful.

@alexcrichton
Copy link
Member

Ok, thanks anyway for looking into this @aaxte!

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.

4 participants