Skip to content

Add MustCall annotations to resource-owning JDBC, MIDI, and audio interfaces#254

Open
parameshn wants to merge 8 commits intotypetools:masterfrom
parameshn:mustcall-annote
Open

Add MustCall annotations to resource-owning JDBC, MIDI, and audio interfaces#254
parameshn wants to merge 8 commits intotypetools:masterfrom
parameshn:mustcall-annote

Conversation

@parameshn
Copy link
Copy Markdown

@parameshn parameshn commented Jan 22, 2026

Add MustCall annotations to core JDBC, MIDI, and audio interfaces that own native or external resources.
These annotations model required close() obligations and propagate correctly to all subinterfaces, enabling the MustCall Checker to detect resource leaks.

Related to typetools/checker-framework#6354.

This ensures that close() obligations are enforced consistently and propagate to all subinterfaces such as Mixer, SourceDataLine, and TargetDataLine, without requiring redundant annotations on each subtype.

Audio Lines own native system resources; failing to call close() leaks OS-level audio handles. Annotating Line enforces this lifecycle and correctly propagates to all subtypes.
Closing a MidiDevice:

-releases native MIDI resources
-stops internal threads
-automatically closes all associated transmitters and receivers

Indicates that the application has finished using the receiver, and that limited resources it requires may be released or made available.
Closing a MidiDevice:
 -releases native MIDI resources
-stops internal threads
-automatically closes all associated transmitters and receivers Indicates that the application has finished using the receiver, and that limited resources it requires may be released or made available.
All ResultSet and RowSet implementations, both connected and disconnected, should be explicitly closed. Connected types release live database resources such as connections and cursors, while disconnected types still hold internal resources that should not be left to garbage collection, especially in long running applications.
Copy link
Copy Markdown

@msridhar msridhar left a comment

Choose a reason for hiding this comment

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

A couple of quick comments. Also, I think we probably want to have a corresponding PR in typetools/checker-framework with tests for these changes. To do that properly, see here for instructions.

Also, looks like WPI tests are failing. @kelloggm any idea how to debug that?

Comment on lines +100 to +101
@AnnotatedFor({"mustcall"})
@InheritableMustCall("close")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Based on the Javadoc, I think we may need @NotOwning annotations on methods like MidiSystem#getReceiver and MidiSystem#getTransmitter, to indicate that the MidiDevice returned by those APIs does not need to be explicitly closed. Do you agree?

Copy link
Copy Markdown
Author

@parameshn parameshn Jan 26, 2026

Choose a reason for hiding this comment

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

From my understanding, when MidiSystem.getReceiver() / getTransmitter() are used, the underlying MidiDevice is opened implicitly by the system and closed implicitly when the returned Receiver / Transmitter is closed (assuming no other Receivers/Transmitters remain open). As a result, the application does not own the MidiDevice; its responsibility is only to close the returned Receiver / Transmitter. Since the MidiDevice is not exposed by these APIs, there isn’t really a place to annotate it with @NotOwning at the API level.

// Requests a Receiver from the system.
// Implicitly opens the underlying MidiDevice
        Receiver receiver = MidiSystem.getReceiver();

// Closes the Receiver.
 // Implicitly closes the MidiDevice (if this was the last open one)
        receiver.close();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think I misunderstood what was going on here. We don't need @NotOwning annotations on these APIs.

* @since 1.3
*/
@AnnotatedFor({"mustcall"})
@InheritableMustCall("close")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is a Line object automatically open at the time it is constructed, is an explicit open() call required? If it's the latter, we might need to treat Line like Socket and add a @CreatesMustCallFor annotation somewhere. This might be tricky since Line is an interface.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It’s the latter: a Line is not automatically open when obtained, and an explicit open() call is required.
Given that Line is an interface with multiple implementations and open(...) variants, annotating Line itself seems likely to be too coarse and incomplete. I think a similar argument applies to MidiDevice as well, since it is also an interface and can be opened both explicitly and implicitly. I’d appreciate your thoughts on whether there’s a better way to model this.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is a bit tricky. I think ideally, we'd annotate any relevant methods that create an unopened Line as @MustCall({}), and then annotate any open methods that actually acquire the resource as @CreatesMustCallFor (like here). Does this seem infeasible?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I think we need something like this for this instance

if method overrides another method:
    if overridden method has @InheritableCreatesMustCallFor:
        treat current method as if it has @CreatesMustCallFor

custom one maybe?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@msridhar If that approach seems reasonable, I’m happy to prototype it and share a draft PR for feedback before applying it to Line and MidiDevice.

Copy link
Copy Markdown
Author

@parameshn parameshn Mar 11, 2026

Choose a reason for hiding this comment

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

Hi
just checking in on this discussion.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sorry @parameshn I've been very busy, hoping to get back to this next week

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

No worries, thanks for the update! Looking forward to your thoughts.

@kelloggm
Copy link
Copy Markdown

WPI tests are failing for JDKs 11 and 17 only (but not 21 and 25) because of changes upstream; the CF itself only runs the WPI tests on the latest LTS JDK now, and they're not supported for older JDKs. The particular problem is that the build scripts used by the target projects use -Xlint:all,-processing,-this-escape; the -this-escape xlint option is present in the Java 21 docs but not in the Java 17 docs. So, the right change is probably to disable the WPI tests for Java 11/17 entirely on this repo.

Statement owns independent JDBC resources and must be explicitly closed. Relying on Connection.close() is unsafe (e.g., with connection pooling) and not clearly guaranteed by the JDBC specification. Annotating Statement enables sound resource-leak detection and reflects recommended JDBC usage.
@msridhar
Copy link
Copy Markdown

@parameshn sorry for being slow to reply here. I wonder if we should pull out the SQL changes into their own PR? Those look fine / uncontroversial to me, whereas the audio stuff is trickier.

@parameshn
Copy link
Copy Markdown
Author

No problem at all 🙂

Yes, I’ve split the SQL changes into a separate PR so they can be reviewed independently. I also think we can proceed with Receiver and Transmitter in a separate PR, since their lifecycle is straightforward and shouldn’t introduce the same complications as Line and MidiDevice.

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.

3 participants