Repository navigation
Make it safe to initialize RegularImmutableList before ImmutableList in guava-android (as has already been safe in guava-jre). - #8634
Merged
Conversation
…st` in `guava-android` (as has already been safe in `guava-jre`). That initialization ordering has been impossible to trigger through public APIs since #1977. However, we've since learned of cases in the wild in which libraries use reflection to access non-public APIs for the purpose of initializing them in basically arbitrary order: - GraalVM does this if users request build-time initialization for a class directly or indirectly. - At least two systems inside Google try to eagerly initialize classes during normal runtime. - It's not hard to imagine a custom serialization framework that would do this. (Normal Java serialization, by contrast, is fine: `ImmutableList` instances are always serialized as `SerializedForm` instances, rather than as `RegularImmutableList` instances directly.) I wonder if GWT-RPC could have caused such trouble back in the day. (My quick read of our old serializer implementation suggests "no," but I didn't think about the GWT-RPC system as a whole.) Plus, we've gotten reports of what I still suspect to be a bug in someone's Android toolchain, in which a nondescript use of `ImmutableSet.asList` appeared to be optimized to skip the `ImmutableList` initialization and instead access `RegularImmutableList` directly. (This is uncommon, so it may happen only with a combination of some toolchain and a case in which `RegularImmutableList` is specialized to handle _only_ the empty-collection case. Here's [an old example](https://issues.chromium.org/issues/40051933#comment6).) Since the first two cases above are most likely to be pulling in `guava-jre`, not `guava-android`, this is likely the biggest practical danger. Beyond avoiding NPE, I believe that this CL even protects against [class-initialization deadlocks](#1977) for a system that manages to try to initialize `RegularImmutableList` first in one thread and `ImmutableList` first in another—even though, again, the latter should be "impossible": - A caller who initializes `ImmutableList` first is safe because `ImmutableList` initialization doesn't trigger `RegularImmutableList` initialization. - A caller who somehow initializes `RegularImmutableList` first is safe because it either initializes `ImmutableList` itself or safely waits for the other thread, which can complete without dependency on `RegularImmutableList`, as discussed above. That said, I would still worry about any system that manages to initialize `RegularImmutableList` first—not because of trouble with `RegularImmutableList` itself anymore but because these same kinds of trouble (both NPE-like problems and class-init deadlocks) could arise with other classes. Anyway: Other than the tests that show that this change helps (cl/967775267), this CL is the only work that I have in mind for #8617 at the moment. (Also, undo my static importing of `RegularImmutableList.EMPTY` from cl/88131198. I remain the world's biggest fan of static imports, but I acknowledge that a class-init cycle is a case that's not necessarily best swept under the rug. Not that I'd expect for most readers to have that on their radars regardless. But I am not _totally_ unreasonable. (Compare ccb126d.)) (And remove a comment about how our `subList` declarations narrow the return type. They do, but there's enough else going on that I found the comment weird when I came across it.) RELNOTES=n/a PiperOrigin-RevId: 971463557
copybara-service
Bot
force-pushed
the
test_967832688
branch
from
August 26, 2026 20:23
068258f to
5195255
Compare
copybara-service Bot
pushed a commit
to google/error-prone
that referenced
this pull request
Aug 27, 2026
This is prompted by google/guava#8617 / google/guava#8634. PiperOrigin-RevId: 972006555
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.
Make it safe to initialize
RegularImmutableListbeforeImmutableListinguava-android(as has already been safe inguava-jre).That initialization ordering has been impossible to trigger through public APIs since #1977. However, we've since learned of cases in the wild in which libraries use reflection to access non-public APIs for the purpose of initializing them in basically arbitrary order:
ImmutableListinstances are always serialized asSerializedForminstances, rather than asRegularImmutableListinstances directly.) I wonder if GWT-RPC could have caused such trouble back in the day. (My quick read of our old serializer implementation suggests "no," but I didn't think about the GWT-RPC system as a whole.)Plus, we've gotten reports of what I still suspect to be a bug in someone's Android toolchain, in which a nondescript use of
ImmutableSet.asListappeared to be optimized to skip theImmutableListinitialization and instead accessRegularImmutableListdirectly. (This is uncommon, so it may happen only with a combination of some toolchain and a case in whichRegularImmutableListis specialized to handle only the empty-collection case. Here's an old example.) Since the first two cases above are most likely to be pulling inguava-jre, notguava-android, this is likely the biggest practical danger.Beyond avoiding NPE, I believe that this CL even protects against class-initialization deadlocks for a system that manages to try to initialize
RegularImmutableListfirst in one thread andImmutableListfirst in another—even though, again, the latter should be "impossible":ImmutableListfirst is safe becauseImmutableListinitialization doesn't triggerRegularImmutableListinitialization.RegularImmutableListfirst is safe because it either initializesImmutableListitself or safely waits for the other thread, which can complete without dependency onRegularImmutableList, as discussed above.That said, I would still worry about any system that manages to initialize
RegularImmutableListfirst—not because of trouble withRegularImmutableListitself anymore but because these same kinds of trouble (both NPE-like problems and class-init deadlocks) could arise with other classes.Anyway: Other than the tests that show that this change helps (cl/967775267), this CL is the only work that I have in mind for #8617 at the moment.
(Also, undo my static importing of
RegularImmutableList.EMPTYfrom cl/88131198. I remain the world's biggest fan of static imports, but I acknowledge that a class-init cycle is a case that's not necessarily best swept under the rug. Not that I'd expect for most readers to have that on their radars regardless. But I am not totally unreasonable. (Compare ccb126d.))(And remove a comment about how our
subListdeclarations narrow the return type. They do, but there's enough else going on that I found the comment weird when I came across it.)RELNOTES=n/a