Integrated: 8305748: Clarify reentrant behavior of close() in FileInputStream, FileOutputStream, and RandomAccessFile
Archie Cobbs
acobbs at openjdk.org
Wed May 10 16:27:31 UTC 2023
On Thu, 6 Apr 2023 22:36:33 GMT, Archie Cobbs <acobbs at openjdk.org> wrote:
> IO stream classes like `FileOutputStream` can have assocated NIO channels.
>
> When `close()` is invoked on one of these classes, it in turn invokes `close()` on the associated channel (if any). But when the associated channel's `close()` method is invoked, it in turn invokes `close()` on the associated stream (if any).
>
> As a result, these IO stream `close()` methods invoke themselves reentrantly when there is an associated channel.
>
> This is not a problem for these classes because they are written to handle this (i.e., they are idempotent), but it can be surprising (or worse, just silently bug-inducing) for subclasses that override `close()`.
>
> There are two possible ways to address this:
> 1. Modify the code to detect and avoid the (unnecessary) reentrant invocations
> 2. Add a `@implNote` to the Javadoc so subclass implementers are made aware
>
> This patch takes the second, more conservative approach.
This pull request has now been integrated.
Changeset: 0198afca
Author: Archie Cobbs <acobbs at openjdk.org>
Committer: Brian Burkhalter <bpb at openjdk.org>
URL: https://git.openjdk.org/jdk/commit/0198afca3ac1a7c421b0669ae2180eee3e4f1482
Stats: 17 lines in 4 files changed: 16 ins; 0 del; 1 mod
8305748: Clarify reentrant behavior of close() in FileInputStream, FileOutputStream, and RandomAccessFile
Reviewed-by: alanb, bpb
-------------
PR: https://git.openjdk.org/jdk/pull/13379
More information about the core-libs-dev
mailing list