Skip to content

8277585: Remove the terminally deprecated finalize() method from javax.imageio.stream APIs #26650

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

Closed
wants to merge 4 commits into from

Conversation

prrace
Copy link
Contributor

@prrace prrace commented Aug 5, 2025

This PR removes javax/imageio/stream/ImageInputStreamImpl.finalize()
As a result, sub-classes which over-ride it to be empty no longer need to do so.
Also it means that the 2 remaining classes which used it no longer can.
FileCacheImageOutputStream will have its cache cleaned up by a disposer.
The impact on applications is that they, or the ImageWriter may need to call flush() IF they relied on finalization.
However that should be extremely unlikely given that finalization will happen far too late in most cases, and is
really meant to clean up internal resources.
The JDK's GIF and TIFF image writers don't flush themselves, so applications which use these together with one of these caching streams would have learned this already.

The principal outside risk is to 3rd party ImageIO stream subclasses which both allocate native resources and rely on finalization as a backstop clean up in case applications forget to call close. But it will be the applications that are affected if the resource is depleted.
The risks of this will be covered in the CSR.

There's also a lengthy write up in the JBS issue.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Change requires CSR request JDK-8364778 to be approved
  • Commit message must refer to an issue

Issues

  • JDK-8277585: Remove the terminally deprecated finalize() method from javax.imageio.stream APIs (Bug - P4)
  • JDK-8364778: Remove the terminally deprecated finalize() method from javax.imageio.stream APIs (CSR)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/26650/head:pull/26650
$ git checkout pull/26650

Update a local copy of the PR:
$ git checkout pull/26650
$ git pull https://git.openjdk.org/jdk.git pull/26650/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26650

View PR using the GUI difftool:
$ git pr show -t 26650

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/26650.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 5, 2025

👋 Welcome back prr! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Aug 5, 2025

@prrace This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8277585: Remove the terminally deprecated finalize() method from javax.imageio.stream APIs

Reviewed-by: achung, azvegint, serb

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 134 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot changed the title 8277585 8277585: Remove the terminally deprecated finalize() method from javax.imageio.stream APIs Aug 5, 2025
@openjdk
Copy link

openjdk bot commented Aug 5, 2025

@prrace The following label will be automatically applied to this pull request:

  • client

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the rfr Pull request is ready for review label Aug 5, 2025
@mlbridge
Copy link

mlbridge bot commented Aug 5, 2025

Webrevs

@prrace
Copy link
Contributor Author

prrace commented Aug 5, 2025

/label car

@prrace
Copy link
Contributor Author

prrace commented Aug 5, 2025

/label csr

@openjdk
Copy link

openjdk bot commented Aug 5, 2025

@prrace
The label car is not a valid label.
These labels are valid:

  • graal
  • serviceability
  • hotspot
  • hotspot-compiler
  • ide-support
  • i18n
  • shenandoah
  • jdk
  • javadoc
  • security
  • hotspot-runtime
  • jmx
  • build
  • nio
  • client
  • core-libs
  • compiler
  • net
  • hotspot-gc
  • hotspot-jfr

@openjdk
Copy link

openjdk bot commented Aug 5, 2025

@prrace
The label csr is not a valid label.
These labels are valid:

  • graal
  • serviceability
  • hotspot
  • hotspot-compiler
  • ide-support
  • i18n
  • shenandoah
  • jdk
  • javadoc
  • security
  • hotspot-runtime
  • jmx
  • build
  • nio
  • client
  • core-libs
  • compiler
  • net
  • hotspot-gc
  • hotspot-jfr

@openjdk openjdk bot added the csr Pull request needs approved CSR before integration label Aug 5, 2025
return;
}
try {
cache.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice a similar nested class/method in FileCacheImageInputStream with a few differences. Does the cache.close() and cacheFile.delete need to be wrapped in null checks? And once deleted do the StreamDisposerRecord pointers (this.cacheFile and this.cache) need to be set to null like in the ImageInputStream version of the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I could re-use that. I just need to make it accessible .. but not public.
That one has null checks because it clears the vars.
My version uses a "disposed" var for the same since I made those vars final.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of final is recommended wherever possible, as the values are then guaranteed to be visible on other threads (e.g. the disposer thread).

However IIUC, values set in a DisposerRecord by a program thread should (already) be visible on the disposer thread because Disposer.add() and Disposer.run() (via records.put() and records.remove(), respectively) lock the same records Hashtable object.

private final RandomAccessFile cache;
private volatile boolean disposed;

public FileCacheDisposerRecord(File cacheFile, RandomAccessFile cache) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FileCacheDisposer makes more sense based on what the code does, but would StreamDisposerRecord be better to remain consistent for the input/output streams?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, see comment below.

* A simple pattern would be
* {@snippet lang='java':
* try (FileOutputStream fos = new FileOutputStream("out.jpg");
* (ImageOutputStream ios = new FileCacheImageOutputStream(fos, null)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is an extra left parenthesis here at the start of the line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

* } // implicit finally block closes the streams in the reverse order to opening
* }
* <p>
* Sub-classers of these Image I/O API stream types can to a limited extent protect
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commas missing here
Sub-classers of these Image I/O API stream types can, to a limited extent, protect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Member

@bchristi-git bchristi-git left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can MemoryCacheImageOutputStream be included in this PR?

@prrace
Copy link
Contributor Author

prrace commented Aug 8, 2025

Can MemoryCacheImageOutputStream be included in this PR?

Why ? It has nothing to dispose.

@bchristi-git
Copy link
Member

Can MemoryCacheImageOutputStream be included in this PR?

Why ? It has nothing to dispose.

Doesn't it have a cache, similar to MemoryCacheImageInputStream ?

public class MemoryCacheImageOutputStream extends ImageOutputStreamImpl {
....
private MemoryCache cache = new MemoryCache();

@prrace
Copy link
Contributor Author

prrace commented Aug 8, 2025

Can MemoryCacheImageOutputStream be included in this PR?

Why ? It has nothing to dispose.

Doesn't it have a cache, similar to MemoryCacheImageInputStream ?

public class MemoryCacheImageOutputStream extends ImageOutputStreamImpl { .... private MemoryCache cache = new MemoryCache();

That just wraps an ArrayList<byte[]>
I was planning on later removing the disposer from the MemoryCacheImageInputStream

@prrace
Copy link
Contributor Author

prrace commented Aug 10, 2025

The CSR is ready for review https://bugs.openjdk.org/browse/JDK-8364778

@openjdk openjdk bot added ready Pull request is ready to be integrated and removed csr Pull request needs approved CSR before integration labels Aug 15, 2025
@prrace
Copy link
Contributor Author

prrace commented Aug 19, 2025

/integrate

@openjdk
Copy link

openjdk bot commented Aug 19, 2025

Going to push as commit 0858743.
Since your change was applied there have been 145 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Aug 19, 2025
@openjdk openjdk bot closed this Aug 19, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Aug 19, 2025
@openjdk
Copy link

openjdk bot commented Aug 19, 2025

@prrace Pushed as commit 0858743.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client [email protected] integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

5 participants