Skip to content

Comments

Set current_time to absolute system time to prevent drift#171

Open
cb341 wants to merge 1 commit intolucaspoffo:masterfrom
cb341:fix-time-drift-on-update
Open

Set current_time to absolute system time to prevent drift#171
cb341 wants to merge 1 commit intolucaspoffo:masterfrom
cb341:fix-time-drift-on-update

Conversation

@cb341
Copy link

@cb341 cb341 commented Feb 4, 2025

Prevent current time update from drifting

Current setup

The current_time gets updated by a fixed amount every system tick. This is error prone especially for long running sessions, like described in #157.

Proposal

Instead of having the delay be updated by an relative amount, the current_time should be set to an absolute value in order to prevent a drift of the internal clock.

In addition to setting the current_time at the beginning of the session, the current_time should be updated on every tick accordingly.

Problems / Considerations

  • Maybe there are reasons as to why the client and server clocks should not stay in sync.
  • Maybe there are situations where it is expected for the duration to be set manually.

Alternatives

  • Introduce a flag like auto_sync which periodically updates the client and server time to current system time, to avoid drifting.

fn update_internal_state(&mut self, duration: Duration) -> Result<(), NetcodeError> {
self.current_time += duration;
fn update_internal_state(&mut self) -> Result<(), NetcodeError> {
self.current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
Copy link
Author

Choose a reason for hiding this comment

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

This is probably not the best approach.

Instead there should be some method to synchronize the clocks.
Otherwise it is not possible to set a custom delay.

Copy link
Author

Choose a reason for hiding this comment

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

Perhaps the current_time should be synchronized every so often by passing in a special duration.

@cb341 cb341 marked this pull request as ready for review February 4, 2025 13:55
@cb341 cb341 force-pushed the fix-time-drift-on-update branch from 4f23740 to e9258be Compare February 4, 2025 13:59
@cb341
Copy link
Author

cb341 commented Aug 13, 2025

@lucaspoffo, could you please take a look at this PR?

@cb341
Copy link
Author

cb341 commented Oct 1, 2025

Any update @lucaspoffo ?

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.

1 participant