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

feat(hydroflow)!:Change current_tick_start to wall clock time #1196

Merged
merged 3 commits into from
May 17, 2024

Conversation

rohitkulshreshtha
Copy link
Contributor

Addresses #1187.

Wall-clock time isn't monotonic, but that's expected to be understood.

Copy link

cloudflare-workers-and-pages bot commented May 14, 2024

Deploying hydroflow with  Cloudflare Pages  Cloudflare Pages

Latest commit: 1a635a7
Status: ✅  Deploy successful!
Preview URL: https://3c88fce3.hydroflow.pages.dev
Branch Preview URL: https://rohit-hf-1187.hydroflow.pages.dev

View logs

@rohitkulshreshtha
Copy link
Contributor Author

The WASM SystemTime implementation through the instant crate does the following for duration_since:

 pub fn duration_since(&self, earlier: SystemTime) -> Result<Duration, ()> {
        let dur_ms = self.0 - earlier.0;
        if dur_ms < 0.0 {
            return Err(());
        }
        Ok(Duration::from_millis(dur_ms as u64))
    }
  1. The implementation in instant doesn't seem to be a drop-in replacement for std::time - std::time returns a Duration in Err(Duration) to indicate how far behind the time went.
  2. The failing test fails every time, which means the time goes backward each time.
  3. The failing test, as written, is flaky and can benefit from a pluggable, deterministic clock.

@MingweiSamuel
Copy link
Member

MingweiSamuel commented May 15, 2024

Let's use https://crates.io/crates/web-time instead of instant (which is out of date)

#[cfg(not(target_family = "wasm"))]
pub use std::time::SystemTime;
#[cfg(target_family = "wasm")]
pub use web_time::SystemTime;

@rohitkulshreshtha
Copy link
Contributor Author

This isn't a choice-of-library issue. My main point is that evaluating that some wall-clock time has elapsed after defer_tick is always going to be flaky. It worked in the past because performance counters were being used.

@MingweiSamuel
Copy link
Member

MingweiSamuel commented May 17, 2024

Oh! it seems the code in instant is actually just completely broken: sebcrozet/instant#49

https://github.com/sebcrozet/instant/blob/master/src/wasm.rs#L201-L203

self.duration_since(SystemTime::now())

Is backwards and would always be negative if time goes forward. Should be

Self::now().duration_since(*self)

Should use web-time regardless

@rohitkulshreshtha
Copy link
Contributor Author

Oh. I didn't know it had the check completely backward. I suspected it's a millisecond-grained timestamp that makes equal timestamp fails (so >= or <= in a check somewhere instead of < or >).

I agree with replacing it. The test will start passing most of the time with a correct implementation. I can then look at pluggable clocks independently.

@MingweiSamuel
Copy link
Member

Yeah I knew to prefer web-time but I didn't realize how busted instant was until just now looking thru the issues 😭

Copy link
Member

@MingweiSamuel MingweiSamuel left a comment

Choose a reason for hiding this comment

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

Nice!

use tokio::sync::mpsc::UnboundedSender;
use tokio::task::JoinHandle;
use web_time::SystemTime;
Copy link
Member

Choose a reason for hiding this comment

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

Oh actually may want to target flag this, I think it's always different than std, even on regular builds

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the library target flags this internally.

@MingweiSamuel
Copy link
Member

I guess maybe your original bug cause may also be true?

@rohitkulshreshtha
Copy link
Contributor Author

Yup. It passes on my laptop. It may succeed if the test is re-run. Disabling auto-merge.

@rohitkulshreshtha rohitkulshreshtha force-pushed the rohit_hf_1187 branch 3 times, most recently from 963c298 to 95a4331 Compare May 17, 2024 20:18
Addresses #1187.

Wall-clock time isn't monotonic, but that's expected to be understood.
Switched to using `web-time` crate instead of `instant`.
Removing a unit-test: Discussed with Mingwei and it is not super important.
@rohitkulshreshtha rohitkulshreshtha enabled auto-merge (squash) May 17, 2024 20:28
@rohitkulshreshtha rohitkulshreshtha merged commit 218175c into main May 17, 2024
13 checks passed
@rohitkulshreshtha rohitkulshreshtha deleted the rohit_hf_1187 branch May 21, 2024 16:44
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