Fix #70: Add @Volatile to DefaultNetworkListener fields - #73
Open
aimkiray wants to merge 1 commit into
Open
Conversation
…read visibility connectivityManager and callback are plain var fields written in start()/stop() on Dispatchers.Main.immediate. Without @volatile there is no happens-before guarantee that writes are visible if these fields are ever accessed from a different dispatcher. Benign in the current lifecycle ordering (all access is on the main thread), but removes the latent hazard. Fixes meow-rs#70
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #70
Supersedes the
@Volatileportion of closed PR #67, which was incompatible withmainafter the Compose migration (#63).Problem
DefaultNetworkListeneris a Kotlinobject(singleton) with two mutable fields:Without
@Volatile, the Java Memory Model does not guarantee that writes made by one thread are visible to reads on another thread.Fix
Add
@Volatileto both fields:Analysis
In the current codebase, both
start()andstop()execute onDispatchers.Main.immediate(the main thread):start()is called fromVpnService.preInit()←GlobalScope.launch(Dispatchers.Main.immediate)stop()is called fromVpnService.killProcesses()←GlobalScope.launch(Dispatchers.Main.immediate)→scope.launch { }(inheritsMain.immediate)There is no cross-thread access to these fields. The
@Volatileannotation is therefore a defensive best-practice improvement rather than a fix for an observed bug. However, it removes a latent hazard if the call sites are ever refactored to use a different dispatcher.Files changed
core/.../net/DefaultNetworkListener.kt— 2 lines changed (@Volatileadded to 2 fields)