Skip to content

Close the digest stream when shaDigestForFile can't read a file - #116

Merged
pjfanning merged 1 commit into
apache:trunkfrom
pjfanning:leak-schema-resource-manager-parse
Sep 7, 2026
Merged

Close the digest stream when shaDigestForFile can't read a file#116
pjfanning merged 1 commit into
apache:trunkfrom
pjfanning:leak-schema-resource-manager-parse

Conversation

@pjfanning

Copy link
Copy Markdown
Member

BaseSchemaResourceManager.shaDigestForFile closes the stream only after the read loop completes:

DigestInputStream str = digestInputStream(inputStreamForFile(filename));

byte[] dummy = new byte[4096];
int i = 1;
while (i > 0) {
    i = str.read(dummy);
}

str.close();

An IOException part-way through skips the close. The caller, syncCacheWithLocalXsdFiles, swallows it —

} catch (IOException e) {
    // unable to read digest... no problem, ignore then
}

— and carries on to the next file, so sdownload -sync over a cache containing unreadable .xsd files leaks one handle per file instead of failing.

Wrapped in a try-with-resources. getMessageDigest().digest() moves inside the block; it reads the digest state and does not require the stream to be closed first.

Added SchemaResourceManagerStreamTest, which serves a stream that opens and then fails every read. It fails on trunk and passes with this change. Full suite: 3171 tests pass (170 skipped).

🤖 Generated with Claude Code

shaDigestForFile calls str.close() only after the read loop finishes, so an
IOException part-way through leaks the stream. The caller,
syncCacheWithLocalXsdFiles, swallows that exception - "unable to read digest...
no problem, ignore then" - and carries straight on to the next file, so an
sdownload -sync over a cache with unreadable .xsd files leaks one handle per
file rather than failing.

Wrap it in a try-with-resources.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@pjfanning
pjfanning merged commit 1c14e8d into apache:trunk Sep 7, 2026
3 checks passed
@pjfanning
pjfanning deleted the leak-schema-resource-manager-parse branch September 7, 2026 13:42
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.

1 participant