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

Doesn't work on Windows if process exits shortly after calling open::that #12

Closed
kuviman opened this issue Oct 11, 2017 · 7 comments
Closed

Comments

@kuviman
Copy link

kuviman commented Oct 11, 2017

Hello,

I am basically doing this:

extern crate open;
fn main() {
    assert!(open::that("http://localhost").unwrap().success());
    std::thread::sleep_ms(1000);
}

So, if the sleep is removed, nothing is being opened.

@Byron
Copy link
Owner

Byron commented Nov 19, 2017

Thanks for letting me know. Unfortunately I don't have any clue how that could be fixed. Maybe someone with access to a windows machine can chime in.

@Fanaen
Copy link

Fanaen commented Feb 10, 2018

Interestingly, it seems the problem happens only when running the program through Cargo. I am currently building a CLI app and I ran a few tests on my Windows 10 :

  1. cargo run doesn't open the browser without thread::sleep_ms
  2. cargo run does open the browser with thread::sleep_ms and at least 50 as param
  3. I tried multiple creation_flags (Windows specifics) and different output methods but none of them changed the outcome.
  4. cargo build then .\target\release\projectname.exe does open the browser even without the thread::sleep_ms

My knowledge of the Cargo + Windows environment is not enough to explain why it behaves like this. As for the fix, in the meantime, adding in the readme a simple warning about that very specific situation should be enough.

@Byron
Copy link
Owner

Byron commented Feb 24, 2018

@Fanaen Thanks a lot for the summary! Do you think you could drop a PR which adds the information you would find helpful? Maybe as some sort of 'Caveats' section.

@kuviman
Copy link
Author

kuviman commented May 31, 2018

I guess the reason is that start spawns child process, that is still running while the main program has exited already. For some reason cargo run kills it after exit.

Here is an example too see it more clearly:

  1. create a binary sleep-hello.exe like this:
fn main() {
    std::thread::sleep_ms(3000);
    println!("Hello");
}
  1. create a binary open-test.exe:
extern crate open;
fn main() {
    open::that("sleep-hello.exe");
    std::thread::sleep_ms(1000);
}

You will see that there is a cmd.exe instance running for only a second, and killed when cargo exits. Running without cargo (.\target\release\open-test.exe) works ok: open-test.exe exits after a second, but cmd.exe window is not killed and runs for 2 seconds more, as expected.

Back to the http://localhost example, try running that one with your browser closed before you run it. It doesn't matter what the sleep duration is, browser will still be closed after cargo exits.

@kuviman
Copy link
Author

kuviman commented May 31, 2018

I've opened an issue in cargo repo: rust-lang/cargo#5598

@Seeker14491
Copy link
Contributor

This issue has been fixed; the given code works for me with or without the sleep.

@kuviman
Copy link
Author

kuviman commented Nov 6, 2018

Indeed, works now

@kuviman kuviman closed this as completed Nov 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants