Skip to content

Make FileBackedOutputStream use PhantomReference instead of finalize().#8228

Merged
copybara-service[bot] merged 1 commit into
masterfrom
test_874257301
Feb 26, 2026
Merged

Make FileBackedOutputStream use PhantomReference instead of finalize().#8228
copybara-service[bot] merged 1 commit into
masterfrom
test_874257301

Conversation

@copybara-service
Copy link
Copy Markdown
Contributor

Make FileBackedOutputStream use PhantomReference instead of finalize().

This generally makes it more resilient and future-proof, but we still don't recommend relying on GC for object cleanup, in part because any small change (like this one) could upset a delicate balance somewhere.

Also, while I'm messing with reachabilityFence again: Remove a confused comment from 7eba58f in GcFinalizationTest: Once we use reachabilityFence directly, we'll eliminate the helper method, and http://errorprone.info/bugpattern/ReachabilityFenceUsage will remain happy because the call to reachabilityFence will appear directly inside a finally block. (I'm still not sure that finally is necessary there, but it's easy, and it's probably reasonable as a default practice.)

(I'm also not sure if finally is necessary for the new usages of reachabilityFence in FileBackedOutputStream. I guess the fear would be that we'd allow the FileOutputStream to be closed while it's in the process of preparing to throw an exception, and the close call could clear something that the exception-construction code wants to read? I do suspect that the new reachabilityFence calls are a good idea to have somewhere, whether in a finally block or not, to ensure that background cleanup doesn't reset a FileBackedOutputStream during a call to a method like write (which might then open a new file) or close (which might then not see an exception because the background reset sees it instead). Or maybe reachabilityFence isn't necessary because our methods are synchronized. (synchronized should at least mean that we don't need the fence in a finally block, at least not for the reasons that I mentioned above: It won't be possible to reset the stream until write or close exits the critical area.) But presumably it won't hurt anything.)

RELNOTES=n/a

@copybara-service copybara-service Bot force-pushed the test_874257301 branch 5 times, most recently from 502e32d to c43156e Compare February 26, 2026 15:25
…lize()`.

This generally makes it more resilient and future-proof, but we still don't recommend relying on GC for object cleanup, in part because any small change (like this one) could upset a delicate balance somewhere.

Also, while I'm messing with `reachabilityFence` again: Remove a confused comment from 7eba58f in `GcFinalizationTest`: Once we use `reachabilityFence` directly, we'll eliminate the helper method, and http://errorprone.info/bugpattern/ReachabilityFenceUsage will remain happy because the call to `reachabilityFence` will appear directly inside a `finally` block. (I'm still not sure that `finally` is necessary there, but it's easy, and it's probably reasonable as a default practice.)

(I'm also not sure if `finally` is necessary for the new usages of `reachabilityFence` in `FileBackedOutputStream`. I guess the fear would be that we'd allow the `FileOutputStream` to be closed while it's in the process of preparing to throw an exception, and the `close` call could clear something that the exception-construction code wants to read? I do suspect that the new `reachabilityFence` calls are a good idea to have _somewhere_, whether in a `finally` block or not, to ensure that background cleanup doesn't `reset` a `FileBackedOutputStream` during a call to a method like `write` (which might then open a _new_ file) or `close` (which might then not see an exception because the background `reset` sees it instead). Or maybe [`reachabilityFence` isn't necessary because our methods are `synchronized`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/ref/Reference.html#reachabilityFence(java.lang.Object)). (`synchronized` should at least mean that we don't need the fence in a `finally` block, at least not for the reasons that I mentioned above: It won't be possible to `reset` the stream until `write` or `close` exits the critical area.) But presumably it won't hurt anything.)

RELNOTES=n/a
PiperOrigin-RevId: 875720195
@copybara-service copybara-service Bot merged commit 7c6b17c into master Feb 26, 2026
@copybara-service copybara-service Bot deleted the test_874257301 branch February 26, 2026 15:49
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