Skip to content

Fix IonFactory resource cleanup on failed construction - #791

Open
Dongnyoung wants to merge 4 commits into
FasterXML:3.xfrom
Dongnyoung:fix-ion-failed-construction-cleanup
Open

Dongnyoung wants to merge 4 commits into
FasterXML:3.xfrom
Dongnyoung:fix-ion-failed-construction-cleanup

Conversation

@Dongnyoung

Copy link
Copy Markdown
Contributor

As discussed in #780
Fixes IonFactory resource cleanup when parser or generator construction fails after opening File/Path resources.

IonFactory handles these paths separately from the base factory implementations, so the failed-construction cleanup in the base factories does not cover these Ion-specific paths.

Parser cleanup

There are two failure points that require different cleanup handling:

  1. Failure before an IonReader is created, for example during input decoration.

    • The InputStream opened by IonFactory is still owned by the outer File/Path construction path.
    • If construction fails here, that stream needs to be closed directly.
  2. Failure after an IonReader is created, while constructing the IonParser.

    • At this point, resource management has been transferred to the IonReader/parser construction path.
    • If parser construction fails, the IonReader is closed there, which also closes the factory-created input stream.

The initial implementation used a simple try/catch around the outer File/Path path and closed the opened stream on any failure. However, this could close the same stream again when the failure occurs after the IonReader has already taken ownership and been closed.

The implementation therefore tracks whether ownership has been transferred to the downstream parser construction path. The outer failure handler only closes the stream if that transfer has not happened yet.

IOContext instances created during parser construction are also released on failure so that their BufferRecycler leases are returned.

Generator cleanup

The File/Path generator paths have the corresponding cleanup issue: if generator construction fails after IonFactory opens the output stream, the factory-created stream and its IOContext need to be cleaned up.

Changes

  • Close factory-created input streams when input decoration fails before ownership is transferred.
  • Close the IonReader when parser construction fails after it has been created.
  • Avoid closing the same input resource again from the outer File/Path failure handler after ownership has been transferred.
  • Close factory-created output streams when generator construction fails.
  • Release IOContext instances on failed parser/generator construction.
  • Add regression tests for File and Path parser/generator failure paths.

Verification

.\mvnw.cmd -pl ion -DskipITs -DskipCheckstyle -Dtest=IonFactoryFailedConstructionTest "-Dsurefire.useModulePath=false" test

@github-actions

github-actions Bot commented Sep 15, 2026

Copy link
Copy Markdown

🧪 Code Coverage Report

Coverage Type Coverage Change
📝 Instructions 78.11% 📈 +0.06%
🔀 Branches 70.12% 📉 -0.01%

if (!inputCleanupDelegated) {
_closeOnFailedConstruction(in, e);
}
_releaseContextOnFailedConstruction(ioCtxt, e);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would this not work in finally block?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, thanks! 😊😊

@cowtowncoder cowtowncoder changed the title Fix IonFactory resource cleanup on failed construction Fix IonFactory resource cleanup on failed construction Sep 15, 2026
@cowtowncoder

Copy link
Copy Markdown
Member

Good! Just needs release notes.

@Dongnyoung
Dongnyoung force-pushed the fix-ion-failed-construction-cleanup branch from b6ff33d to 4b89e7b Compare September 16, 2026 02: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.

2 participants