Skip to content

[test] Convert select and poll tests to using clock_gettime#26401

Open
brendandahl wants to merge 2 commits intoemscripten-core:mainfrom
brendandahl:flaky-select
Open

[test] Convert select and poll tests to using clock_gettime#26401
brendandahl wants to merge 2 commits intoemscripten-core:mainfrom
brendandahl:flaky-select

Conversation

@brendandahl
Copy link
Collaborator

Prefer clock_gettime with CLOCK_MONOTONIC instead of gettimeofday
whose implementation isn't guaranteed to be monotonic.

Add some margin for error in the timeout asserts.
Normalize negative nano seconds in time delta.

…pten-core#26280)

Prefer `clock_gettime` with `CLOCK_MONOTONIC` instead of `gettimeofday`
whose implementation isn't guaranteed to be monotonic.
@brendandahl brendandahl requested review from inolen and sbc100 March 6, 2026 17:43
if (delta_nsec < 0) {
delta_nsec += 1000000000;
delta_sec -= 1;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why doesn't this work if you just allow delta_nsec to remain negative and then it will act as a subtraction below? (like this the version of this PR that I landed).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The previous version would round down. For example:
(0 * 1000) + (299999999 / 1000000) would become 299ms. In the end I don't think this change really matters much, because I've seen the duration off by as much as 17ms locally.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see so that + 500000 here is the fix to avoid the rounding down? And that means delta_nsec cannot be left as negative?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, changes it to round to nearest.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually don't we want to round down here.

i.e if wait for 999 ms we don't want report that a waiting for whole second, because a whole second has not elapsed yet.

Same with 1 millisecond + 999 microsecond .. that still is not 2ms and if 2ms was the target 1.999 is not enough time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I guess it depends how precise the clock is expected to be. The new code rounds to nearest microsecond. e.g.
999'500'000 becomes 1000ms
999'499'999 becomes 999ms

Anyway, I can revert part as it doesn't really matter since I see the duration is multiple milliseconds early locally.


#define TIMEOUT_MS 300
// It is possible for the Javascript timers (such as setTimeout or Atomics.wait)
// to wake up slightly earlier than requested. Because we measure times accurately using
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this actually true? What source did you use for this? All the specs I could find out setTimeout said it would always block for at least that number of milliseconds and never less.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure about atomic wait, but

The callback will likely not be invoked in precisely delay milliseconds. Node.js makes no guarantees about the exact timing of when callbacks will fire, nor of their ordering. The callback will be called as close as possible to the time specified.

https://nodejs.org/docs/latest/api/timers.html#settimeoutcallback-delay-args

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you perhaps call out node in particular here because I think the webplatform setTimeouts does not allow any kind of easrly callback (at least if MDN is to be believed).


#define TIMEOUT_MS 300
// It is possible for the Javascript timers (such as setTimeout or Atomics.wait)
// to wake up slightly earlier than requested. Because we measure times accurately using
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you perhaps call out node in particular here because I think the webplatform setTimeouts does not allow any kind of easrly callback (at least if MDN is to be believed).

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.

3 participants