Skip to content

Register default file formats on demand instead of at class init - #484

Open
mattjala wants to merge 1 commit into
HDFGroup:masterfrom
mattjala:format_registration_cycle
Open

Register default file formats on demand instead of at class init#484
mattjala wants to merge 1 commit into
HDFGroup:masterfrom
mattjala:format_registration_cycle

Conversation

@mattjala

Copy link
Copy Markdown
Collaborator

FileFormat's static initializer registered the default formats by instantiating each implementing class. Every implementing class is a subclass of FileFormat, so whenever a subclass was itself what triggered FileFormat's initialization, the JVM was already initializing that subclass on the same thread. Class.forName then returned immediately rather than waiting, and the instance was built from a class whose own static fields were still unassigned.

This specifically became an issue for H4File, though it could easily have happend to NC2File or FitsFile instead.

Registration now happens on the first call to a method that consults the list of supported formats. Those calls come from outside any class initialization, so Class.forName completes the subclass before the instance is built. The formats are also a table rather than four copies of the same block, and a format that fails to load no longer stops the ones after it.

Fixes #481

…ation

FileFormat's static initializer registered the default formats by instantiating
each implementing class. Every implementing class is a subclass of FileFormat, so
whenever a subclass was itself what triggered FileFormat's initialization, the
JVM was already initializing that subclass on the same thread. Class.forName then
returned immediately rather than waiting, and the instance was built from a class
whose own static fields were still unassigned.

This specifically became an issue for H4File, though it could easily have
happend to NC2File or FitsFile instead.

Registration now happens on the first call to a method that consults the list of
supported formats. Those calls come from outside any class initialization, so
Class.forName completes the subclass before the instance is built. The formats
are also a table rather than four copies of the same block, and a format that
fails to load no longer stops the ones after it.

Fixes HDFGroup#481
@mattjala mattjala added the Component - Object Library Improvements to the object library label Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Component - Object Library Improvements to the object library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

H4File registration fails due to a class initialization cycle

1 participant