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

Fix conditions that could cause SNTPClient.blockingEpochTime to block forever #69

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ Version numbers can be found under [Releases](https://github.com/tidal-music/net
## Usage

Create your `SNTPClient` via its constructor. Its API allows you to toggle synchronization (starts
off) and to retrieve the time based on the latest successful synchronization, if any.
off) and to retrieve the time based on the latest successful synchronization.

The property that retrieves the aforementioned time is nullable as it will return `null` if no
The nullable property `epochTime` retrieves the aforementioned time will return `null` if no
synchronization has occurred successfully during the lifetime of the process and no backup file has
been specified for the `SNTPClient` instance or said file contains no valid prior synchronization
data.

As an alternative, the `blockingEpochTime` method can be used to suspend the caller until a valid
synchronization or backup restoration occurs to avoid a nullable return type.

## networktime-singletons

This is a tiny utility on top of the base artifact that extends the `SNTPClient` API by adding a new
Expand Down
1 change: 1 addition & 0 deletions changelog/1.1.1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug when accessing SNTPClient.blockingEpoch time multiple times prior to a successful sync.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class SNTPClient(
val epochTime by delegate::epochTime

/**
* The calculated epoch time, blocking until it has been calculated at least once.
* The calculated epoch time. Suspends the caller until said time has been calculated at least
* once.
*/
@ObjCName(name = "blockingEpochTimeMs")
suspend fun blockingEpochTime() = delegate.blockingEpochTime()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ constructor(
suspend fun blockingEpochTime(): Duration {
epochTime?.let { return it }
synchronizationResultProcessor.firstSynchronizationLock
.lock()
.apply {
lock()
unlock()
}
return epochTime!!
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ internal class SynchronizationResultProcessor(
return value
}
set(value) {
val oldValue = mutableState.synchronizationResult
mutableState.synchronizationResult = value
firstSynchronizationLock.unlock()
if (oldValue == null && value != null) {
firstSynchronizationLock.unlock()
}
if (backupFilePath != null && value != null) {
try {
fileSystem.write(backupFilePath) {
Expand Down
Loading