Skip to content

Got "overflow when subtracting duration from instant" when "adapter.get_config()" #11

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

Closed
Effect-Wei opened this issue Apr 1, 2024 · 6 comments · Fixed by #12
Closed

Comments

@Effect-Wei
Copy link

Hello, I got thread panicked when running adapter.get_config()
I'm using Windows 11 and rustc 1.77.1 (7cf61ebde 2024-03-27)
Maybe similar to this issue?

Here's the stacktrace:

thread '<unnamed>' panicked at library\std\src\time.rs:433:33:
overflow when subtracting duration from instant
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/7cf61ebde7b22796c69757901dd346d0fe70bd97/library\std\src\panicking.rs:647
   1: core::panicking::panic_fmt
             at /rustc/7cf61ebde7b22796c69757901dd346d0fe70bd97/library\core\src\panicking.rs:72
   2: core::panicking::panic_display
             at /rustc/7cf61ebde7b22796c69757901dd346d0fe70bd97/library\core\src\panicking.rs:196
   3: core::panicking::panic_str
             at /rustc/7cf61ebde7b22796c69757901dd346d0fe70bd97/library\core\src\panicking.rs:171
   4: core::option::expect_failed
             at /rustc/7cf61ebde7b22796c69757901dd346d0fe70bd97/library\core\src\option.rs:1988
   5: core::option::Option::expect
             at /rustc/7cf61ebde7b22796c69757901dd346d0fe70bd97/library\core\src\option.rs:894
   6: std::time::impl$3::sub
             at /rustc/7cf61ebde7b22796c69757901dd346d0fe70bd97/library\std\src\time.rs:434
   7: wireguard_nt::adapter::Adapter::get_config
             at C:\Users\name\.cargo\registry\src\mirrors.tuna.tsinghua.edu.cn-2eab394af869c8a2\wireguard-nt-0.3.0\src\adapter.rs:576
   8: my_code::wg_connect::closure$0
             at .\src\main.rs:191
   9: core::hint::black_box
             at /rustc/7cf61ebde7b22796c69757901dd346d0fe70bd97\library\core\src\hint.rs:334
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Related part of my code:

    let _thread = std::thread::spawn(move || {
        std::thread::sleep(Duration::from_secs(1));
        'outer: loop {
            for _ in 0..5 {
                if stop.load(Ordering::Relaxed) {
                    drop(adapter);
                    break 'outer;
                }
                std::thread::sleep(Duration::from_millis(200));
            }
            let stats = adapter.get_config(); //*** Line 191 ***
            for peer in stats.peers {
                //let handshake_age = Instant::now().saturating_duration_since(peer.last_handshake);
                let handshake_age = Duration::from_secs(0);
                window
                    .emit(
                        "wg-statistics",
                        WgStatistics {
                            up: peer.tx_bytes,
                            down: peer.rx_bytes,
                            handshake_age: handshake_age.as_secs(),
                        },
                    )
                    .unwrap();
                println!(
                    "  {:?}, up: {}, down: {}, handshake: {:.1}s ago",
                    peer.allowed_ips,
                    peer.tx_bytes,
                    peer.rx_bytes,
                    handshake_age.as_secs()
                );
            }
        }
    });
@TroyNeubauer
Copy link
Collaborator

Good catch, thanks for filing!

Using SystemTime and checked_sub should fix this, will write a PR shortly.

I'm curious what your time setting is? Assuming its normal (April 2024?)

@Effect-Wei
Copy link
Author

Is's April 2, 2024 23:04 (UTC+8) on my computer and server. This is the right time.

I also did some experiments and found that this may be because I adapter.get_config() before the first handshake. For example:

connect();
sleep(10s);
adapter.get_config()
print(handshakeAge);

It always give Last handshake: 5s ago instead of 10s and no more panic.

Hope this information helps.

(And sorry for my bad English)

@TroyNeubauer
Copy link
Collaborator

I have a possible fix out in #12. I was unable to replicate the panic, so could you test the fix-handshake-time-panic branch for me?

Also I changed WireguardPeer::last_handshake to be a SystemTime to better match the C code, which will be a breaking change

@Effect-Wei
Copy link
Author

(Sorry for replying so late)

This fix works! The same code (with necessary changes) will no longer panic.

Now it gives:

handshake: 1712415051s ago    // about 54 years ago, that's 1970
handshake: 1712415052s ago
handshake: 1712415053s ago
handshake: 1712415054s ago
handshake: 1712415055s ago
handshake: 0s ago             // first handshake
handshake: 1s ago
...

I guess I can change my code to show "N/As ago" or whatever. It doesn't matter.

Thanks for your efforts!

@TroyNeubauer
Copy link
Collaborator

No worries, I'll change last_handshake to be an Option<SystemTime> to make this more clear and upload a new version soon!

@TroyNeubauer
Copy link
Collaborator

Just released v0.4.0.

Btw your english is good!

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 a pull request may close this issue.

2 participants