Skip to content

Fix StopWatch's stopInstant match stopTimeNanos when split is called#1610

Merged
garydgregory merged 2 commits intoapache:masterfrom
larshelge:master
Mar 4, 2026
Merged

Fix StopWatch's stopInstant match stopTimeNanos when split is called#1610
garydgregory merged 2 commits intoapache:masterfrom
larshelge:master

Conversation

@larshelge
Copy link
Contributor

@larshelge larshelge commented Mar 4, 2026

This PR improves consistency in StopWatch. In StopWatch two variables are used to represent the stop time, using long/milliseconds and Instant respectively, to prepare for Java 8 removal.

For most operations including start, stop and suspend, the two variables are set together to the approximately same time.

The exception is for the split operation, where only the Instant variable is set. This is inconsistent and is problematic when you extend the StopWatch class with a custom class, as the long/milliseconds variable is private and not accessible from the outside, while the Instant variable is the only accessible variable through the getStopInstant. The getStopTime method delegates to getStopInstant. Calling getStopTime after split is called, without also calling stop, is causing a nullpointer exception.

No AI was used for this PR.

@larshelge larshelge changed the title fix: Make stopInstant in sync with stopTimeNanos fix: Make stopInstant be in sync with stopTimeNanos for split Mar 4, 2026
@larshelge
Copy link
Contributor Author

There is a CI test failure but it appears to be unrelated.

@garydgregory
Copy link
Member

Yep, this is a "random" and recurring issue. I'll take a look at any side effects for the PR later today or tomorrow.

@garydgregory garydgregory merged commit aa47951 into apache:master Mar 4, 2026
71 of 84 checks passed
@garydgregory garydgregory changed the title fix: Make stopInstant be in sync with stopTimeNanos for split Fix StopWatch's stopInstant match stopTimeNanos when split is called Mar 4, 2026
@garydgregory
Copy link
Member

I merged the PR. TY @larshelge 👍

@larshelge
Copy link
Contributor Author

Great, really appreciate the quick response @garydgregory!

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